optimizations
optimizations

file:a/init.lua -> file:b/init.lua
--- a/init.lua
+++ b/init.lua
@@ -1,10 +1,15 @@
 -- init.lua --
+
+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 "")))
+end
 
 -- Configure wifi via enduser setup
 enduser_setup.start(
     function()
         do
-        local def_sta_config=wifi.sta.getdefaultconfig(true)
+            tmr.stop(0)
             tmr.alarm(1, 3000, 1, function()
                 if wifi.sta.getip()==nil then
                     print("\tConnected to access point, obtaining IP address ...")
@@ -22,11 +27,6 @@
         print("enduser_setup: Err #" .. err .. ": " .. str)
     end
 );
-
-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 "")))
-end
 
 tmr.alarm(0, 3000, 1, function()
     if wifi.sta.getip()==nil then

file:a/mqtt.lua -> file:b/mqtt.lua
--- a/mqtt.lua
+++ b/mqtt.lua
@@ -1,9 +1,9 @@
 -- mqtt.lua --
 
-prefix = "/IoTmanager"
-device = "/relay01"
+local prefix = "/IoTmanager"
+local device = "/relay01"
 
-mqttConfig = {}
+local mqttConfig = {}
 mqttConfig.host = '192.168.0.50'
 mqttConfig.port = '9443'
 mqttConfig.user = 'iot'
@@ -13,20 +13,16 @@
 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)
-
-if mqtt ~= nil then
-    --mqtt:close()
-end
+--gpio.write(gpio13, gpio.LOW) -- OFF
+gpio.write(gpio13, gpio.HIGH) -- ON
 
 -- initiate the mqtt client and set keepalive timer to 120sec
 local mqtt = mqtt.Client(device, 120, mqttConfig.user, mqttConfig.pass, 1)
@@ -64,6 +60,7 @@
             status2 = 0
         end
     else
+        -- to parse client ID from topic
         local id = 1
         msg = cjson.decode(data)
         print('\tNew request from IoT Manager: id="' .. id .. '", command="' .. msg.command .. '", param="' .. msg.param .. '"');
@@ -72,13 +69,13 @@
             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
+            --if (msg.param == 10 or msg.param == 0) then
                 msg = '{"id":"1", "page": "Page 1", "pageId": 10, "widget":"toggle", "descr":"Relay 1", "topic":"' .. prefix .. device ..'/toggle1"}'
                 mqtt:publish(prefix .. device .. "/config", msg, 1, 1, function(conn)
                 end)
                 mqtt:publish(prefix .. device .. "/toggle1/status", "{\"status\":" .. status1 .. "}", 1, 1, function(conn)
                 end)
-            end
+            --end
             --if (msg.param == 20 or msg.param == 0) then
                 msg = '{"id":"2", "page": "Page 2", "pageId": 20, "widget":"toggle", "descr":"Relay 2", "topic":"' .. prefix .. device ..'/toggle2"}'
                 mqtt:publish(prefix .. device .. "/config", msg, 1, 1, function(conn)
@@ -92,7 +89,7 @@
 
 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 = 2
+    -- subscribe topic with qos = 1
     mqtt:subscribe({[prefix .. "/+/+/control"]=1, [prefix .. "/+/request"]=1}, function(conn)
     end)
 end)

comments