Current Lua downloadable firmware will be posted here

User avatar
By MaxItaly
#64670 This is the code of my AP webserver, fw master 2.0.0:

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)

connActive = 1

--show web server
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 = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end

buf = buf.."<!DOCTYPE HTML>"
buf = buf.."<html><head><meta http-equiv=refresh content=1></head>"
buf = buf.."<title>myAccessPoint</title>"
buf = buf.."<body><h1>myAccesPoint</h1>"
buf = buf.."<h2>"..myip.."</h2>"
buf = buf.."<p>Led ESP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href=\"?pin=OFF1\"><button>OFF</button></a></p>"
buf = buf.."<p>Led RIMS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href=\"?pin=OFF2\"><button>OFF</button></a></p>"
buf = buf.."<h2> Status Attuale</h2>"

local _on,_off = "",""

if(_GET.pin == "ON1")then
gpio.write(pinOnESP, gpio.LOW)
riga = "Led ESP ON"
elseif(_GET.pin == "OFF1")then
gpio.write(pinOnESP, gpio.HIGH)
riga = "Led ESP OFF"
elseif(_GET.pin == "ON2")then
gpio.write(pinOnRims, gpio.HIGH)
riga1 = "Led RIMS ON"
elseif(_GET.pin == "OFF2")then
gpio.write(pinOnRims, gpio.LOW)
riga1 = "Led RIMS OFF"
end

buf = buf..riga
buf = buf.."<h2> </h2>"
buf = buf..riga1
buf = buf.."<h2> </h2>"
buf = buf..string.format("Counter: %d", counter)
buf = buf.."</body></html>"

local function send(sck)
if #buf > 0 then
sck:send(buf)
buf = ""
else
sck:close()
collectgarbage();
--print("Heap2:" .. node.heap())
end
end

client:on("sent", send)
send(client)

end)

collectgarbage();
--print("Heap0:"..node.heap())
end)

The HTML page is refresh every 1 sec but after 50 time the sw crash with the msg "out of memory". The heap start from 30000 and decrease to 5000 before the crash. The function use local variable. Can help me ?
Thanks
User avatar
By marcelstoer
#64680 Your code is obviously based on our example at https://nodemcu.readthedocs.io/en/lates ... #example_6, that's good. However, you changed

Code: Select allif #response > 0 then
  localSocket:send(table.remove(response, 1))
else


to

Code: Select allif #buf > 0 then
  sck:send(buf)
  buf = ""
else


and that doesn't make sense IMO. Rather then "sending batches while buffer not empty" you send the entire buffer at once and clear it afterwards. That renders our nice callback-based approach moot.

The real issue though is this

MaxItaly wrote:buf = buf.."<html><head><meta http-equiv=refresh content=1></head>"
...
The HTML page is refresh every 1 sec


All the sockets remain in TIME_WAIT until cleared by the network library after some time. https://github.com/nodemcu/nodemcu-firmware/pull/1838 changed the config to reduce the time curing which they remain in TIME_WAIT. Try a firmware from the dev branch.
User avatar
By MaxItaly
#64690 OK i understood what you mean, but with the fw 0.9.6 works ! i prefer to use the new fw because it works at 115200 and support the dynamic timer.
I modifies the code with :
htmlpage[#htmlpage + 1] = "</body></html>\r\n"

local function send(sck)
if #htmlpage > 0 then
sck:send(table.remove(htmlpage, 1))
else
sck:close()
htmlpage = nil
print("Heap Available:" .. node.heap())
end
end

client:on("sent", send)
send(client)

but the time required to send the html page is about 5 sec, and therefore is not possible to refresh at 1 sec.
I'll use the fw 096, waiting the new fw good !
Thanks, Max ;)