print('Setting up WIFI...') wifi.setmode(wifi.STATION) station_cfg={} station_cfg.ssid="YOUR SSID" station_cfg.pwd="YOUR PASSWORD" station_cfg.save=false wifi.sta.config(station_cfg) wifi.sta.connect() local HOMIE_DEVICE = "homie/ledcontrol"..node.chipid() local light_state = { alcovec = "0,0,0", alcovew = "0", alcoveww = "0", mainc = "0,0,0", mainw = "0", mainww = "0", shower = "0", kitchen = "0" } start_fade = false; fade_wait = false; m = mqtt.Client( "ledcontrol", 120, "MQTT USERNAME", "MQTT PASSWORD" ) m:lwt( "/lwt", "offline", 0, 0 ) m:on( "message", function( client, topic, data ) local topic_path = split( topic, "/" ) local homie_node = topic_path[3] if( light_state[ homie_node ] == nil ) then return end if( topic_path[5] == "set" ) then light_state[ homie_node ] = data if( homie_node == "alcovec" ) then local rgb = split( data, "," ) led_register( 1, math.floor( rgb[2] * 100/255 ) ) led_register( 3, math.floor( rgb[1] * 100/255 ) ) led_register( 5, math.floor( rgb[3] * 100/255 ) ) elseif( homie_node == "mainc" ) then local rgb = split( data, "," ) led_register( 2, math.floor( rgb[2] * 100/255 ) ) led_register( 4, math.floor( rgb[1] * 100/255 ) ) led_register( 6, math.floor( rgb[3] * 100/255 ) ) elseif( homie_node == "alcovew" ) then led_register( 9, data * 100 ) elseif( homie_node == "alcoveww" ) then led_register( 7, data * 100 ) elseif( homie_node == "mainw" ) then led_register( 10, data * 100 ) elseif( homie_node == "mainww" ) then led_register( 8, data * 100 ) elseif( homie_node == "shower" ) then led_register( 12, data * 100 ) elseif( homie_node == "kitchen" ) then led_register( 11, data * 100 ) elseif( homie_node == "switchlight1" ) then led_register( 13, data * 100 ) elseif( homie_node == "switchlight2" ) then led_register( 14, data * 100 ) elseif( homie_node == "switchlight3" ) then led_register( 15, data * 100 ) elseif( homie_node == "switchlight4" ) then led_register( 16, data * 100 ) end -- each time the set topic was called, wait 100ms to see if there -- are additional fade requests. fade_wait = true start_fade = true end end) -- check every 100 ms if there's a fade job -- only fade if there's no other job running already. tmr_fade = tmr.create() tmr_fade:register( 100, tmr.ALARM_AUTO, function( t ) if( fade_wait ) then fade_wait = false elseif( start_fade and not running() ) then start_fade = false fade_start() end end) tmr_mqtt = tmr.create() tmr_mqtt:register( 1000, tmr.ALARM_SINGLE, function( t ) register_homie() collectgarbage() t:register( 5000, tmr.ALARM_SINGLE, function( t ) dofile( "ledcontrol.lc" ) LEDCtl:init() collectgarbage() setup_inputs() m:publish( HOMIE_DEVICE.."/$state", "ready", 0, 1 ) -- lower CPU frequency to 80 MHz after setup -- runs more stable. node.setcpufreq( node.CPU80MHZ ) --node.egc.setmode(node.egc.ON_MEM_LIMIT, -6144) print( 'heap: ', node.heap() ) t:unregister() tmr_fade:start() end) t:start() end) tmr_reg = tmr.create() tmr_reg:register( 10000, tmr.ALARM_SINGLE, function( t ) m:connect( "MQTT SERVER IP", 1883, false, function(client) t:unregister() print( "Connected to MQTT broker" ) client:subscribe( HOMIE_DEVICE.."/#", 0, function( client ) print ( "Successfully subscribed to "..HOMIE_DEVICE.."/#" ) tmr_mqtt:start() end) end, function( client, reason ) print("failed reason: " .. reason) t:start() end) end) tmr_wifi = tmr.create() tmr_wifi:alarm( 1000, tmr.ALARM_AUTO, function( t ) if wifi.sta.getip() == nil then print('Waiting for IP ...') else net.dns.setdnsserver( '9.9.9.10', 0 ) print('IP is ' .. wifi.sta.getip()) t:unregister() tmr_reg:start(); end end) function register_homie() m:publish( HOMIE_DEVICE.."/$homie", "4.0", 0, 1 ) m:publish( HOMIE_DEVICE.."/$name", "LED Control", 0, 1 ) m:publish( HOMIE_DEVICE.."/$nodes", "alcovec,mainc,alcovew,alcoveww,mainw,mainww,shower,kitchen,switch1,switch2,switch3,switch4,switch5,switch6,switchlight1,switchlight2,switchlight3,switchlight4", 0, 1 ) m:publish( HOMIE_DEVICE.."/$extensions", "", 0, 1 ) m:publish( HOMIE_DEVICE.."/$state", "init", 0, 1 ) for homie_node, name in next, { alcovec = "Alcove RGB light", mainc = "Main RGB light" } do m:publish( HOMIE_DEVICE.."/"..homie_node.."/$name", name, 0, 1 ) m:publish( HOMIE_DEVICE.."/"..homie_node.."/$type", "RGB light", 0, 1 ) m:publish( HOMIE_DEVICE.."/"..homie_node.."/$properties", "color", 0, 1 ) m:publish( HOMIE_DEVICE.."/"..homie_node.."/color/$name", "Color "..name, 0, 1 ) m:publish( HOMIE_DEVICE.."/"..homie_node.."/color/$datatype", "color", 0, 1 ) m:publish( HOMIE_DEVICE.."/"..homie_node.."/color/$format", "rgb", 0, 1 ) m:publish( HOMIE_DEVICE.."/"..homie_node.."/color/$settable", "true", 0, 1 ) end for homie_node, name in next, { alcovew = "Alcove white light", alcoveww = "Alcove warm white light", mainw = "Main white light", mainww = "Main warm white light", shower = "Shower light", kitchen = "Kitchen light", switchlight1 = "Switch light 1", switchlight2 = "Switch light 2", switchlight3 = "Switch light 3", switchlight4 = "Switch light 4" } do m:publish( HOMIE_DEVICE.."/"..homie_node.."/$name", name, 0, 1 ) m:publish( HOMIE_DEVICE.."/"..homie_node.."/$type", "White light", 0, 1 ) m:publish( HOMIE_DEVICE.."/"..homie_node.."/$properties", "dimmer", 0, 1 ) m:publish( HOMIE_DEVICE.."/"..homie_node.."/dimmer/$name", "Brightness "..name, 0, 1 ) m:publish( HOMIE_DEVICE.."/"..homie_node.."/dimmer/$datatype", "float", 0, 1 ) m:publish( HOMIE_DEVICE.."/"..homie_node.."/dimmer/$format", "0:1", 0, 1 ) m:publish( HOMIE_DEVICE.."/"..homie_node.."/dimmer/$settable", "true", 0, 1 ) end for i = 1, 6, 1 do m:publish( HOMIE_DEVICE.."/switch"..i.."/$name", "Switch "..i, 0, 1 ) m:publish( HOMIE_DEVICE.."/switch"..i.."/$type", "Light switch "..i, 0, 1 ) m:publish( HOMIE_DEVICE.."/switch"..i.."/$properties", "state", 0, 1 ) m:publish( HOMIE_DEVICE.."/switch"..i.."/state/$name", "State "..i, 0, 1 ) m:publish( HOMIE_DEVICE.."/switch"..i.."/state/$datatype", "enum", 0, 1 ) m:publish( HOMIE_DEVICE.."/switch"..i.."/state/$settable", "false", 0, 1 ) m:publish( HOMIE_DEVICE.."/switch"..i.."/state/$format", "OFF,ON", 0, 1 ) end end function split( s, delimiter ) result = {}; for match in (s..delimiter):gmatch("(.-)"..delimiter) do table.insert(result, match); end return result; end function setup_inputs() -- New Board uses GPIO: GPIO 14, 12, 13, 9, 10, 2 -- Rev 1 using GPIO 4, 5, 9, 10, 12, 13 -- NodeMCU: 2, 1, 11, 12, 6, 7 local inputs = { 2, 1, 11, 12, 6, 7 } for pin = 1, 6, 1 do print( "Pin "..pin.." GPIO "..inputs[ pin ] ) gpio.mode( inputs[ pin ], gpio.INT ) -- gpio.mode( inputs[ pin ], gpio.INT, gpio.PULLUP ) gpio.trig( inputs[ pin ], "both", function( level, when, count ) local action = ( level == gpio.HIGH ) and "OFF" or "ON" --print( '{ "switch-event": { "switch": '..pin..', "action": "'..action..'" } }' ) m:publish( HOMIE_DEVICE.."/switch"..pin.."/state", action, 0, 0 ) end) end end