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!

Advertisements