2

I am new to lua and nodemcu. I have a script that runs in the following way:

1) Search for two configuration file in flash, if both are present, then configure the wifi AP in station mode(wifi configuration is stored in file). Then create a one shot alarm to trigger callback after 20 sec to check whether we got an IP.

2) If IP is received before the callback, then, unregister the previous timer and perform an HTTP post and also create another timer which triggers after another 20 sec to check whether http callback was executed.

3) If http callback runs first then it unregisters the previous timer.

4) After all these checks just start a timer that runs a script "pollslave.lua" every 10 ms to poll the HSPI bus.

I tried to make it pretty sure that at a time only one timer is running.

I am testing now by keeping the server(in dev. PC) offline so that HTTP callback is not executed and the timer callback executes first.

It starts polling the slave but after about 10sec nodemcu reboots. Folowing is my "init_test.lua"

local fileutil=require("fileutility")
local wifiutil=require("wifiutility")
local httputil=require("httputility")

local dvconf="dvconf.cnf"
local wificonf="wificonf.cnf"
local post_method="TestSrv"
msg=""

wifi.eventmon.register(wifi.eventmon.STA_GOT_IP,
    function(T)
        print("IP Received")
        if wifitout then
            wifitout:unregister()
            print("WiFi Timout Monitor was Running...hence stopped")
            wifitout=nil
        end
        local dconf=fileutil.loadDeviceConfigs(dvconf)
        local srv_url=dconf.server
        local auth_id=dconf.auth_id
        local pwd=dconf.pwd
        local len=string.len(srv_url)
        if string.sub(srv_url,len,len)~="/" then
            srv_url=srv_url.."/"
        end
        httpTimeoutMon()
        httputil.getServerLogin(srv_url..post_method,
        auth_id,pwd,function() startPollSlave() end)
    end)

spi.setup(1,spi.MASTER,spi.CPOL_HIGH,spi.CPHA_LOW,8,80,spi.FULLDUPLEX)

wifi.setphymode(wifi.PHYMODE_N)


function startPollSlave()
    pollslavetimer=tmr.create()
    pollslavetimer:alarm(10,tmr.ALARM_AUTO,function() dofile("pollslave.lua") end)
end

function httpTimeoutMon()
    srvtout=tmr.create()
    print("Starting Server Timeout Monitor..")
    srvtout:alarm(20000,tmr.ALARM_SINGLE,function()
        print("http server Timeout Occured...")
        if pollslavetimer then
            local s,m=pollslavetimer:state()
            if s==false then
                msg="Server is offline.."
                pollslavetimer:start()
                print("Pollslave was created but not running..hence started..")
            end
        else
            print("Pollslave was not created...hence started...")
            msg="Server is offline.."
            startPollSlave()
        end
    end)
    print("Started Server Timeout Monitor...")
end

function wifiTimeoutMon()
    wifitout=tmr.create()
    print("Starting WiFi Timeout Monitor")
    wifitout:alarm(20000,tmr.ALARM_SINGLE,function()
        print("20 sec Timeout Occured..")
        if pollslavetimer then
            local s,m=pollslavetimer:state()
            if s==false then
                wifi.sta.clearconfig()
                msg="No Internet Access.."
                pollslavetimer:start() 
            end
        else
            wifi.sta.clearconfig()
            msg="No Internet Access.."
            startPollSlave()
        end
    end)
    print("WiFi Timeout Monitor Started..")
end

if file.exists(dvconf) then
    print("Device Settings File found...")
    local dconf=fileutil.loadDeviceConfigs(dvconf)
    if file.exists(wificonf) then
        print("WiFi Configurations found...")
        wifi.sta.getap(1,function(table)
            print("WiFi Scan Complete...")
            local count,wtable=wifiutil.indexWiFiList(table)
            print("Detected Usable WiFi: "..count)
            print("Sorting WiFi..")
            wtable=wifiutil.sortWiFiList(wtable,count)
            local i=0
            print("Loading Stored WiFi..")
            local wconf=fileutil.loadWiFiConfig(wificonf)
            while i<count do
                local j=0
                while wconf and wconf[j] do
                    if wtable[i].ssid==wconf[j].ssid and wtable[i].bssid==wconf[j].bssid then
                        print("Stored WiFi Matches with detected wifi no. "..i)
                        wifiTimeoutMon()
                        print("Configuring detected WiFi..")
wifi.sta.config({ssid=wtable[i].ssid,pwd=wconf[j].pwd,bssid=wtable[i].bssid})
                        print("Configured..Now Wait For IP...")
                        i=count;break
                    end
                    j=j+1
                end
                i=i+1
            end
            if i==count then
                msg="Please Connect To WiFi.."
                startPollSlave()
            end
            print("Leaving now..")
        end)
    else
        print("WiFi Configurations not found...")
        --startPollSlave()
    end
