ESP8266 WiFi LED dimmer Part 7 of X: Updated Dimmer and Domoticz code

I recently did a small addition to my dimmer code which gives you the ability to add the dimmer time to the sent values remotely. This way you can choose to dim quickly or slowly per dim action.


This series has been rebooted

Please take a look at the following post to visit the new rebooted series and index of all posts:

I made code for the ESP8266 modules and also changed my Domoticz code by cause the command input/protocol also changes. Currently you can’t change the dimspeed setting in Domoticz itself, only by changing it by the switch associated LUA file.

The reason why I wanted to make these changes is because I want automatic/scheduled turning on and off of lights to be done very slow and smoothly but manual turn on (using a physical switch) to be fast and almost instant. Both need to run through Domoticz but would require different settings.

ESP8266 LUA code

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






    if string.find(payload,"LED1") then
     LED1_target=tonumber(string.sub(payload, 29) )
     print("Received LED1 Target Value: "..LED1_target)

     Fadetime1=tonumber(string.sub(payload,12,15) )
     print ("Received LED1 Fadetimer: " ..Fadetime1)
     if (Stepcounter1) < 0 then
      else PosStepcounter1=(Stepcounter1)
     if (PosStepcounter1) == 0 then
      else PosStepcounter1=(PosStepcounter1)

     if (DimTimer1) == 0 then 
      else DimTimer1=(DimTimer1)

      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 )

    if string.find(payload,"LED2") then
        print("Received LED2 Target Value")
     LED2_target=tonumber(string.sub(payload, 29) )

     Fadetime2=tonumber(string.sub(payload,12,15) )
     print ("Received LED1 Fadetimer: " ..Fadetime2)
     if (Stepcounter2) < 0 then
      else PosStepcounter2=(Stepcounter2)
     if (PosStepcounter2) == 0 then
      else PosStepcounter2=(PosStepcounter2)

     if (DimTimer2) == 0 then 
      else DimTimer2=(DimTimer2)

      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 )

print ("Booted to QuinLED_ESP8266_V0.5")

Domoticz Code

Then we need some new code for Domoticz, the general input line will now be the following:
echo Fadetimer1=9000,LED1_target=888 | nc -w 2 43333

Be sure to always keep the dimming value between 1000 and 9000! The “-w 2” adds a timeout value to the netcat command so Domoticz doesn’t hang too long if the netcat command does not return when a ESP is turned off or not reachable. I made it a variable in the script so everyone can choose their own timeout.

commandArray = {}

DomDevice = 'ESP-01_PCB_1'

IP = ''
Port = '43333'

LEDtarget = 'LED1_Target='
Fadetimer = 'Fadetimer1='
Waittime = '2'
FadeTime1 = '8000'

 if devicechanged[DomDevice] then
   if(devicechanged[DomDevice]=='Off') then DomValue = 0;
   print ("Turning off " .. DomDevice);
   runcommand = "echo " .. (Fadetimer) .."" .. (FadeTime1) .. "," .. (LEDtarget) .. "0  | nc -w " .. (Waittime) .. " " .. (IP) .. " " .. (Port) .. " ";
   print (runcommand);
 return commandArray
    DomValue = (otherdevices_svalues[DomDevice]);
   CalcValue = DomValue * 33;
   print ("Value received from Domoticz was " .. (DomValue) .." ");
   print ("Calculated value for ESP is " .. (CalcValue) .." ");
   print ("Dimming "  .. (DomDevice) .. " to " .. (CalcValue) .. " ");
   runcommand = "echo " .. (Fadetimer) .."" .. (FadeTime1) .. "," .. (LEDtarget) .. "" .. (CalcValue) .. " | nc -w " .. (Waittime) .. " " .. (IP) .. " " .. (Port) .. " ";
   print (runcommand);
return commandArray


