Site icon Intermittent Technology

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: https://blog.quindorian.org/2016/07/esp8266-lighting-revisit-and-history-of-quinled.html/


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
 

 

Exit mobile version