local unescape = function (s) s = string.gsub(s, "+", " ") s = string.gsub(s, "%%(%x%x)", function (h) return string.char(tonumber(h, 16)) end) return s end function Sendfile(conn, filename) if file.open(filename, "r") then local function sendChunk() local line = file.read(512) if line then conn:send(line, sendChunk) else file.close() conn:close() collectgarbage() end end conn:send("HTTP/1.1 200 OK\r\n" .. -- "Server: NodeMCU on ESP8266\r\n" .. "Content-Type: text/html; charset=UTF-8\r\n\r\n", sendChunk) -- conn:send("HTTP/1.1 200 OK\r\n", sendChunk) else conn:send("HTTP/1.0 404 Not Found\r\n\r\nPage not found") conn:close() end end srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive", function(conn,request) 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+)=([^%&]+)&*") do _GET[k] = unescape(v) end end if (_GET.psw ~= nil and _GET.ap ~= nil and string.len(_GET.psw)>=8 and _GET.stnname ~= nil and _GET.stnpsw ~= nil) then conn:send("Saving configuration data...") file.open("config.lua", "w") file.writeline('stnname = "' .. _GET.stnname .. '"') print(_GET.stnname) file.writeline('stnpsw = "' .. _GET.stnpsw .. '"') print(_GET.stnpsw) file.writeline('ssid = "' .. _GET.ap .. '"') print(_GET.ap) file.writeline('password = "' .. _GET.psw .. '"') print(_GET.psw) if (_GET.ipaddr ~= nil and _GET.netmask ~= nil and _GET.gateway ~= nil and _GET.wsport ~= nil) then file.writeline('ipaddr = "' .. _GET.ipaddr .. '"') file.writeline('netmask = "' .. _GET.netmask .. '"') file.writeline('gateway = "' .. _GET.gateway .. '"') file.writeline('wsport = "' .. _GET.wsport .. '"') print(_GET.ipaddr) print(_GET.netmask) print(_GET.gateway) print(_GET.wsport) else file.writeline('wsport = 80') print("Webserver IP address and netmask and gateway and port NOT provided -- DHCP will be used") end if (_GET.tschanid ~= nil and _GET.tsapikey ~= nil) then file.writeline('tschanid = "' .. _GET.tschanid .. '"') print(_GET.tschanid) file.writeline('tsapikey = "' .. _GET.tsapikey .. '"') print(_GET.tsapikey) else print("ThingSpeak API write key NOT provided") end if (_GET.wuapiid ~= nil and _GET.wuapikey ~= nil) then file.writeline('wuapiid = "' .. _GET.wuapiid .. '"') print(_GET.wuapiid) file.writeline('wuapikey = "' .. _GET.wuapikey .. '"') print(_GET.wuapikey) else print("Weather Underground station ID and key NOT provided") end if (_GET.ubitoken ~= nil and _GET.ubidevid ~= nil) then file.writeline('ubitoken = "' .. _GET.ubitoken .. '"') print(_GET.ubitoken) file.writeline('ubidevid = "' .. _GET.ubidevid .. '"') print(_GET.ubidevid) else print("Ubidots token and device ID NOT provided") end file.close() file.open("config.lua", "r") print(file.read()) file.close() node.compile("config.lua") file.remove("config.lua") print("Rebooting with new SSID/PASSWORD...") node.restart() else print("AP and Wifi Password and Station Name and Station Password NOT provided...") end Sendfile(conn, 'main_ap.html') end) end)