optimizations
optimizations

file:a/config.lua -> file:b/config.lua
--- a/config.lua
+++ b/config.lua
@@ -19,7 +19,7 @@
 mqttConfig.port = ''
 mqttConfig.user = ''
 mqttConfig.pass = ''
-mqttConfig.secure = 0
+mqttConfig.secure = false
 mqttConfig.connected = false
 
 sData = {}
@@ -31,6 +31,7 @@
 sData.altitude = 0
 sData.pageName = "Comana weather station"
 sData.pageId = 50
+sData.pageOrder = 100
 sData.widget = "data"
 sData.title = "Summer kitchen temperature"
 sData.topic = prefix .. device ..'/ws1'

file:a/func.lua -> file:b/func.lua
--- a/func.lua
+++ b/func.lua
@@ -24,8 +24,9 @@
     return string.format("%02d/%02d/%04d %02d:%02d",  tm["day"], tm["mon"], tm["year"], tm["hour"], tm["min"])
 end
 
-function lcdPrint(txt)
-    if serialDebug then print(txt) end
+function lcdPrint(txt, txt1)
+    txt1 = txt1 or ''
+    if serialDebug then print(txt, txt1) end
     disp:setColor(255, 0, 0)
     disp:drawBox(0, 210, 320, 20)
     disp:setPrintPos(2, 225)
@@ -124,6 +125,8 @@
     end
 
     if mqttConfig.connected == true then
+        sData.value = sData.temperature
+        sData.valueappend = " C"
         mqtt:publish(sData.topic .. "/config", sjson.encode(sData), 1, 0, function(conn)
         end)
     end

file:a/mqtt.lua -> file:b/mqtt.lua
--- a/mqtt.lua
+++ b/mqtt.lua
@@ -15,12 +15,21 @@
 
 -- initiate the mqtt client and set keepalive timer to 120sec
 if mqttConfig.enabled == true then
-    mqtt = mqtt.Client(device, 120, mqttConfig.user, mqttConfig.pass, 1)
 
-    mqtt:on("connect", function(con)
+    function onConnect()
         lcdPrint("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
+        mqttConfig.connected = true
+        -- subscribe topic with qos = 1
+        mqtt:subscribe({[prefix .. "/+/+/data"]=1, [prefix .. "/device"]=1}, function(conn)
+        end)
+    end
+
+    mqtt = mqtt.Client(device, 120, mqttConfig.user, mqttConfig.pass, true)
+
+    mqtt:on("connect", function(conn)
+        onConnect()
     end)
-    mqtt:on("offline", function(con)
+    mqtt:on("offline", function(conn)
         lcdPrint("\tDisconected from " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker, reconnecting\n")
         mqttConfig.connected = false
     end)
@@ -43,18 +52,19 @@
                 rtctime.set(json.time + timezoneHours * 60 * 60, 0)
                 update()
             end
-            mqtt:publish(prefix .. device .. "/device", '{"pages" : [{"pageId" : 50, "pageName" : "Weather stations", "icon": "fa fa-thermometer-quarter"}]}', 1, 0, function(conn)
+            mqtt:publish(prefix .. device .. "/device", '{"pages" : [{"id" : 50, "name" : "Weather stations", "icon": "fa fa-thermometer-quarter", "order": "1000"}]}', 1, 0, function(conn)
             end)
             update()
         end
     end)
 
     mqtt:connect(mqttConfig.host, mqttConfig.port, mqttConfig.secure, function(conn)
-        lcdPrint("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
-        mqttConfig.connected = true
-        -- subscribe topic with qos = 1
-        mqtt:subscribe({[prefix .. "/+/+/data"]=1, [prefix .. "/device"]=1}, function(conn)
-        end)
+            onConnect()
+        end,
+        function(conn, reason)
+            print("\tCould not connect to MQTT server")
+            print("\tClient ", conn)
+            print("\tReason ", reason)
     end)
 end
 

comments