forked from eHomeDIY/WT02-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi.lua
76 lines (62 loc) · 1.61 KB
/
wifi.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
local was_in_setup = false
local function wifi_start_app()
doifexists(cfg.main_script or 'main.lua')
end
local function wifi_connected()
log('wifi connected')
if was_in_setup then
tmr.alarm(0, 10000, 0, function()
wifi.setmode(wifi.STATION)
wifi_start_app()
end)
else
wifi_start_app()
end
end
local function wifi_setup()
log('wifi setup started')
enduser_setup.start(function()
log('wifi setup successful')
was_in_setup = true
wifi_check()
end, function(err, str)
log('enduser_setup: Err #' .. err .. ': ' .. str)
end)
end
local function wifi_hasip()
return wifi.sta.getip() ~= nil
end
function wifi_check()
local count = 0
log('wifi checking')
if wifi_hasip() then
wifi_connected()
else
tmr.alarm(0, cfg.wifi_check_interval or 1000, 1, function()
log('wifi check', count)
if wifi_hasip() then
tmr.stop(0)
log('wifi has ip')
wifi_connected()
return
end
if cfg.wifi_check_count ~= 0 and count >= (cfg.wifi_check_count or 60) then
tmr.stop(0)
log('wifi timeout waiting')
wifi_setup()
return
end
count = count + 1
end)
end
end
local function wifi_init()
if wifi.getmode() == wifi.SOFTAP then
log('wifi not set up')
wifi_setup()
else
log('wifi set up')
wifi_check()
end
end
wifi_init()