My code below does pretty much what I want - open in a browser and it displays a slider control, sends it's value when it changes to the ESP8266 module which parses out the value, applies it to the pwm function of the red led and it's complement to the blue led. Slide the slider, see the rate of blinking speed up or slow down.
All good until the heap disappears and the module reboots. I sprinkled some 'show heap' print statements through the code with index numbers to make it easier to tell which printed value came from which part of the code.
Each time the slider is changed, four heap values are printed out. Here's a view of what the first few heap values look like:
=node.heap()
25464
1= 23560
2= 23880
3= 23880
4= 22288
1= 23216
2= 22832
3= 22880
4= 21336
1= 22840
2= 22488
3= 22536
4= 20984
1= 22192
2= 21792
3= 21800
4= 20248
1= 22632
2= 22232
3= 22304
4= 20752
1= 22304
2= 21896
3= 21904
4= 20376
1= 22264
2= 21920
3= 21968
4= 20416
1= 21912
2= 21544
3= 21456
4= 19824
A little up then a big jump down, relentlessly, heading for the PANIC- out of memory error and reboot.
If anyone has ideas on how to debug this or suggestions as what is causing it, I would love to hear from you! Thanks!
wifi.setmode(wifi.STATION)
wifi.sta.config("xxx","yyy")
print(wifi.sta.getip())
redled = 3
blueled = 4
gpio.mode(redled, gpio.OUTPUT)
gpio.mode(blueled, gpio.OUTPUT)
srv=net.createServer(net.TCP,3600)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
print("1",node.heap())
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
print("2",node.heap())
pwm.setup(redled,1000,511)
pwm.setup(blueled,1000,511)
tmpwm = 50
ramp = 0
pwmout = 100
if(_GET.somevar) then
pwm.start(redled)
xxx = (_GET.somevar);
tmpwm = tonumber(xxx)
if(tmpwm>1023) then
tmpwm = 1023
end
end
function motordrive()
ramp = ramp + 80
if (ramp > tmpwm) then
ramp = 0
if rampdir then
pwmout = pwmout + 10
if pwmout > 1023 then
pwmout = 1023
rampdir = false
end
else
pwmout = pwmout - 10
if pwmout < 10 then
pwmout = 5
rampdir = true
end
end
end
pwm.setduty(redled,pwmout)
pwm.setduty(blueled,1024 - pwmout)
end
tmr.alarm(0, 1, 1, motordrive)
print("3",node.heap())
conn:send('<!DOCTYPE html>')
conn:send('<html>')
conn:send('<body bgcolor="Turquoise">')
conn:send('<h1><center>===> X*Y*Z*T <===</center></h1>')
conn:send('<h1 style="font-size:300%;">')
conn:send('<center><p id="demo" /p></center>')
conn:send('<center><font color=red><b>Faster</b></font>')
conn:send('<input style="width:550px; height:50px" type="range" name="cmd" id="cmd" value="" min=1 max=1023 step=1 onchange="myFunction()"/>')
conn:send('<font color=red><b>Slower</b></font></center></form></h1>')
conn:send('<script>')
conn:send('function myFunction() {')
conn:send(' var x = document.getElementById("cmd").value;')
conn:send(' document.getElementById("demo").innerHTML = x ;')
conn:send(' var uri = "\?somevar=" + x;')
conn:send(' var xhttp = new XMLHttpRequest();')
conn:send(' xhttp.open("GET", uri, true);')
conn:send(' xhttp.send();')
conn:send(' }</script>')
conn:send('</body>')
conn:send('</html>')
client:close();
collectgarbage();
print("4",node.heap())
end)
end)