-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetwifi.lua
87 lines (76 loc) · 3.08 KB
/
setwifi.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
--setwifi.lua
print("Opening Sup Wifi Setup..")
wifi.setmode(wifi.STATIONAP)
cfg={}
cfg.ssid="SimpleIOThings Sensor Setup"
--cfg.password="12345678" --comment to leave open
wifi.ap.config(cfg)
ipcfg={}
ipcfg.ip="192.168.1.1"
ipcfg.netmask="255.255.255.0"
ipcfg.gateway="192.168.1.1"
wifi.ap.setip(ipcfg)
ap_list = ""
function listap(t)
for bssid,v in pairs(t) do
local ssid, rssi, authmode, channel = string.match(v, "([^,]+),([^,]+),([^,]+),([^,]+)")
ap_list = ap_list.."<option value='"..ssid.."'>"..ssid.."</option>"
end
end
wifi.sta.getap(1, listap)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local buf = "";
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
if path == "/favicon.ico" then
conn:send("HTTP/1.1 404 file not found")
return
end
if (path == "/" and vars == nil) then
buf = buf.."<html><body style='width:90%;margin-left:auto;margin-right:auto;background-color:White;'>";
buf = buf.."<h1>Sup Wifi Configuration</h1>"
buf = buf.."<form action='' method='get'>"
buf = buf.."<h4>SSID:</h4>"
buf = buf.."<select name='dssid'>"..ap_list.."</select>"
buf = buf.." or <input type='text' name='ssid' value='' maxlength='100' width='100px' placeholder='ssid' />"
buf = buf.."<br><br>"
buf = buf.."<h4>Password:</h4>"
buf = buf.."<input type='text' name='password' value='' maxlength='100' width='100px' placeholder='empty if AP is open' />"
buf = buf.."<p><input type='submit' value='Submit' style='height: 25px; width: 100px;'/></p>"
buf = buf.."</body></html>"
elseif (vars ~= nil) then
restarting = "<html><body style='width:90%;margin-left:auto;margin-right:auto;background-color:LightGray;'><h1>Restarting...You may close this window.</h1></body></html>"
client:send(restarting);
client:close();
if(_GET.dssid)then
ssid = _GET.dssid
password = ""
if (_GET.ssid) then
ssid = _GET.ssid
end
if (_GET.password) then
password = _GET.password
end
print("Setting to: "..ssid..":"..password)
tmr.alarm(0, 5000, 1, function()
wifi.setmode(wifi.STATION);
wifi.sta.config(ssid,password);
node.restart()
end)
end
end
client:send(buf);
client:close();
collectgarbage();
end)
end)