-- WiFi mode -- One of: 1 = STATION, 2 = SOFTAP, 3 = STATIONAP, 4 = NULLMODE print("WiFi Mode: ", wlanmode) local function config_sta() wifi.sta.on("connected", function() print("sta connected") end) wifi.sta.on("got_ip", function(event, info) print("sta got ip "..info.ip) localstaip = info.ip failure201 = 0 end) wifi.sta.config({ssid=sta_ssid, pwd=sta_pwd, auto=true}, true) if (sta_hostname ~= nil and sta_hostname ~= '') then wifi.sta.sethostname(sta_hostname) end end local function config_ap() wifi.ap.on("start") wifi.ap.on("sta_connected", function(event, info) print("Station connected: "..info.mac ) end) wifi.ap.config({ssid=ap_ssid, pwd=ap_pwd, channel=ap_channel}) wifi.ap.setip({ip=ap_ip, netmask=ap_netmask, gateway=ap_gateway, dns=ap_dns}) if (ap_hostname ~= nil and ap_hostname ~= '') then wifi.ap.sethostname(ap_hostname) end end if wlanmode == 1 then wifi.mode(wifi.STATION, true) end if wlanmode == 2 then wifi.mode(wifi.SOFTAP, true) end if wlanmode == 3 then wifi.mode(wifi.STATIONAP, true) end wifi.start() if wlanmode == 1 or wlanmode == 3 then config_sta() end if wlanmode == 2 or wlanmode == 3 then config_ap() end if wlanmode == 4 then --config_ap(nil) print("Wifi is disabled") end -- Handle the situation when we can not connect to the local WiFi hotspot. -- Turn off all station interfaces and start local WiFi hotspont. Disable mqtt. failure201 = 0 wifi.sta.on("disconnected", function(ev, info) print("SSID:", info.ssid, "BSSID:", info.bssid, "Reason:", info.reason) if info.reason == 201 then print ("FOR SSID:", info.ssid,"NO_AP_FOUND") failure201 = failure201 + 1 print(info.ssid, "disconnected, reason code:", info.reason, "Number of failed connection attempts:", failure201) if failure201 >= 20 then print("Disabling station mode WiFi, turing on default AP configuration.") --mqtttimer:stop() mqtt_enabled=false nextreboot=60 if wlanmode == 1 or wlanmode == 3 then wlanmode = 2 wifi.sta.disconnect() wifi.stop() wifi.mode(wifi.SOFTAP, true) config_ap() wifi.start() end failure201 = 0 end end end) uplinktimer = tmr.create() uplinktimer:register(10000, tmr.ALARM_SINGLE, function() print("Starting NTP service") time.initntp() end) uplinktimer:start()