rgbmatrixscreen/nodemcu/http/wifi_settings.lua
2021-04-29 21:46:54 -04:00

72 lines
2.1 KiB
Lua

return function (connection, req, args)
local staConfig = {ssid="", pwd=""}
-- Check for a wifi config file
if file.open("wificonfig.lua") then
file.close()
print('Found config.')
local res = pcall(dofile("wificonfig.lua"))
if (res) then
staConfig = dofile("wificonfig.lua")()
end
end
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n")
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Wifi Settings</title></head><body><h1>Wifi settings</h1><pre>')
connection:send("SSID: "..staConfig.ssid)
connection:send("\nPassword: "..staConfig.pwd)
connection:send("\nSTA IP: "..wifi.sta.getip())
connection:send("\nMAC: "..wifi.sta.getmac())
connection:send("\nstatus: "..wifi.sta.status())
connection:send([===[
Statuses:
0: IDLE
1: CONNECTING
2: WRONG_PASSWORD
3: NO AP FOUND
4: CONNECT FAIL
5: GOT IP/CONNECTED
]===])
connection:send('</pre><br />')
local form = [===[
<form method="POST">
<h2>Password and SSID must not contain quotes! " ' or `</h2>
SSID:<br><input type="text" name="ssid"><br>
Password:<br><input type="text" name="password"><br>
<input type="submit" value="Submit">
</form>
]===]
if req.method == "GET" then
connection:send(form)
elseif req.method == "POST" then
local rd = req.getRequestData()
-- connection:send(cjson.encode(rd))
connection:send('<h2>Received the following values:</h2>')
connection:send("<ul>\n")
if (rd.ssid) then
file.open("wificonfig.lua", "w+")
file.write('return function()\nreturn {ssid="' .. tostring(rd.ssid) .. '"')
if (rd.password) then
file.write(', pwd="' .. tostring(rd.password) .. '"')
end
file.write("}\nend")
file.close()
end
for name, value in pairs(rd) do
connection:send('<li><b>' .. name .. ':</b> ' .. tostring(value) .. "<br></li>\n")
end
connection:send("</ul>\n")
else
connection:send("NOT IMPLEMENTED")
end
connection:send('</body></html>')
end