-
Notifications
You must be signed in to change notification settings - Fork 13
/
autoconnect.lua
67 lines (58 loc) · 1.46 KB
/
autoconnect.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
-- choose the strongest open AP available and connect to it
-- tested with NodeMcu 0.9.2 build 20150123
Tstart = tmr.now()
ConnStatus = nil
function ConnStatus(n)
status = wifi.sta.status()
uart.write(0,' '..status)
local x = n+1
if (x < 50) and ( status < 5 ) then
tmr.alarm(0,100,0,function() ConnStatus(x) end)
else
if status == 5 then
print('\nConnected as '..wifi.sta.getip())
dofile('httpget.lua')
else
print("\nConnection failed")
end
end
end
best_ssid = nil
function best_ssid(ap_db)
local min = 100
ssid = nil
for k,v in pairs(ap_db) do
if tonumber(v) < min then
min = tonumber(v)
ssid = k
end
end
gpio.write(red,gpio.LOW)
gpio.write(blue,gpio.HIGH)
return min
end
strongest = nil
function strongest(aplist)
print("\nAvailable Open Access Points:\n")
for k,v in pairs(aplist) do print(k..' '..v) end
ap_db = {}
if next(aplist) then
for k,v in pairs(aplist) do
if '0' == string.sub(v,1,1) then
ap_db[k] = string.match(v, '-(%d+),')
end
end
signal = -best_ssid(ap_db)
end
if ssid then
print("\nBest SSID: ".. ssid)
wifi.sta.config(ssid,"")
print("\nConnecting to "..ssid)
ConnStatus(0)
else
print("\nNo available open APs")
ssid = ''
end
end
wifi.setmode(wifi.STATION)
wifi.sta.getap(function(t) strongest(t) end)