reset wifi data on button push, mqtt device config
[nodemcu-iot-mqtt-relay.git] / mqtt.lua
blob:a/mqtt.lua -> blob:b/mqtt.lua
--- a/mqtt.lua
+++ b/mqtt.lua
@@ -1,38 +1,30 @@
 -- mqtt.lua --
 
-prefix = "/IoTmanager"
-device = "/relay01"
+-- include MQTT config file
+dofile("mqtt-config.lua")
 
-mqttConfig = {}
-mqttConfig.host = '192.168.0.50'
-mqttConfig.port = '9443'
-mqttConfig.user = 'iot'
-mqttConfig.pass = 'iot'
-mqttConfig.secure = 1
-
-print ("Trying to connect to MQTT host " .. mqttConfig.host .. " on port " .. mqttConfig.port)
+print ("Trying to connect to MQTT host " .. mqttConfig.host .. ":" .. mqttConfig.port)
 
 local status1 = 1
-gpio12 = 6
+local gpio12 = 6
 gpio.mode(gpio12, gpio.OUTPUT)
---gpio.write(gpio12, gpio.LOW)
-gpio.write(gpio12, gpio.HIGH)
+--gpio.write(gpio12, gpio.LOW) -- OFF
+gpio.write(gpio12, gpio.HIGH) -- ON
 
 local status2 = 1
-gpio13 = 7
+local gpio13 = 7
 gpio.mode(gpio13, gpio.OUTPUT)
---gpio.write(gpio13, gpio.LOW)
-gpio.write(gpio13, gpio.HIGH)
+--gpio.write(gpio13, gpio.LOW) -- OFF
+gpio.write(gpio13, gpio.HIGH) -- ON
 
 -- initiate the mqtt client and set keepalive timer to 120sec
-
-mqtt = mqtt.Client(device, 120, mqttConfig.user, mqttConfig.pass, 1)
+local mqtt = mqtt.Client(device, 120, mqttConfig.user, mqttConfig.pass, 1)
 
 mqtt:on("connect", function(con)
-    print ("connected to MQTT " .. mqttConfig.host .. " broker\n")
+    print ("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
 end)
 mqtt:on("offline", function(con)
-    print ("\tdisconected from MQTT broker, reconnecting\n")
+    print ("\tDisconected from " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker, reconnecting\n")
 end)
 
 -- on receive message
@@ -50,23 +42,35 @@
             gpio.write(gpio12, gpio.LOW)
             status1 = 0
         end
+        -- publish status to be in sync with all devices connected
+        mqtt:publish(prefix .. device .. "/toggle1/status", "{\"status\":" .. status1 .. "}", 1, 1, function(conn)
+        end)
     elseif topic == prefix .. device .. "/toggle2/control" then
         if data == "1" then
-            print "Received message ON@Relay2"
+            print "\t\tReceived message ON@Relay2"
             gpio.write(gpio13, gpio.HIGH)
             status2 = 1
         elseif data == "0" then
-            print "Received message OFF@Relay2"
+            print "\t\tReceived message OFF@Relay2"
             gpio.write(gpio13, gpio.LOW)
             status2 = 0
         end
+        -- publish status to be in sync with all devices connected
+        mqtt:publish(prefix .. device .. "/toggle2/status", "{\"status\":" .. status2 .. "}", 1, 1, function(conn)
+        end)
     else
-        local id = 1
+        local list = {} 
+        local k = 0
+        for v in string.gmatch(topic, "([^/]+)") do
+            list[k] = v
+            k = k + 1
+        end
+        local id = list[1]
         msg = cjson.decode(data)
         print('\tNew request from IoT Manager: id="' .. id .. '", command="' .. msg.command .. '", param="' .. msg.param .. '"');
-        
+
         if msg.command == "getPages" then
-            mqtt:publish(prefix .. device .. "/response", '{"pages" : [{"pageId" : 10, "pageName" : "Page 1"}, {"pageId" : 20, "pageName" : "Page 2"}]}', 1, 1, function(conn)    
+            mqtt:publish(prefix .. device .. "/response", '{"pages" : [{"pageId" : 10, "pageName" : "Page 1"}, {"pageId" : 20, "pageName" : "Page 2"}]}', 1, 1, function(conn)
             end)
         elseif msg.command == "getPageById" then
             --if (msg.param == 10 or msg.param == 0) then
@@ -87,9 +91,9 @@
     end
 end)
 
-mqtt:connect(mqttConfig.host, mqttConfig.port, mqttConfig.secure, 1, function(conn) 
-    print ("connected to " .. mqttConfig.host  .. " MQTT broker\n")
-    -- subscribe topic with qos = 2
+mqtt:connect(mqttConfig.host, mqttConfig.port, mqttConfig.secure, 1, function(conn)
+    print ("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
+    -- subscribe topic with qos = 1
     mqtt:subscribe({[prefix .. "/+/+/control"]=1, [prefix .. "/+/request"]=1}, function(conn)
     end)
 end)

comments