Skip to content

Instantly share code, notes, and snippets.

@psolyca
Last active May 4, 2020 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psolyca/2500507e75ee43f78070179d7ef06a64 to your computer and use it in GitHub Desktop.
Save psolyca/2500507e75ee43f78070179d7ef06a64 to your computer and use it in GitHub Desktop.
Light control with NodeMCU v3 #nodemcu #esp8266

The goal is to control all my lights with Domoticz and my wall switches.

A NodeMCU v3 will be used as the controller, a PCF8574 board to extand I/O, a 4 relays board to switch lights on/off and a mini AC-DC converter.

ESPeasy is the firmware of the main board and Domoticz the home automation manager.

Home configuration

Lights in the home are simple or 3-way switches. By the way, all cables from live, neutral and between switches and lights are gathered in a connecting box as the following: wiring_present

The house is divided into two part, one for living rooms with 6 lights (2 are peers) and one for sleeping rooms with 7 independant lights. Thus there are two boxes.

The idea is to implement all the stuff in these boxes. The configuration will be the following one : wiring_futur

Hardware

As NodeMCU as few GPIO pin available, PCF8574A boards will be used to expand I/O ports. For living rooms, one board is mandatory whereas in sleeping rooms two boards are mandatory.

GPIOs 4 (D1) and 5 (D2) will be used as SDA and SCL.

Software

Lights have to :

  • be controlled by Domoticz,
  • be autonomous (manual switch in case of network lost) and report back the state of lights to Domoticz.

ESPeasy

Devices
Rules
Wireless switch
Manual switch

Domoticz

Device
Events

The rule is shared with the RollerShutter control.

commandArray = {}

local relaisPav = { ["entrée"] = 65, ["salon"] = 66, ["salle à manger / cuisine"] = 67 }
local relaisCha = { ["WC"] = 66, ["salle de bain"] = 68, ["couloir"] = 70, ["Jaouen"] = 72, ["Leonie"] = 74, ["parents"] = 76 }

-- Normalize a given string
function stripChars(str)
    local tableAccents = {}
        tableAccents["à"] = "a"
        tableAccents["ç"] = "c"
        tableAccents["è"] = "e"
        tableAccents["é"] = "e"
        tableAccents["ê"] = "e"
        tableAccents["ù"] = "u"
    local normalizedString = ''

    for strChar in string.gmatch(str, "([%z\1-\127\194-\244][\128-\191]*)") do
        if tableAccents[strChar] ~= nil then
            normalizedString = normalizedString..tableAccents[strChar]
        else
            normalizedString = normalizedString..strChar
        end
    end
 return normalizedString
end

for i, v in pairs(devicechanged) do
    --print("i : "..i)
    --print("v : "..tostring(v))
    if (i:sub(1,5) == 'Volet') then
        i=tostring(i)
        addr=i:gsub('Volet ','VR_')
        espURL = "http://"..addr.."/control?cmd="
        if otherdevices[i] == 'Open' then
            pourcentage = 0
        elseif otherdevices[i] == 'Closed' then
            pourcentage = 100
        else
            pourcentage = tonumber(otherdevices_svalues[i])
        end
        -- print("Valeur d'ouverture du volet :"..pourcentage)
        commandArray[1] = {['OpenURL'] = espURL..'event,move='..pourcentage}
    end

    if (stripChars(i):sub(1,7) == "Lumiere") then
            --print("i : "..stripChars(i))
            --print("v : "..tostring(v))
            if relaisPav[i:gsub('Lumière ','')] then
                commandArray[1] = {['OpenURL'] = 'http://Relais_PaV/control?cmd=event,Toggle='..relaisPav[i:gsub('Lumière ','')]}
            elseif relaisCha[i:gsub('Lumière ','')] then
                commandArray[1] = {['OpenURL'] = 'http://Relais_Cha/control?cmd=event,Toggle='..relaisCha[i:gsub('Lumière ','')]}
            end
    end
end

return commandArray
Switches
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment