Lua server fails to serve page at times on some browsers
Posted: Tue Apr 11, 2017 5:00 pm
90% of the time every thing works great, but 10% of the time I will connect to my AP and then attempt to navigate to 192.168.4.1. When I do this, the page won't be served and the browser states the address is unreachable. I then try connecting to the exact same AP from another device, without resetting the ESP8266, and am able to connect just fine.
I've tried multiple browsers, as well as disconnecting and reconnecting to the AP, and neither seem to help. This will happen one day, and then the next using the same code, it will connect again and load the page just fine. Any ideas?
I've tried multiple browsers, as well as disconnecting and reconnecting to the AP, and neither seem to help. This will happen one day, and then the next using the same code, it will connect again and load the page just fine. Any ideas?
Code: Select all
-- handle data receive from AP client
local function handleReceive(sock, data)
-- buffer request
clients[sock].rcvBuf = clients[sock].rcvBuf .. data
-- check for at least one \r\n\r\n
local k, l = string.find(clients[sock].rcvBuf, "\r\n\r\n")
if k then
-- parse resource
local resource = util.parseResource(clients[sock].rcvBuf)
if not resource then
sock:on("sent", function(s) closeClient(s) end)
sock:send("HTTP/1.0 404 Not Found\r\nCache-Control: no-store\r\nContent-Type: text/html\r\n\r\n<a href='"..wifi.ap.getip().."'>")
end
if resource == "" then resource = "index.htm" end
-- begin sending resource if available
if resource == "ajaxReq" then
handleAjax(sock)
else
clients[sock].file = file.open(resource)
if not clients[sock].file then
sock:on("sent", function(s) closeClient(s) end)
sock:send("HTTP/1.0 404 Not Found\r\nCache-Control: no-store\r\nContent-Type: text/html\r\n\r\n<a href='"..wifi.ap.getip().."'>")
return
end
clients[sock].readStarted = false
sock:on("sent", function(s) sendFile(s) end)
sock:send("HTTP/1.0 200 OK\r\nCache-Control: no-store\r\nContent-Type: text/html\r\n\r\n")
end
end
end
-- handle new client connection
local function handleConn(newSock)
clients[newSock] = {}
clients[newSock].rcvBuf= ""
newSock:on("disconnection", function(sock, err)
print('client disconn')
closeClient(sock)
end)
newSock:on("receive", handleReceive)
end
-- setup server and enter AP mode
function createSetupServer()
local srv=net.createServer(net.TCP)
srv:listen(80, handleConn)
-- configure wifi
wifi.eventmon.register(wifi.eventmon.AP_STADISCONNECTED, function() print("Dropped AP client") end)
wifi.setmode(wifi.STATIONAP, false);
wifi.ap.config({ssid="Sensive".. tostring(node.chipid()), pwd="12345678", auth=wifi.WPA2_PSK, save=false, beacon=100})
return srv
end