The last part of the ESP8266 WiFi LED dimmer project is getting it to work inside of my Domotica system of choice: Domoticz. We can do this using LUA scripts, in the end you will have an easy dimmer slider in Domoticz which you can use to control each channel of the ESP8266 WiFi LED dimmer.
This guide will not tell you how to install Domoticz or get it working, it will purely explain how to get the LED dimmer function inside of a working Domoticz installation.
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/
Create a Dummy Device
Fill in the name and select the type “Dummy” |
Dummy device has been added |
Create a new Switch
It’s the blue button on the top left |
When the dialog appears, select the dummy switch you created as the ‘Hardware’. Then type a name for the Light, best to use a description as where it is or what it is going to light. Be sure not to include spaces in the name. Then select that it’s a ‘Dimmer’ Switch type with type “LightWaveRF”.
Be sure to keep the switch type as LightWaveRF |
Once that is done, the switch should show up in the “Switches” window.
New dimmer switch on the bottom right |
Once that is done, click “edit” on the switch to take a look inside of it.
Inside of the switch, you do NOT have to fill in any action scripts |
Creating the LUA scripts
Domoticz script directory with some scripts |
The script we need to create in the “script_device_GPIO-0_channel.lua” file is the following:
commandArray = {}
DomDevice = 'GPIO-0_channel'
IP = '10.10.200.10'
Port = '43333'
LEDtarget = 'LED1_Target='
if devicechanged[DomDevice] then
if(devicechanged[DomDevice]=='Off') then DomValue = 0;
print ("Turning off " .. DomDevice);
runcommand = "echo " .. (LEDtarget) .. "0 | nc " .. (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 " .. (LEDtarget) .. "" .. (CalcValue) .. " | nc " .. (IP) .. " " .. (Port) .. " ";
print (runcommand);
os.execute(runcommand);
end
return commandArray
Updated the Domoticz code to handle turning off the dimmer in a script correctly. Also made it fully Dynamic with the variables at the beginning of the script.
Be sure to fill in the variables at the beginning of the script, no need to edit anything else in the script itself.
Domoticz gives you a 32 value Dimmer setting you can readout. We use this value to calculate to a maximum duty cycle setting the ESP8266 accepts for it’s PWM channels (max is 1023). Then we use netcat again to send the command to the ESP-01 we configured. I know I can use LUA native commands to send the commands to the device but since the default install of Domoticz does not come with the ‘sockets’ library I decided to do it this way for now. I will probably try it in the future to see if it’s faster or better in any way.
The way it works now is that even if the ESP-01 is dimming you can send it a new command and it will immediately change it’s dimming cycle towards that value. So even if you select 5 values quickly after each other it should end up at the latest setting 5 seconds after you made the setting in Domoticz.
And that’s it! Now you have a dimmer you can control and you LED strip should follow with dimming the light towards to value you choose.
The fading is controlled inside the LUA script running on the ESP-01 the default setting is to use 5 seconds as the time every dim will take. I like the soft fading of the lights but also value the constant 5 second rule.
Now it’s easy to expand from here. If you wish to use the second channel, create another switch, and use the same script but edit it to send LED2_target= and you’re done. The same thing for using multiple modules, every time you will need to create a new switch which you can then control in Domoticz.
I am try to pilot limtless rgb
so i need to send UDP command on port 8899.
The script work because i see the print on the log … but nothing on the lan
sorry this is the log
2015-04-01 21:52:47.648 LUA: Value received from Domoticz was 18
2015-04-01 21:52:47.648 LUA: Calculated value for ESP is 594
2015-04-01 21:52:47.648 LUA: Dimming testdimmer to 594
2015-04-01 21:52:47.648 LUA: echo LED1_Target=594 | nc 192.168.1.165 8899
2015-04-01 21:52:47.602 (Tutorial_Dimmer_Device) Lighting 5 (testdimmer)
and this is the script
commandArray = {}
DomDevice = 'testdimmer'
IP = '192.168.1.165'
Port = '8899'
LEDtarget = 'LED1_Target='
if devicechanged[DomDevice] then
if(devicechanged[DomDevice]=='Off') then DomValue = 0;
print ("Turning off " .. DomDevice);
runcommand = "echo " .. (LEDtarget) .. "0 | nc " .. (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 " .. (LEDtarget) .. "" .. (CalcValue) .. " | nc " .. (IP) .. " " .. (Port) .. " ";
print (runcommand);
os.execute(runcommand);
end
return commandArray
When I create a dummy device like you've described it, the lua script doesnt work. The script is partly working. When I debug it with extra print() functions in it, I can see it won't pass the first if-statement:
LEDtarget = 'LED1_Target='
print("start script")
if devicechanged[DomDevice] then
If I create a dummy device from type AC, it is working, yet only with the following if-statement in the script:
commandArray = {}
IP = '10.0.0.139'
Port = '19444'
DomValue = 0
DomDevice = 'MoodLight'
DomR = 255
DomG = 31
DomB = 0
for i, v in pairs(devicechanged) do
if (i == DomDevice) then
if(v == 'Off' ) then
DomValue = 0
elseif(v == 'On') then
DomValue = 16
else
DomValue = otherdevices_svalues[DomDevice]
end
DomR = math.ceil(DomValue / 16 * DomR)
DomG = math.ceil(DomValue / 16 * DomG)
DomB = math.ceil(DomValue / 16 * DomB)
runcommand = "echo '{ "color": [" .. DomR .. "," .. DomG .. "," .. DomB .. "], "command": "color", "priority": 1 }' | nc " .. (IP) .. " " .. (Port) .. " ";
print (runcommand);
os.execute(runcommand);
end
end
return commandArray
This script is only working in an AC type dummy device, and not in a LightWaveRX dummy device….
Got any idea?
Im using another runcommand as I have another RGB controller and use it to send also the color 😉
This statement is wrong in the latest version v2.3771.
"The first step we need to do is to create a Dummy Device under which we can list the new LED dimmer channels. You can add a device in the "setup" –> "devices" menu."
It should be Setup -> Hardware
Hi,
I've been following your path to sucess with this esp dimmer, and have managed to get everything working except for the Domoticz control- The log from Domoticz shows that the code is ok ( i pasted it into a Cygwin terminal and it worked perfectly) but it is not outputting to the esp from Domoticz . Is there some special setup for Domoticz I'm missing? Anything possibly to do with my device is on network 192.168.*.* and Domotics shows as being 127.0.0.1?
I've got this working with the GPIO0 but how do i control GPIO2 ? I've tried creating another device but it keeps controlling GPIO0. BTW great project, i'm trying to control two 10w LED floodlights.
This comment has been removed by the author.
I seem to have now got this sorted. I was getting errors when attempting to set the second LED so removed the script and device and started again. The only difference i did was naming the second script script_device_GPIO2-1_channel.lua this time. The only things i need to work on now are that just doing a power on does not work as it does not send what the slider is currently set to and also i've a hum from the lights also.