I made a few changes to the m:on(“offline”) function to handle failed Wifi connections better.
I had a few issues where my wifi router restarted, upon restarting (with the old sketch on the ESP07) once the wifi was back up, it reconnected to the Wifi though the magic of NodeMCU’s wifi.sta.autoconnect(1) functionality. However despite the Wifi reconnecting, the MQTT stopped working.
I added a new function called reconnect() to handle the dropped connection. It sets up a timer action to check when the Wifi is up again (Status = 5 and IP is not = null) then reruns the MQTT connect/subscribe:
function reconnect() print ("Waiting for Wifi") if wifi.sta.status() == 5 and wifi.sta.getip() ~= nil then print ("Wifi Up!") tmr.stop(1) m:connect(Broker, 1883, 0, function(conn) print("Mqtt Connected to:" .. Broker) mqtt_sub() --run the subscription function end) end end
Here’s the full sketch now
- Elegant reconnect after wifi fails
- 7 channels
- /dev1/ch1-7 mqtt names
-- One time ESP Setup -- wifi.setmode(wifi.STATION) wifi.sta.config ( "openhardwarecoza" , "novell1234" ) print(wifi.sta.getip()) Broker="192.168.1.200" --GPIO2 is connected to LED via resistor, initially off gpio.mode(1,gpio.OUTPUT) gpio.write(1,gpio.LOW) gpio.mode(2,gpio.OUTPUT) gpio.write(2,gpio.LOW) gpio.mode(3,gpio.OUTPUT) gpio.write(3,gpio.LOW) gpio.mode(4,gpio.OUTPUT) gpio.write(4,gpio.LOW) gpio.mode(5,gpio.OUTPUT) gpio.write(5,gpio.LOW) gpio.mode(6,gpio.OUTPUT) gpio.write(6,gpio.LOW) gpio.mode(7,gpio.OUTPUT) gpio.write(7,gpio.LOW) function reconnect() print ("Waiting for Wifi") if wifi.sta.status() == 5 and wifi.sta.getip() ~= nil then print ("Wifi Up!") tmr.stop(1) m:connect(Broker, 1883, 0, function(conn) print("Mqtt Connected to:" .. Broker) mqtt_sub() --run the subscripion function end) end end m = mqtt.Client("KITCHEN", 180, "user", "password") m:lwt("/lwt", "ESP8266", 0, 0) m:on("offline", function(con) print ("Mqtt Reconnecting...") tmr.alarm(1, 10000, 1, function() reconnect() end) end) -- on publish message receive event m:on("message", function(conn, topic, data) print("Recieved:" .. topic .. ":" .. data) if (data=="ON") and (topic=="/dev1/ch1/com") then print("Enabling Output") gpio.write(1,gpio.HIGH) m:publish("/dev1/ch1/state","ON",0,0) elseif (data=="OFF") and (topic=="/dev1/ch1/com") then print("Disabling Output") gpio.write(1,gpio.LOW) m:publish("/dev1/ch1/state","OFF",0,0) elseif (data=="ON") and (topic=="/dev1/ch2/com") then print("Enabling Output") gpio.write(2,gpio.HIGH) m:publish("/dev1/ch2/state","ON",0,0) elseif (data=="OFF") and (topic=="/dev1/ch2/com") then print("Disabling Output") gpio.write(2,gpio.LOW) m:publish("/dev1/ch2/state","OFF",0,0) elseif (data=="ON") and (topic=="/dev1/ch3/com") then print("Enabling Output") gpio.write(3,gpio.HIGH) m:publish("/dev1/ch3/state","ON",0,0) elseif (data=="OFF") and (topic=="/dev1/ch3/com") then print("Disabling Output") gpio.write(3,gpio.LOW) m:publish("/dev1/ch3/state","OFF",0,0) elseif (data=="ON") and (topic=="/dev1/ch4/com") then print("Enabling Output") gpio.write(4,gpio.HIGH) m:publish("/dev1/ch4/state","ON",0,0) elseif (data=="OFF") and (topic=="/dev1/ch4/com") then print("Disabling Output") gpio.write(4,gpio.LOW) m:publish("/dev1/ch4/state","OFF",0,0) elseif (data=="ON") and (topic=="/dev1/ch5/com") then print("Enabling Output") gpio.write(5,gpio.HIGH) m:publish("/dev1/ch5/state","ON",0,0) elseif (data=="OFF") and (topic=="/dev1/ch5/com") then print("Disabling Output") gpio.write(5,gpio.LOW) m:publish("/dev1/ch5/state","OFF",0,0) elseif (data=="ON") and (topic=="/dev1/ch6/com") then print("Enabling Output") gpio.write(6,gpio.HIGH) m:publish("/dev1/ch6/state","ON",0,0) elseif (data=="OFF") and (topic=="/dev1/ch6/com") then print("Disabling Output") gpio.write(6,gpio.LOW) m:publish("/dev1/ch6/state","OFF",0,0) elseif (data=="ON") and (topic=="/dev1/ch7/com") then print("Enabling Output") gpio.write(7,gpio.HIGH) m:publish("/dev1/ch7/state","ON",0,0) elseif (data=="OFF") and (topic=="/dev1/ch7/com") then print("Disabling Output") gpio.write(7,gpio.LOW) m:publish("/dev1/ch7/state","OFF",0,0) end end) function mqtt_sub() m:subscribe("/dev1/#",0, function(conn) print("Mqtt Subscribed to OpenHAB feed for device KITCHEN") end) end tmr.alarm(0, 1000, 1, function() print ("Connecting to Wifi... ") if wifi.sta.status() == 5 and wifi.sta.getip() ~= nil then print ("Wifi connected") tmr.stop(0) m:connect(Broker, 1883, 0, function(conn) print("Mqtt Connected to:" .. Broker) mqtt_sub() --run the subscription function end) end end)