I am trying to toggle an LED using ESP8266-01 by sending POST from my laptop (using node.js)
I now have a memory issue because whenever I send POST request, the memo
So the solution from Marcel worked.
Here is just another option to solve the problem:
print("Starting main.lua... \n")
gpio.mode(3, gpio.OUTPUT)
srv=net.createServer(net.TCP,28800)
print("Server created... \n")
srv:listen(80,function(conn)
conn:on("receive", function(conn,request)
local _,_,method,path= string.find(request, "([A-Z]+) (.+)?(.+) HTTP")
local _,_,key,light_level = string.find(request, "(%a+)%s*:%s*(%d+)")
if(method == nil)then
_,_,method,path = string.find(request, "([A-Z]+) (.+) HTTP")
end
local duty=light_level*1023/100
pwm.setup(3, 500, duty)
local message={}
print("Level:"..light_level)
if(method == "POST")then --light_level was sent from node.js as the header of the request
if(duty>0)then
pwm.start(3)
message = {"HTTP/1.0 200 OK\r\n Content-Type: text/html\r\n\r\n"}
message[#message + 1] = (light_level/100)
elseif(duty==0)then
pwm.stop(3)
message = {"HTTP/1.0 200 OK\r\n Content-Type: text/html\r\n\r\n"}
message[#message + 1] = 0
end
elseif(method == "GET")then
message[#message + 1] = "HTTP/1.1 200 OK\r\n Content-Type: text/html\r\n\r\n"
message[#message + 1] = "LED STATE="..tostring(pinState).."\r\n"
end
local function send()
if #message > 0 then
conn:send(table.remove(message, 1))
else
conn:close()
conn = nil
collectgarbage()
end
end
conn:on("sent", send)
send()
local message = nil
local _,_,method,path = nil
local _,_,key,light_level = nil
local duty=nil
--for debugging
local heapSize=node.heap()
if heapSize<2000 then
node.restart()
end
print("Memory Used:"..collectgarbage("count"))
print("Heap Available:"..heapSize)
local heapSize=nil
--debugging end
end)
end)