Site icon Intermittent Technology

ESP8266 WiFi LED dimmer Part 3 of X: Flashing and programming the ESP-01

To get started with the ESP8266 ESP-01 I recommend flashing and programming it before soldering it down. This way you know it’s working and that the program code is also functioning. Depending on how you mount it on your board it can be a bit hard to do so later on!

DSC_0154

Connect the board to USB using an Arduino or Serial-to-USB adapter

This series has been rebooted

Please take a look at the following post to visit the new rebooted series and index of all posts: https://blog.quindorian.org/2016/07/esp8266-lighting-revisit-and-history-of-quinled.html/


On the photo I used an Arduino Nano v3 (Chinese version) as a USB to Serial bridge. If you load something into the Arduino that doesn’t use the serial pins (such as the blinky sketch) this should work without problem.
 
A little warning, since the Arduino has 5v on it’s serial pins you will need to supply the ESP-01 with 5v also. This is officially NOT supported. For me this has worked without problems for about a week now, but you’re warned. 😉 Mixing 3.3v and 5v also does not work. I recommend using the USB-to-serial adapters I mentioned in the previous article since you can select the output voltage on it and it can also supply a little bit more power then an Arduino can. With the Arduino on USB as a power source the lights would flicker a lot and serial input could be very spotty (often corrupt) with WiFi also connected. With disconnected WiFi it is manageable though certainly not perfect.
 
With the adapter it’s easy. You just need to connect the right pins together with some dupont cables (See previous post).
 

Firmware flash mode

To get the ESP-01 in firmware flash mode we will need to connect it in a special way. 
Schematic overview of the ESP-01. Mind the orientation of the board, this is viewed from the top side

Normal running mode

The default pins to connect are:
ESP-01 VCC – 3.3v Serial Adapter
ESP-01 GND – Ground or Minus Serial Adapter
ESP-01 RX – Connect to TX on Serial Adapter
ESP-01 TX – Connect to RX on Serial Adapter
ESP-01 CH_PD – 3.3v Serial Adapter

Since two pins need to be connected to 3.3v it’s easiest to either use a breadboard with some dupont cables or a screw connector block to multiply the connection.
 
This will set the ESP-01 in normal operating mode. 

Flash mode

To get into firmware flash mode you need to make two additional connections, easiest is to use a screw connector block or breadboard for this.

 
ESP-01 GPIO0 – Pull low by connecting to ground / minus
ESP-01 GPIO2 – Pull high by connecting to 3.3v
 
When you reboot the module it should be in firmware flash mode!
 

Downloading the software

The next step is to get the software. I am using the NodeMCU firmware with the LUA language to make it all work. There are three sources for this, there is http://www.esp8266.com/ and there is the NodeMCU website http://www.nodemcu.com/
 
The third source is directly from Github, this should always contain the most recent version of the firmware and programs. I recommend using this way and I will describe it here. 
 
Go to https://github.com/nodemcu and go to the nodemcu-firmware part. There you click the “Download Zip” button on the right of your screen. 

You should end up with a zip file with all the files in it. Extract it. The files we are looking for are located in the nodemcu-firmware-masterpre_build.9.4512k-flash directory and the file we are going to flash is nodemcu_512k_latest.bin
 
Then go back to https://github.com/nodemcu and select nodemcu-flasher and do the same (Download zip). Extract it into a directory and run the program located in the Win64/Release directory.
 
It should show you window like this:
 
Next go into the second tab “Config” and use the machine cogwheel button and select the nodemcu_512k_latest.bin
 
If your ESP-01 is connected correctly and in firmware flash mode it should automatically fill in the MAC addresses when you click flash. The following should take place:
 
When flashing is successful we can continue. 

Sadly, this isn’t as easy as it was written here, it should be, but the 3 modules I flashed needed toying and trying again and again for about an hour each to actually get it to go into flash mode. Once it did there was no problem and it flashed just fine, but still it was a bit hard to get it do so sometimes. I still don’t know yet why it doesn’t always work the first time, it might be because I was using a 5v Arduino.
 
Once you have managed to get the firmware flashed, disconnect the firmware flashing pins:
ESP-01 GPIO0 – Pull low by connecting to ground
ESP-01 GPIO2 – Pull high by connecting to 3.3v
 

Installing the QuinLED program on the ESP-01

For sending the programming to the ESP-01 I recommend downloading a tool called ESPlorer, you can download it here. It’s a JAVA based tool so you need to install the latest Java version to run it. Sadly Java also has some downsides, when coding for a while inside of ESPlorer it can slow down quite a lot especially when sending the code over serial to the ESP-01. Restarting the program quickly fixes the problem, but it kind of sucks. Hopefully the author of the program is able to fix this!
 
Having started the program you will be presented with the main interface:
 
On the top in the right you can select your COM port (check in device manager) and select 9600 as the baud rate. Hit connect and you are connected to the now LUA running ESP-01 over serial.
 
Here you can issue commands to the ESP-01.
 
Some useful commands to remember:
 
  • node.restart();  –  Restarting the ESP8266
  • wifi.setmode(wifi.STATION)  –  Setting it to WiFi client mode
  • wifi.sta.config(“SSID”,”PASSSSS”)  –  Connecting to your WiFi network
  • print(wifi.sta.getip())  –  Get the IP of the ESP8266
  • wifi.sta.connect()  –  Connect to the set SSID
  • wifi.sta.disconnect()  –  Disconnect wireless
  • print(“Hello World!”)  –  Print some text
You can paste these commands in the little white box next to send and then execute and select them quickly when you need them.
 
