-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.lua
99 lines (88 loc) · 3.29 KB
/
setup.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
local module = {}
--[[
This module sets up a WiFi connection and launches the app as soon as it's ready
This code is derived from the NodeMCU Docs https://nodemcu.readthedocs.io/en/master/en/upload/#initlua
as well as the tutorial on https://www.foobarflies.io/a-simple-connected-object-with-nodemcu-and-mqtt/
]]
local function wifi_wait_ip()
if wifi.sta.getip()== nil then
print("IP unavailable, Waiting...")
else
tmr.stop(1)
print("\n====================================")
print("ESP8266 mode is: " .. wifi.getmode())
print("MAC address is: " .. wifi.ap.getmac())
print("IP is "..wifi.sta.getip())
print("====================================")
app.start()
end
end
local function wifi_start(list_aps)
if list_aps then
for key,value in pairs(list_aps) do
if config.SSID and config.SSID[key] then
wifi.setmode(wifi.STATION);
wifi.sta.config({ssid=key, pwd=config.SSID[key]})
-- wifi.sta.connect()
print("Connecting to " .. key .. " ...")
--config.SSID = nil -- can save memory
tmr.alarm(1, 2500, tmr.ALARM_AUTO, wifi_wait_ip)
end
end
else
print("Error getting AP list")
end
end
-- Define WiFi station event callbacks
local function wifi_connect_event(T)
print("Connection to AP("..T.SSID..") established!")
print("Waiting for IP address...")
if disconnect_ct ~= nil then disconnect_ct = nil end
end
local function wifi_got_ip_event(T)
-- Note: Having an IP address does not mean there is internet access!
-- Internet connectivity can be determined with net.dns.resolve().
print("Wifi connection is ready! IP address is: "..T.IP)
print("Startup will resume momentarily, you have 3 seconds to abort.")
print("Waiting...")
-- tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
end
local function wifi_disconnect_event(T)
if T.reason == wifi.eventmon.reason.ASSOC_LEAVE then
--the station has disassociated from a previously connected AP
return
end
-- total_tries: how many times the station will attempt to connect to the AP. Should consider AP reboot duration.
local total_tries = 75
print("\nWiFi connection to AP("..T.SSID..") has failed!")
--There are many possible disconnect reasons, the following iterates through
--the list and returns the string corresponding to the disconnect reason.
for key,val in pairs(wifi.eventmon.reason) do
if val == T.reason then
print("Disconnect reason: "..val.."("..key..")")
break
end
end
if disconnect_ct == nil then
disconnect_ct = 1
else
disconnect_ct = disconnect_ct + 1
end
if disconnect_ct < total_tries then
print("Retrying connection...(attempt "..(disconnect_ct+1).." of "..total_tries..")")
else
wifi.sta.disconnect()
print("Aborting connection to AP!")
disconnect_ct = nil
end
end
function module.start()
print("Configuring Wifi ...")
-- Register WiFi Station event callbacks
-- wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, wifi_connect_event)
-- wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, wifi_got_ip_event)
-- wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, wifi_disconnect_event)
wifi.setmode(wifi.STATION);
wifi.sta.getap(wifi_start);
end
return module