11 thoughts on “ESP8266 WiFi LED dimmer Part 7 of X: Updated Dimmer and Domoticz code”

  1. Thank you for your work! Please make a video from the beginning how to write correctly using ESPlorer the init files.lua in ESP8266 and later in Domoticz. I'm not good in programming and not know your language sorry but I know how to solder and have a great desire to repeat your project. I have a home server Domoticz on the Raspberry Pi and soldered the fee for your photos.

  2. I am slowly moving forward with the implementation. This time, I am facing the following issue: when configuring the devices I have created LED_1 and LED_2 hardware and LED-GPIO-0 asnd LED-GPIO-2 switches exactly like shown in the post. Unfortunately, when trying to operate the switch, I am getting (LED_2) Lighting 5 (Unknown) errors and the code in script is not executed. Any ideas?

  3. For the sake of others, who might experience the same situation, on Domoticz for Windows, it seems, the switches created manually were not synced (for the lack of better words) with Devices in Setup.
    As a result, Switch created manually, when trying to operate, was creating a new Unknown device. In order to resolve it, it was necessary to create Switch, operate it, go to Setup/Devices, delete Switch, rename existing Unknown one to the old name and create Switch from that page.
    After this complicated operation, all is working fine.

  4. I have finally built the dimmer and the Domoticz part works fine – I am able to set the level and turn on/off the dimmer using nc. The issue is however with LEDs – there is huge difference between LED strip powered directly from the power supply and via dimmer, even at max light level (1023). See the difference:
    It is build on prototype board, not on PCB. I have checked and the output voltage is 12,4V when no LED strip is connected and it drops to 7,7V when I connect even the small (6 pcs like on the photo) LED strip. Are the cables too thin to provide enough current, or something totally different is wrong?

    Do you have any idea, what might be wrong? Other than that, it seems to be working as expected.

  5. Sadly, I think I might know what is wrong. Try to set the voltage you feed the ESP8266 to a bit higher, like 4V. I'm pretty sure that will increase the LED strip to almost full brightness.

    The problem is that the MOSFETs are wrong spec. Sadly, ordering from China I have also had one batch which did not function correctly and after doing some research it turns out there where different data sheets which use a different max gate voltage. The max gate voltage should be under 3.3V and for the MOSFETs I received it was 4V. And that also corresponded directly to the less brightness I was seeing.

    !!Running the ESP8266 anywhere above 3.6V will kill it faster though, how fast, no one knows, but it's not a permanent solution.

    *On my last order, I have had good luck with this seller. They actually state the Vgs(th) (Max) @ Id 2.5V @ 250ยตA we are looking for!

  6. It is really sad news,but I am not sure, if I understand fully – do they sell P16NF06 as P16NF06L (there is a difference staded between them for the gate voltage 2-4V vs 1-2,5V), they sell correct ones but not keeping the parameters or this something different altogether? Is there any way to check this – ie. measure any voltages at purchase itme (talking about local purchasing)?

  7. Hi Quindor, many thanks for sharing this code! I have not built such an awesome, sophisticated 2-channel dimmer like you did, but it helped me a lot on my last project. I created a single channel dimmer for one of those cheap (Action) 12V/5m/300led strands, very cool to be able to dim that using wifi!!
    I was able to power the ESP8266 using the led strip power adaptor with a voltage regulator(LD117 3v3) and a 10 micro farad cap. As a mosfet i used an IRL2203N. It just sits between the power adaptor and the strand ๐Ÿ™‚

    2 remarks on your code: i believe in line 82 of the nodemcu code you accidentally mistyped LED1 for LED2, i just noticed that.
    Also, about the netcat stuff and the -w parameter: i found using the "&" at the end of the command works best when using os.execute() commands in domoticz, it runs the command in the background and doesn't wait for it to finish, the -w parameter is maybe best to be kept for safety, but running the command in the background in domoticz is a life saver ๐Ÿ˜‰

    Also a question: i used a AC/Lighting2 dummy dimmer device in domoticz and there seem to be only 14 levels it can be set to.
    Please see my script for details:

    I used a non-linear scale because it offers better dimming granulation; using a linear scale makes all the higher levels look alike and at the lower dimming levels the changes are too big, but of course that is subject to personal preference ๐Ÿ™‚
    Also i accounted for a minimum level of dimming needed to actually light up the strand.
    The question: in your code you multiplied with 33, but doesn't that make a maximum level of 33*14=462 (out of a thousand)? Maybe you used a different type of dummy dimmer that has a higher resolution (>14)?

    Anyway with your blog it was very easy to create a fast-responding, cheap and simple dimmer for domoticz, many thanks for that!

    For others that are interested in the schematics here is a picture of my quick and dirty note:
    The resistor between the ESP8266 and the gate on the mosfet is 1k2.

  8. Thanks, thank you for sharing, my family is using, I can’t get your dimming firmware to access my home assistant, hope you see my message and thank you again. Life brings beauty!

    1. Since it uses a protocol I developed myself, I don’t think it’s going to be automatically compatible with Home Assistant, you will probably have to write a script extension like I have done for Domoticz.

Leave a Reply

Your email address will not be published. Required fields are marked *