optimizations
optimizations

file:a/init.lua -> file:b/init.lua
-- init.lua -- -- init.lua --
   
local def_sta_config=wifi.sta.getdefaultconfig(true) 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 ""))) 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 end
   
-- configure wifi via enduser setup -- configure wifi via enduser setup
enduser_setup.start( enduser_setup.start(
function() function()
do do
tmr.stop(0) tmr.stop(0)
tmr.alarm(1, 3000, 1, function() tmr.alarm(1, 3000, 1, function()
if wifi.sta.getip()==nil then if wifi.sta.getip()==nil then
print("\tConnected to access point, obtaining IP address ...") print("\tConnected to access point, obtaining IP address ...")
else 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(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()) print('\tip: ', wifi.sta.getip())
-- Run the main file --enduser_setup.stop()
  -- run the main file
if file.exists("main.lc") then if file.exists("main.lc") then
dofile("main.lc") dofile("main.lc")
else else
dofile("main.lua") dofile("main.lua")
end end
tmr.stop(1) tmr.stop(1)
end end
end) end)
end end
end, end,
function(err, str) function(err, str)
print("enduser_setup: Err #" .. err .. ": " .. str) print("enduser_setup: Err #" .. err .. ": " .. str)
end end
); );
   
tmr.alarm(0, 3000, 1, function() tmr.alarm(0, 3000, 1, function()
if wifi.sta.getip()==nil then if wifi.sta.getip()==nil then
  def_sta_config=wifi.sta.getdefaultconfig(true)
print("Connecting to access point " .. def_sta_config.ssid) print("Connecting to access point " .. def_sta_config.ssid)
else else
tmr.stop(0) tmr.stop(0)
end end
end) end)
   
-- configure wifi reset button -- configure wifi reset button
pin = 3 pin = 3
function pin3cb() function pin3cb()
print("Resetting wireless configuration and restarting") print("Resetting wireless configuration and restarting")
station_cfg = {} node.restore()
station_cfg.ssid = "1234567890"  
station_cfg.pwd = "1234567890"  
station_cfg.save = true  
wifi.sta.config(station_cfg)  
node.restart() node.restart()
end end
gpio.trig(pin, "down", pin3cb) gpio.trig(pin, "down", pin3cb)
   
comments