As the title says... Chat on...

User avatar
By joefly888
#20500 I got a simple script that is basically tmr.alarm every 10seconds calls a function to do a http POST request to ubidots.com

I noticed my script each time it "loops" I print out the heap, it decreased by varying amounts from a few to over 100 kb. It runs for awhile before crashing due to lack of memory.

The HTTP connection is closed, I continually use the same variables.

What could be causing the HEAP leakage? Is there something in particular in my code that I should be careful of or investigate?
User avatar
By joefly888
#20560 hello DNC, I thought it was my code, so I started tracking it down and it is happening even when no code is running.

after file.format()
I have this init.lua running
Code: Select allprint (node.heap())
print (node.heap())
print (node.heap())
print (node.heap())
print (node.heap())

tmr.alarm(0,5000, 1, print(node.heap() ))


this is the output
Code: Select allNodeMCU 0.9.5 build 20150214  powered by Lua 5.1.4
21336
20992
20952
20912
20872
20832
>


notice the decreasing node count. Again, after file.format() and each restart, the same node output, so it is consistent, at least in this code. I also run print(node.heap()) in esplorer terminal after this output and see that it recovered and stabilizes at 22688.

also a separate problem. My is my tmr.alarm not running at all?


is this a bug?
User avatar
By joefly888
#20564 Given my code stabilizes at 22688 and assuming this is not a bug

I run this code and I get continual decreasing heap.
Code: Select alltempPin = 3  --gpio00 for temp
motion = 7 --gpio13 for motion detection
door = 1 -- gpio05 for door sensor
gpio.mode(motion, gpio.INPUT)
gpio.mode(door, gpio.INPUT)

function gettemp()
   t = require("ds18b20")
   t.setup(tempPin)
   addrs = t.addrs()
   if (addrs ~= nil) then
     print("Total DS18B20 sensors: "..table.getn(addrs))
   end
   temp = t.read()
   print ("before ..."..node.heap())
   -- Don't forget to release it after use
   t = nil
   ds18b20 = nil
   package.loaded["ds18b20"]=nil
   print ("after ..."..node.heap())
end

function sendData()   --- Post new datapoints to ubidots
    print("Sending data to ubidots.com")     -- conection to ubidots.com
    connout = nil
    connout = net.createConnection(net.TCP, 0)
    connout:on("receive", function(connout, payloadout)
        if (string.find(payloadout, "201 CREATED") ~= nil) then
            print("Posted OK");
        end
    end)

    function postIt (varName, idvariable, value)
        print ("Posting "..varName..": "..value..".....");   
        local postval = "POST /api/v1.6/variables/"..idvariable.."/values HTTP/1.1\n"       
          .."Content-Type: application/json\n"
          .."Content-Length: "..(string.len(value)+11).."\n"   -- length of {"value": '..value..'}'
          .."X-Auth-Token: ubidotstoken\n"
          .."Host: things.ubidots.com\n\n"
          ..'{"value": '..value..'}\n\n';
        connout:send(postval)
     end
         
     connout:on("connection", function(connout, payloadout)
        gettemp()
        postIt("Temp", "ubidotsID1", temp)
        postIt("Volt", "ubidotsID2", node.readvdd33() / 1000)
        postIt("Door", "ubidotsID3", gpio.read(door))
        print("heap: "..node.heap())
    end)

    connout:on("disconnection", function(connout, payloadout)
        connout:close();
        collectgarbage();
        print("disconnect")
    end)
    connout:connect(80,'things.ubidots.com')
    end
     
-- send data every X ms to ubidots
tmr.alarm(0, 10000, 1, function() sendData() end )