Site icon Intermittent Technology

ESP8266 LED Lighting: Programming and controlling QuinLED

It’s been a while since the last article and video about the ESP8266 but finally the article and video on how how to program QuinLED and then control it from your PC is here!

This post is part of a series

The index for this series can be found here. 

Video

As is now quite common I’ve made a detailed video about how to flash, program and then control the board. I will type it out in text form in this article too, but being able to see how it works always works best in my opinion!


 

 

New boards designs coming soon!

Currently, all DirtyPCB links are down because of a change to their website, coincidentally we also moved to the new house and officially don’t have any internet yet which is why the FTP site is ALSO down. So currently there is no way to order boards.

I have been working on a slightly optimized and updated board design though. I already have the first “print” in and just need to make a few corrections to it before it can be released into the world. So hold on just a bit longer, I should have them available soon!

 Programs, firmware and code ZIP file

I have uploaded an updated ZIP file which contains all the programs, firmware and code you are going to need to use QuinLED.

Click the giant icon below to start the download.

Click here to download the ZIP file

Flashing QuinLED

Since the last article I’ve acquired some convenient little USB adapters with a serial chip on-board which allow you to plug in and run an ESP-01 directly from your USB port. This saves a lot of time because you don’t have to fiddle with serial wires and such. Once you are done programming it, just plug it into a QuinLED board and you are done!

As can be seen in the video I have multiple of these little adapters of which I have modded one to always be in “flash mode”. That way I can very quickly flash an ESP-01, then plug it into a normal USB adapter, program it and them move it over to QuinLED.

I can certainly recommend getting a few of these! You can get them here. If you are uncomfortable soldering one yourself, you could also get this version which has a flash button on it!

*If you want more detailed instructions on how to use the flashing program, please look in the previous article (or watch the video)

Programming QuinLED

Once you’ve flashed the ESP-01 you want to use on the QuinLED board (either using serial wires or the USB adapter) open up ESPlorer. You will need to have Java installed for it to work.

Connecting to the ESP-01

ESPlorer should automatically detect the COM port your ESP is connect to, if not, select the right port and click op “open”.

That should open the serial port and make the connection with the ESP-01 module. Hit the “heap” button (lower part of the window) a few times and see if you get a response back. Often the first and second time will be a bit of garbage but after that it should work.

Loading the needed code

I’ve prepared a “init.lua” file which holds the whole program needed to have the ESP-01 make a network connection and perform the dimming functions.

In ESPlorer click the open file button and load the “init.lua” file included in the ZIP file.

There is lots of code in there! In a general overview it’s structured like this:

That’s a very high level over view, but this series isn’t about programming!

Program code

As written above, all the code is in the ZIP file you can download but if you’d rather copy and paste it, please also find it here:

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
 
Fadetime=5000
 
Stepcounter1=0
PosStepcounter1=0
DimTimer1=0
 
Stepcounter2=0
PosStepcounter2=0
DimTimer2=0
 
wifi.setmode(wifi.STATION)
wifi.sta.config("WIFISSID","PASSWORD")
 
--register callback: use previous state
wifi.sta.eventMonReg(wifi.STA_CONNECTING, function(previous_State)
    if(previous_State==wifi.STA_GOTIP) then 
        print("Station lost connection with access point\n\tAttempting to reconnect...")
    else
        print("STATION_CONNECTING")
    end
end)
wifi.sta.eventMonStart()
 
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, 28) )
     print("Received LED1 Target Value: "..LED1_target)
 
     Fadetime=tonumber(string.sub(payload,11,14) )
     print ("Received LED Fadetimer: " ..Fadetime)
     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
          
     DimTimer=(Fadetime)/(PosStepcounter1)
 
     if (DimTimer) == 0 then 
      DimTimer=1
      else DimTimer=(DimTimer)
     end

     if (DimTimer) < 1 then 
      DimTimer=1
      else DimTimer=(DimTimer)
     end
 
      print (Fadetime)
      print (Stepcounter1)
      print (PosStepcounter1)
      print (DimTimer)
      print (LED1_current)
      print (LED1_target)
 
 
    tmr.alarm(0, (DimTimer), 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, 28) )
 
     Fadetime=tonumber(string.sub(payload,11,14) )
     print ("Received LED Fadetimer: " ..Fadetime)
     
     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
          
     DimTimer=(Fadetime)/(PosStepcounter2)
 
     if (DimTimer) == 0 then 
      DimTimer=1
      else DimTimer=(DimTimer)
     end

     if (DimTimer) < 1 then 
      DimTimer=1
      else DimTimer=(DimTimer)
     end
 
      print (Fadetime)
      print (Stepcounter2)
      print (PosStepcounter2)
      print (DimTimer)
      print (LED2_current)
      print (LED2_target)
 
 
    tmr.alarm(1, (DimTimer), 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.5.5")

 

Setting the correct WiFi network and password

In the code there is a line called:

wifi.sta.config("CampZone-pub","")

This is the line we need to change to match the settings for your home WiFi network.

To change it edit the line to contain your WiFi network details such as:

wifi.sta.config("blablaWiFi","pass123supersecret")
Saving and running the code on the ESP-01

To save the code to the ESP-01, hit the “Save to ESP” button below the code screen. This will start the transfer of the code line for line to the ESP-01 module.

After that’s done, hit “Save&Compile” and when that completes hit the “Reset” button.

Now your ESP-01 will be running the new code and should be connected to your WiFi network!

Checking if everything works and finding the IP address

The code uses DHCP to ask an IP from the WiFi network. But which IP did the ESP-01 get?

Using the command:

=wifi.sta.getip()

The module will report back which IP it got. Try and ping it to see if you can reach it!

Controlling QuinLED

Now that programming is done, the next step is controlling QuinLED to dim your LED lights!

Installing Netcat

To control QuinLED I make use of a program called Netcat. This is a little tool which uses TCP sockets to send text data to another network device. In the case of my QuinLED code this is using TCP 43333.

There is no official way to install Netcat (or NC for short). Mostly I just dump the entire contents of the ZIP file (included in the ZIP file above) into a directory that’s in the path (such as c:\windows\system32). After that you can run the command from anywhere but just calling “nc.exe”.

Sending a dim command to QuinLED

In the ZIP file I have also included the command line way to send dimming commands to QuinLED!

It looks like this:

echo Fadetimer=2500,LED1_target=888 | nc -w 2 IP.IP.IP.IP 43333

Fill in the correct IP number where it says IP.IP.IP.IP and try to see if the command works. If the ESP-01 receives the commands you should see some status messages scroll in the output window of ESPlorer.

If you did, that means everything is working and you are ready to connect the ESP-01 module to the QuinLED board. Or if it was already on there, you should have seen any connected LED strip dim to the desired value!

Ending remarks

And that should be it, you now have a fully WiFi network controllable LED dimmer!

Next in this series is how to use the same setup and connect everything to Domoticz given it all a nice front-end and automation capabilities!

Exit mobile version