optimizations
optimizations

file:a/init.lua -> file:b/init.lua
--- a/init.lua
+++ b/init.lua
@@ -1,8 +1,13 @@
 -- init.lua --
 
 local def_sta_config=wifi.sta.getdefaultconfig(true)
-if def_sta_config then
+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 "")))
+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
@@ -14,9 +19,11 @@
                 if wifi.sta.getip()==nil then
                     print("\tConnected to access point, obtaining IP address ...")
                 else
+                    def_sta_config=wifi.sta.getdefaultconfig(true)
                     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
+                    --enduser_setup.stop()
+                    -- run the main file
                     if file.exists("main.lc") then
                         dofile("main.lc")
                     else
@@ -34,6 +41,7 @@
 
 tmr.alarm(0, 3000, 1, function()
     if wifi.sta.getip()==nil then
+        def_sta_config=wifi.sta.getdefaultconfig(true)
         print("Connecting to access point " .. def_sta_config.ssid)
     else
         tmr.stop(0)
@@ -44,11 +52,7 @@
 pin = 3
 function pin3cb()
     print("Resetting wireless configuration and restarting")
-    station_cfg = {}
-    station_cfg.ssid = "1234567890"
-    station_cfg.pwd = "1234567890"
-    station_cfg.save = true
-    wifi.sta.config(station_cfg)
+    node.restore()
     node.restart()
 end
 gpio.trig(pin, "down", pin3cb)

comments