I'm testing a little Lua web server that has an autoupdating-feature. The HTML code is redirecting to the web server itself every second. So the client's web browser always gets new data from the server instead of using the browser's cache.
If I connect with even only one client (my PC or my smartphone) after some time the NodeMCU-board crashes with this message:
PANIC: unprotected error in call to Lua API (SO-WebSrv-Test.lua:27: out of memory)
I used this code from Marcel Stoer who answered on a similar "running out of memory" question.
I modified Marcel's Lua code, but this code still eats up all heap memory over time.
I narrowed down the problem a little bit: if the refresh frequency of the HTML code is lower than 30 seconds the code eats up heap memory.
So how do I have to modify this code to achieve constant heap memory usage?
Best regards.
Stefan
tmr.alarm(0, 1000, 1, function()
if wifi.sta.getip() == nil then
print("trying to connect to AccessPoint...")
else
own_ip, netmask, gateway=wifi.sta.getip()
print("connected to AccessPoint:")
print("IP Info: \nIP Address of this device: ",own_ip)
print("Netmask: ",netmask)
print("Gateway Addr: ",gateway,"\n")
print("type IP-Address "..own_ip.." into your browser to display SHT-31-website")
tmr.stop(0)
end
end)
counter = 0
srv = net.createServer(net.TCP, 28800)
print("Server created... \n")
srv:listen(80, function(conn)
conn:on("receive", function(sck, request)
local message = {}
counter = counter + 1
message[#message + 1] = "<head> <meta http-equiv=refresh content=1; URL=http://"..own_ip.."> </head>"
message[#message + 1] = "<h1> ESP8266 SHT-31 Web Server Ver 003</h1>"
message[#message + 1] = "<h2>some more text blabla blub"..counter.."</h2>"
local function send(sk)
if #message > 0 then
sk:send(table.remove(message, 1))
else
sk:close()
message = nil
print("Heap Available:" .. node.heap())
end
end
sck:on("sent", send)
send(sck)
end)
end)