Updated MQTT/ESP8266 sketch to automatically reconnect Wifi and MQTT if the Wifi went down

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)
Advertisements

OPENHAB, MQTT, ARDUINO AND ESP8266: PART 7: Expanding the ESP8266 sketch to handle 7 individual channels of MQTT on a ESP07/ESP12

I recently received some ESP07  modules, and if I keep GPIO0 for programming purposes, it leaves me with 7 GPIO lines!

So I updated the LUA script to accommodate 7 individual channels.

Note since my last posts the naming convention has been simplified to /dev1 (device 1) /dev1/ch1 (Channel1 on Device 1) – for sanity you may want to stick to the original…

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)

 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, 0, function() 
 m:connect(Broker, 1883, 0, function(conn) 
 print("Mqtt Connected to:" .. Broker) 
 mqtt_sub() --run the subscription function 
 end) 
 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() 
 if wifi.sta.status() == 5 and wifi.sta.getip() ~= nil then 
 tmr.stop(0) 
 m:connect(Broker, 1883, 0, function(conn) 
 print("Mqtt Connected to:" .. Broker) 
 mqtt_sub() --run the subscription function 
 end) 
 end 
 end)

Next I added the items to my openhab ITEMS file:

Switch ch1 "Channel 1" (all){mqtt=">[broker:/dev1/ch1/com:command:on:ON],>[broker:/dev1/ch1/com:command:off:OFF],<[broker:/dev1/ch1/state:state:default]"}

Switch ch2 "Channel 2" (all){mqtt=">[broker:/dev1/ch2/com:command:on:ON],>[broker:/dev1/ch2/com:command:off:OFF],<[broker:/dev1/ch2/state:state:default]"}

Switch ch3 "Channel 3" (all){mqtt=">[broker:/dev1/ch3/com:command:on:ON],>[broker:/dev1/ch3/com:command:off:OFF],<[broker:/dev1/ch3/state:state:default]"}

Switch ch4 "Channel 4" (all){mqtt=">[broker:/dev1/ch4/com:command:on:ON],>[broker:/dev1/ch4/com:command:off:OFF],<[broker:/dev1/ch4/state:state:default]"}

Switch ch5 "Channel 5" (all){mqtt=">[broker:/dev1/ch5/com:command:on:ON],>[broker:/dev1/ch5/com:command:off:OFF],<[broker:/dev1/ch5/state:state:default]"}

Switch ch6 "Channel 2" (all){mqtt=">[broker:/dev1/ch6/com:command:on:ON],>[broker:/dev1/ch6/com:command:off:OFF],<[broker:/dev1/ch6/state:state:default]"}

Switch ch7 "Channel 7" (all){mqtt=">[broker:/dev1/ch7/com:command:on:ON],>[broker:/dev1/ch7/com:command:off:OFF],<[broker:/dev1/ch7/state:state:default]"}

and to my sitemap for a quick test:

Switch item=ch1 label="Channel 1"
Switch item=ch2 label="Channel 2"
Switch item=ch3 label="Channel 3"
Switch item=ch4 label="Channel 4"
Switch item=ch5 label="Channel 5"
Switch item=ch6 label="Channel 6"
Switch item=ch7 label="Channel 7"

Time to upgrade my PCB layout to take advantage of switching 7 things from just one ESP07!

Till next time!  The folks at Wiznet.kr  just dropped off a W5500 Ethernet Shield for me to review/openhab/play with/abuse (Sponsored) –  Post coming soon!