Wednesday, March 4, 2015

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.


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)
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("Q-LAN","lkadfsjlkjadsf")



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, 29) )
     print("Received LED1 Target Value: "..LED1_target)

     Fadetime1=tonumber(string.sub(payload,12,15) )
     print ("Received LED1 Fadetimer: " ..Fadetime1)
     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, 29) )

     Fadetime2=tonumber(string.sub(payload,12,15) )
     print ("Received LED1 Fadetimer: " ..Fadetime2)
     
     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.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 10.10.200.12 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 = '10.10.200.12'
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);
   os.execute(runcommand);
 return commandArray
   else
    DomValue = (otherdevices_svalues[DomDevice]);
   end
   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);
   os.execute(runcommand);
 end
return commandArray


7 comments:

  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.

    ReplyDelete
  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?

    ReplyDelete
    Replies
    1. 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.

      Delete
    2. Thank you for sharing the info, I'm sure it will help other people! :D

      Delete
  3. 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: https://drive.google.com/open?id=0B4vdNve__3S4U2pFeVF4cUNSS28
    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.

    ReplyDelete
    Replies
    1. 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!
      http://www.aliexpress.com/item/Free-shipping-50PCS-LOT-STP16NF06L-P16NF06L-MOSFET-N-CH-60V-16A-TO-220-Best-quality/2040225087.html

      Delete
    2. 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)?

      Delete