MQTT config in external file, parsed client ID, indenting
MQTT config in external file, parsed client ID, indenting

file:a/init.lua -> file:b/init.lua
--- a/init.lua
+++ b/init.lua
@@ -2,10 +2,10 @@
 
 local def_sta_config=wifi.sta.getdefaultconfig(true)
 if def_sta_config then
-  print(string.format("Found wifi config data in flash\n\tssid:\"%s\"\tpassword:\"%s\"%s", def_sta_config.ssid, def_sta_config.pwd, (type(def_sta_config.bssid)=="string" and "\tbssid:\""..def_sta_config.bssid.."\"" or "")))
+    print(string.format("Found wifi config data in flash\n\tssid:\"%s\"\tpassword:\"%s\"%s", def_sta_config.ssid, def_sta_config.pwd, (type(def_sta_config.bssid)=="string" and "\tbssid:\""..def_sta_config.bssid.."\"" or "")))
 end
 
--- Configure wifi via enduser setup
+-- configure wifi via enduser setup
 enduser_setup.start(
     function()
         do
@@ -40,7 +40,7 @@
     end
 end)
 
--- Configure reset button
+-- configure wifi reset button
 pin = 3
 function pin3cb()
     print("Resetting wireless configuration and restarting")

file:a/main.lua -> file:b/main.lua
--- a/main.lua
+++ b/main.lua
@@ -1,6 +1,6 @@
 -- main.lua --
 
--- Run the mqtt file
+-- run the MQTT file
 if file.exists("mqtt.lc") then
     dofile("mqtt.lc")
 else

file:b/mqtt-config.lua (new)
--- /dev/null
+++ b/mqtt-config.lua
@@ -1,1 +1,12 @@
+-- mqtt-config.lua --
 
+prefix = "/IoTmanager"
+device = "/" .. wifi.sta.getmac() -- set the device as the wifi card mac address. Is unique, also easy to trace
+
+mqttConfig = {}
+mqttConfig.host = '192.168.0.50'
+mqttConfig.port = '9443'
+mqttConfig.user = 'iot'
+mqttConfig.pass = 'iot'
+mqttConfig.secure = 1
+

file:a/mqtt.lua -> file:b/mqtt.lua
--- a/mqtt.lua
+++ b/mqtt.lua
@@ -1,14 +1,7 @@
 -- mqtt.lua --
 
-local prefix = "/IoTmanager"
-local device = "/relay01"
-
-local mqttConfig = {}
-mqttConfig.host = '192.168.0.50'
-mqttConfig.port = '9443'
-mqttConfig.user = 'iot'
-mqttConfig.pass = 'iot'
-mqttConfig.secure = 1
+-- include MQTT config file
+dofile("mqtt-config.lua")
 
 print ("Trying to connect to MQTT host " .. mqttConfig.host .. ":" .. mqttConfig.port)
 
@@ -49,6 +42,9 @@
             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 "\t\tReceived message ON@Relay2"
@@ -59,14 +55,22 @@
             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
-        -- to parse client ID from topic
-        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,7 +91,7 @@
     end
 end)
 
-mqtt:connect(mqttConfig.host, mqttConfig.port, mqttConfig.secure, 1, function(conn) 
+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)

comments