--TODO: configFile="config.lua" function sendWebPage(conn,answertype) collectgarbage() if (ssid == nil) then ssid="Not set" end if (sntpserverhostname == nil) then sntpserverhostname="ptbtime1.ptb.de" end if (timezoneoffset == nil) then timezoneoffset=1 end buf="HTTP/1.1 200 OK\nServer: NodeMCU\nContent-Type: text/html\n\n" if (node.heap() < 8000) then buf = buf .. "

Busy, please come later again

" else buf = buf .. "" buf = buf .. "WordClock Setup Page" buf = buf .. "\n" buf = buf .. "

Welcome to the WordClock

Please note that all settings are mandatory

" buf = buf .."
" buf = buf .."" buf = buf .."" buf = buf .."" buf = buf .."" buf = buf .."" buf = buf .. "" buf = buf .."
WIFI-SSIDSSID of the wireless network
WIFI-PasswordPassword of the wireless network
SNTP ServerServer to sync the time with. Only one ntp server is allowed.
Offset to UTC timeDefine the offset to UTC time in hours. For example +1 hour for Germany
" if answertype==2 then buf = buf .. "

New configuration saved" elseif answertype==3 then buf = buf .. "

ERROR" elseif answertype==4 then buf = buf .. "

Not all parameters set" end buf = buf .. "\n" end conn:send(buf) buf=nil collectgarbage() end function startWebServer() srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive", function(conn,payload) if (payload:find("GET /") ~= nil) then --here is code for handling http request from a web-browser ssid, password, bssid_set, bssid = wifi.sta.getconfig() sendWebPage(conn,1) conn:on("sent", function(conn) conn:close() end) else if (payload:find("POST /") ~=nil) then --code for handling the POST-request (updating settings) _, postdatastart = payload:find("\r\n\r\n") --Next lines catches POST-requests without POST-data.... if postdatastart==nil then postdatastart = 1 end postRequestData=string.sub(payload,postdatastart+1) local _POST = {} for i, j in string.gmatch(postRequestData, "(%w+)=([^&]+)&*") do _POST[i] = j end postRequestData=nil if ((_POST.ssid~=nil) and (_POST.password~=nil) and (_POST.sntpserver~=nil) and (_POST.timezoneoffset~=nil)) then -- Safe configuration: file.remove(configFile .. ".new") file.open(configFile.. ".new", "w") file.write("-- Config\n" .. "wifi.sta.config(\"" .. _POST.ssid .. "\",\"" .. _POST.password .. "\")\n" .. "sntpserverhostname=\"" .. _POST.sntpserver .. "\"\n" .. "timezoneoffset=\"" .. _POST.timezoneoffset .. "\"\n") file.close() file.remove(configFile) if (file.rename(configFile .. ".new", configFile)) then dofile(configFile) -- load the new values sendWebPage(conn,2) -- success else sendWebPage(conn,3) -- error end else ssid, password, bssid_set, bssid = wifi.sta.getconfig() sendWebPage(conn,4) -- not all parameter set end conn:on("sent", function(conn) conn:close() end) else --here is code, if the connection is not from a webbrowser, i.e. telnet or nc global_c=conn function s_output(str) if(global_c~=nil) then global_c:send(str) end end node.output(s_output, 0) global_c:on("receive",function(c,l) node.input(l) end) global_c:on("disconnection",function(c) node.output(nil) global_c=nil end) print("Welcome to Word Clock") end end end) conn:on("disconnection", function(c) node.output(nil) -- un-register the redirect output function, output goes to serial end) end) end