Skip to content

Instantly share code, notes, and snippets.

@arion
Created February 18, 2017 20:05
Show Gist options
  • Save arion/995afeba68e0709d08209ba0614d8d6f to your computer and use it in GitHub Desktop.
Save arion/995afeba68e0709d08209ba0614d8d6f to your computer and use it in GitHub Desktop.
--setwifi.lua
print("Entering wifi Setup..")
wifi.setmode(wifi.STATIONAP)
cfg={}
cfg.ssid="LeakSensor"
--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)
local function hex_to_char(x)
return string.char(tonumber(x, 16))
end
local function uri_decode(input)
return input:gsub("%+", " "):gsub("%%(%x%x)", hex_to_char)
end
local function resetConfig(ssid, password, data_url)
file.remove("config.lua")
file.open("config.lua","w+")
temp = "PASSWORD = \""..password.."\""
file.writeline(temp)
temp = "SSID = \""..ssid.."\""
file.writeline(temp)
temp = "DATA_URL = \""..data_url.."\""
file.writeline(temp)
file.flush()
temp = nil
file.close()
end
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 = {}
print("Request "..method.." on "..path)
if path == "/favicon.ico" then
conn:send("HTTP/1.1 404 file not found")
return
end
if (path == "/" and vars == nil) then
file.open("setwifi.html","r")
numboflinesinfile = 16
for counter=1, numboflinesinfile-1 do
client:send(file.readline());
end
file.close()
elseif (vars ~= nil) then
for k, v in string.gmatch(vars, '([^&=?]-)=([^&=?]+)') do
if k ~= nil then
print("param: "..k..":"..uri_decode(v))
_GET[k] = uri_decode(v)
end
end
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.data_url)then
data_url = _GET.data_url
ssid = _GET.dssid
password = ""
if (_GET.ssid) then
ssid = _GET.ssid
end
if (_GET.password) then
password = _GET.password
end
resetConfig(ssid, password, data_url)
tmr.alarm(0, 5000, 1, function()
node.restart()
end)
end
end
client:close();
collectgarbage();
end)
end)
@gittus
Copy link

gittus commented Jun 15, 2017

Hi, would it be poossible to get the other files too, as they needs to be also changed (e.g. init.lua to read config.lua)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment