ESP8266 WiFi LED dimmer Part 4 of X: Configuring Domoticz

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.

domoticzdimmers (1)



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

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. 
 
For the purposes of this blog post I have named mine “Tutorial_Dimmer_Device”.
 
adddummy (1)
Fill in the name and select the type “Dummy”


dummyadded
Dummy device has been added


Create a new Switch

We will create a new switch which we will map to the first channel (GPIO-0) of the ESP-01.
 
To do this go to the “Switches” menu and wile in the switches screen click “Manual Light/Switch”. 
 
createmanual
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”.

 
The “LightWaveRF” type makes it a certain type of dimmer which we then catch the output of in our scripts. The ID can be anything as long as you keep each switch as an individual number.
 
createmanualdialog
Be sure to keep the switch type as LightWaveRF

Once that is done, the switch should show up in the “Switches” window.


createmanualcreated
New dimmer switch on the bottom right
 

Once that is done, click “edit” on the switch to take a look inside of it.

insidenewswitch
Inside of the switch, you do NOT have to fill in any action scripts
 


Creating the LUA scripts

Next we need to login to the command prompt of your Domoticz machine. In my case it’s running on a Ubuntu 14.04 LTS Virtual Machine (ESXi 5.5). If you are running it on Windows you will need to make some little changes, this tutorial is written for a Linux hosted Domoticz.
 
Go to your Domoticz script directory. In that directory should be a LUA directory. Go into that and create a new file name called “script_device_GPIO-0_channel.lua”. This makes it clear to Domoticz that the device “GPIO-0_channel” has a script attached to it and it should be called when anything changes.
 
scriptdirectory
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

 

 

–update 2015-01-03
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.

p.s. Currently Domoticz still behaves a bit weird while dimming. This has nothing to do with my implenation (I believe) or the ESP-01 (for sure) but just how it behaves. This has something do with turning a device off being different then setting it to 0%. And also not being able to set your device to 0% in a scene. I’m going to test a bit more with it and hopefully I can find out what is going wrong or can request a fix for this! Right now there is no automatic way to get the dimmer to completely turn off. Turning off the switch yourself does turn it off completely as you would expect.

9 thoughts on “ESP8266 WiFi LED dimmer Part 4 of X: Configuring Domoticz”

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

  2. 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

  3. 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?

  4. 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

  5. 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?

  6. 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.

  7. 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.

Leave a Reply

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