To install the QuinLED dimming program you need to paste the following in the code window:
 
This is version 0.4 of my code, it will probably get updated at some point. I also need to add some documentation in the code at some point. 😛


pwm.setup(3, 1000, 005)
pwm.setup(4, 1000, 005)
pwm.start(3)
pwm.start(4)

LED1_current=005
LED1_target=005
LED2_current=005
LED2_target=005

Fadetime1=5000
Fadetime2=5000

Stepcounter1=0
PosStepcounter1=0
DimTimer1=0

Stepcounter2=0
PosStepcounter2=0
DimTimer2=0

wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PASSSS")



srv=net.createServer(net.TCP) 
srv:listen(43333,function(conn) 
    conn:on("receive",function(conn,payload) 
   
    print("Input:"..payload) 
 
    if string.find(payload,"LED1") then
     LED1_target=tonumber(string.sub(payload, 13) )
     print("Received LED1 Target Value: "..LED1_target)

     Stepcounter1=(LED1_target)-(LED1_current)
     
     if (Stepcounter1) < 0 then
      PosStepcounter1=(Stepcounter1)*-1
      else PosStepcounter1=(Stepcounter1)
     end
     
     if (PosStepcounter1) == 0 then
      PosStepcounter1=(PosStepcounter1)+1
      else PosStepcounter1=(PosStepcounter1)
     end
          
     DimTimer1=(Fadetime1)/(PosStepcounter1)

     if (DimTimer1) == 0 then 
      DimTimer1=(DimTimer1)+1
      else DimTimer1=(DimTimer1)
     end

      print (Fadetime1)
      print (Stepcounter1)
      print (PosStepcounter1)
      print (DimTimer1)
      print (LED1_current)
      print (LED1_target)


    tmr.alarm(0, (DimTimer1), 1, function() 
     if LED1_current < LED1_target then 
      LED1_current = (LED1_current + 1) 
      pwm.setduty(3, LED1_current)
    elseif LED1_current > LED1_target then 
      LED1_current = (LED1_current - 1) 
      pwm.setduty(3, LED1_current)
    elseif LED1_current == LED1_target then tmr.stop(0)
     end end )
    end

    if string.find(payload,"LED2") then
        print("Received LED2 Target Value")
     LED2_target=tonumber(string.sub(payload, 13) )
     
     Stepcounter2=(LED2_target)-(LED2_current)
     
     if (Stepcounter2) < 0 then
      PosStepcounter2=(Stepcounter2)*-1
      else PosStepcounter2=(Stepcounter2)
     end
     
     if (PosStepcounter2) == 0 then
      PosStepcounter2=(PosStepcounter2)+1
      else PosStepcounter2=(PosStepcounter2)
     end
          
     DimTimer2=(Fadetime2)/(PosStepcounter2)

     if (DimTimer2) == 0 then 
      DimTimer2=(DimTimer2)+1
      else DimTimer2=(DimTimer2)
     end

      print (Fadetime2)
      print (Stepcounter2)
      print (PosStepcounter2)
      print (DimTimer2)
      print (LED2_current)
      print (LED2_target)


    tmr.alarm(1, (DimTimer2), 1, function() 
     if LED2_current < LED2_target then 
      LED2_current = (LED2_current + 1) 
      pwm.setduty(4, LED2_current)
    elseif LED2_current > LED2_target then 
      LED2_current = (LED2_current - 1) 
      pwm.setduty(4, LED2_current)
    elseif LED2_current == LED2_target then tmr.stop(1)
     end end )
    end
    end)
    end)

print ("Booted to QuinLED_ESP8266_V0.4")
 
Be sure to change the SSID and Passcode to what you use in your own network. 

When you have it pasted in ESplorer press the “Save to ESP” button to transfer it over. It will ask you for a filename and where to store it. The where is only a local place what the file will also be kept, the filename is important and I recommend you name it “init.lua”. That way it will automatically run when the ESP-01 boots.

The tool should now be pasting the code to the ESP-01 line per line and checking if it made it across intact. I sometimes have that the code will arrive corrupt and the tool will error. When that happens I recommend you give the ESP-01 a restart and then a WiFi disconnect command. Doing that always makes the ESP-01 much more stable for me and transferring the program shouldn’t be a problem.

Once the ESP-01 is programmed it might complain that it can’t start a second TCP server (it depends on what you where running or if you had some fail flashes). Just give the unit a reset. 

Once it’s booted again you can use the above mentioned commands to ask for it’s IP. You should be able to ping that IP. Don’t be alarmed by it’s ‘not so great’ ping times, this is normal.
 


Issuing commands to the ESP-01 running QuinLED

Now that our unit is running the QuinLED program you should be able to send commands to it. If you still have a serial connection open it will post some logging about this too.
 
For sending the commands to the ESP-01 you can either use a putty session in RAW mode to open the TCP port (43333) and type in or paste your command or use a little tool called “netcat”. It is available on most Linux distributions or you can get it in cygwin when running Windows.
 
To send a command to LED1 you type:

echo LED1_target=1000 | nc ip.ip.ip.ip 43333
 

That should said GPIO0 to a duty cycle of 1000. A duty cycle can be anything in the range of 0 to 1023. Do not use a number higher then that because right now the program will crash. 😉 It’s no use anyway because the ESP8266 supports 1024 PWM duty cycle levels so 1023 is fully open.



And that should be it, you can now control both channels of the ESP-01 connected to your LED strip. In my next post I will explain how you can hook this up into Domoticz and give it control over both channels independently.

Exit mobile version