serial debug improvements
serial debug improvements

file:a/init.lua -> file:b/init.lua
--- a/init.lua
+++ b/init.lua
@@ -7,10 +7,9 @@
         local def_sta_config=wifi.sta.getdefaultconfig(true)
             tmr.alarm(1, 3000, 1, function()
                 if wifi.sta.getip()==nil then
-                    print("\tconnected to access point, obtaining IP address ...")
+                    print("\tConnected to access point, obtaining IP address ...")
                 else
-                    -- TODO: remove password
-                    print(string.format("\tconnected to access point ssid:\"%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("\tConnected to access point ssid:\"%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('\tip: ', wifi.sta.getip())
                     -- Run the main file
                     dofile("main.lua")
@@ -24,14 +23,20 @@
     end
 );
 
-tmr.alarm(0, 3000, 1, function() 
+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
-        print("connecting to access point ...")
+        print("Connecting to access point " .. def_sta_config.ssid)
     else
         tmr.stop(0)
     end
 end)
 
+-- Configure reset button
 pin = 3
 function pin3cb()
     print("Resetting wireless configuration and restarting")
@@ -43,3 +48,4 @@
     node.restart()
 end
 gpio.trig(pin, "down", pin3cb)
+

file:a/mqtt.lua -> file:b/mqtt.lua
--- a/mqtt.lua
+++ b/mqtt.lua
@@ -10,7 +10,7 @@
 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
@@ -24,15 +24,18 @@
 --gpio.write(gpio13, gpio.LOW)
 gpio.write(gpio13, gpio.HIGH)
 
+if mqtt ~= nil then
+    --mqtt:close()
+end
+
 -- 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
@@ -52,11 +55,11 @@
         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
@@ -69,13 +72,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)
@@ -88,7 +91,7 @@
 end)
 
 mqtt:connect(mqttConfig.host, mqttConfig.port, mqttConfig.secure, 1, function(conn) 
-    print ("connected to " .. mqttConfig.host  .. " MQTT broker\n")
+    print ("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
     -- subscribe topic with qos = 2
     mqtt:subscribe({[prefix .. "/+/+/control"]=1, [prefix .. "/+/request"]=1}, function(conn)
     end)

comments