else
    print("Device Configurations not found...")
    --startPollSlave()
end

Following are the debugging output....

dofile("init_test.lua")
Device Settings File found...
Device Configurations loaded...
WiFi Configurations found...
> WiFi Scan Complete...

Detected Usable WiFi: 4
Sorting WiFi..
Loading Stored WiFi..
Loading line 0
Stored WiFi Matches with detected wifi no. 0
Starting WiFi Timeout Monitor
WiFi Timeout Monitor Started..
Configuring detected WiFi..
Configured..Now Wait For IP...
Leaving now..
IP Received
WiFi Timout Monitor was Running...hence stopped
Device Configurations loaded...
Starting Server Timeout Monitor..
Started Server Timeout Monitor...
http post issued..now wait for server response...
HTTP client: Disconnected with error: -11
HTTP client: Connection timeout
HTTP client: Connection timeout
http server Timeout Occured...
Pollslave was not created...hence started...
Got:255 Heap Remaining:19840
Got:255 Heap Remaining:19840
Got:255 Heap Remaining:19840
Got:255 Heap Remaining:19840
...........
Got:255 Heap Remaining:1312
Got:255 Heap Remaining:1312
Got:255 Heap Remaining:1312
Got:255 Heap Remaining:1104
Got:255 Heap Remaining:1104
...........
Got:255 Heap Remaining:1104
E:M 520
PANIC: unprotected error in call to Lua API (not enough memory)

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x40100000, len 27264, room 16 
tail 0
chksum 0xc0
load 0x3ffe8000, len 2432, room 8 
tail 8
chksum 0x7b
load 0x3ffe8980, len 136, room 0 
tail 8
chksum 0xe0
csum 0xe0
  • Try to cut pieces away to identify in which part of the whole application you're loosing memory. – Marcel Stör Aug 18 '17 at 04:44
  • 1
    this part: function startPollSlave(). when I increse the polling interval to 50 ms the problem goes away I think the sdk gets enough time to garbage collect. But It would be better to use 10 ms polling interval i really wish if there was a work around in here – Bodhayan Nandi Aug 18 '17 at 06:15
  • 1
    @MarcelStör I even tried running a simple timer like this-> mytimer:alarm(10,tmr.ALARM_AUTO,function() print("Heap:"..(node.heap())) end) In this case also I can see the heap size goes decreasing very rapidly. – Bodhayan Nandi Aug 19 '17 at 04:16
  • If timing hides a problem, then it is not leakage. Maybe collector should be tuned by means of https://www.lua.org/manual/5.1/manual.html#2.10 – user3125367 Aug 20 '17 at 09:34
  • Or you can try to do a manual full cycle via `collectgarbage()` once that hits low water. – user3125367 Aug 20 '17 at 09:37
  • I am calling collectgarbage() when hepsize goes below 4000 that frees a lot of memory, but, only temporarily and ultimately at a later timer even the collect garbage is unable to deal with the problem because the rate of heap loss is much faster. Now I am thinking about running another custom build from nodemcu-build.com website(thaks to you for this facility). I want to eliminate some modules. Currently I am using the following module: file,gpio,http,net,node,sntp,spi,timer,uart,wifi,ssl, – Bodhayan Nandi Aug 21 '17 at 23:45
  • I am not using uart for any purpose, I included thinking that it is needed to display debugging output on esplorer, can I remove this module?,will esplorer work if I do? Also am not using gpio so I can remove that, and also remove http and handle everything manually using net. Does this sounds a good plan or this modules are not that much memory hungry? I ask this because a whole lot of code modifications will be needed if I proceed with this plan. – Bodhayan Nandi Aug 21 '17 at 23:54
  • Also I don't plan on using any function of the node module, so, if I remove that will the SDK still work in the same way as it does now? – Bodhayan Nandi Aug 21 '17 at 23:58

0 Answers0