moved config to config.lua, static IP option, optimizations
moved config to config.lua, static IP option, optimizations

file:b/config.lua (new)
--- /dev/null
+++ b/config.lua
@@ -1,1 +1,16 @@
+-- network
+network = {}
+network.ip = "192.168.0.51"
+network.netmask = "255.255.255.0"
+network.gateway = "192.168.0.1"
 
+-- MQTT
+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/init.lua -> file:b/init.lua
--- a/init.lua
+++ b/init.lua
@@ -1,19 +1,28 @@
 -- init.lua --
+
+dofile("config.lua")
 
 local def_sta_config=wifi.sta.getdefaultconfig(true)
 if def_sta_config.ssid ~= "" 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 "")))
+    wifi.sta.config(def_sta_config)
+    if network.ip ~= "" then
+        --wifi.sta.setip(network)
+    end
 else
     print("No wifi config found on flash. Turning on IoT Setup")
-    --wifi.setmode(wifi.STATIONAP)
-    --wifi.ap.config({ssid="IoTSetup_" .. wifi.sta.getmac(), auth=wifi.OPEN})
-    --enduser_setup.manual(true)
 end
 
 -- configure wifi via enduser setup
+--wifi.setmode(wifi.STATIONAP)
+--wifi.ap.config({ssid="IoTSetup_" .. wifi.sta.getmac(), auth=wifi.OPEN})
+--enduser_setup.manual(true)
 enduser_setup.start(
     function()
         do
+            if network.ip ~= "" then
+                --wifi.sta.setip(network)
+            end
             tmr.stop(0)
             tmr.alarm(1, 3000, 1, function()
                 if wifi.sta.getip()==nil then

file:a/mqtt-config.lua (deleted)
--- a/mqtt-config.lua
+++ /dev/null
@@ -1,12 +1,1 @@
--- 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,7 +1,4 @@
 -- mqtt.lua --
-
--- include MQTT config file
-dofile("mqtt-config.lua")
 
 print ("Trying to connect to MQTT host " .. mqttConfig.host .. ":" .. mqttConfig.port)
 
@@ -43,7 +40,7 @@
             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)
+        mqtt:publish(prefix .. device .. "/toggle1/status", "{\"status\":" .. status1 .. "}", 1, 0, function(conn)
         end)
     elseif topic == prefix .. device .. "/toggle2/control" then
         if data == "1" then
@@ -56,7 +53,7 @@
             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)
+        mqtt:publish(prefix .. device .. "/toggle2/status", "{\"status\":" .. status2 .. "}", 1, 0, function(conn)
         end)
     else
         local list = {}
@@ -70,23 +67,23 @@
         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, 0, 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)
+                mqtt:publish(prefix .. device .. "/config", msg, 1, 0, function(conn)
                 end)
-                mqtt:publish(prefix .. device .. "/toggle1/status", "{\"status\":" .. status1 .. "}", 1, 1, function(conn)
+                mqtt:publish(prefix .. device .. "/toggle1/status", "{\"status\":" .. status1 .. "}", 1, 0, function(conn)
                 end)
-            --end
-            --if (msg.param == 20 or msg.param == 0) then
+            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)
                 end)
-                mqtt:publish(prefix .. device .. "/toggle2/status", "{\"status\":" .. status2 .. "}", 1, 1, function(conn)
+                mqtt:publish(prefix .. device .. "/toggle2/status", "{\"status\":" .. status2 .. "}", 1, 0, function(conn)
                 end)
-            --end
+            end
         end
     end
 end)

comments