-->
Page 1 of 7

[SOLVED]wifi.sta.getap() memory usage

PostPosted: Wed Apr 15, 2015 11:12 pm
by dnc40085
UPDATE: Problem fixed, pull request was submitted and merged to the dev096 branch on 05/07/15.

Hello,
first and foremost I must say that i'm new to lua, and have taken to learning by coding, reading other lua scripts and-+ lua documentation and I believe i've got the gist of it but do to my n00bness there's a few things i don't quite know yet like how to figure out where all my memory is going.

I am currently using this firmware: nodemcu_float_0.9.6-dev_20150406.bin

my goal is to use wifi.sta.getap() to get info (SSID, RSSI, BSSID) about an AP hosted by another esp8266 module (also running same firmware) to use in my program and acquire this info while using the least amount of memory(heap).

For the most part I've achieved my goal but there's about 2k of heap I cant get back after I run wifi.sta.getap() and I don't know if it's my inexperience with LUA or if it is the wifi.sat.getap() function

init.lua
Code: Select alluart.setup(0,115200,8,0,1,1)
print("Ready!")


CheckAP.lua
Code: Select allfunction listap(t)
    if type(t)~="table" then
        print("not table")
        return
    end
    for key,value in pairs(t) do
        ssid = key
        enc, rssi, bssid, chan = string.match(value,
            "(%d),(-?%d+),(%x%x:%x%x:%x%x:%x%x:%x%x:%x%x),(%d+)")
        print(ssid..":\t\t"..bssid..","..rssi.."\n")
        ssid=nil enc=nil rssi=nil bssid=nil chan=nil
    end
end
wifi.setmode(wifi.STATION)
wifi.sta.getap(listap)
listap=nil



After the ESP8266 starts node.heap() reports 20256 bytes free
then after I execute dofile("CheckAP.lua") node.heap() reports 17992 free.

I just can't figure this out, any help would be greatly appreciated.

Re: wifi.sta.getap() memory usage

PostPosted: Thu Apr 16, 2015 11:45 am
by mmcginty
I ran into that too, my solution was to virtualize, write results to file, reset, then process them.

I persist settings in another file, one of those settings is mode, that determines which part of the process it's working on.

For ease I store the settings in the same form as HTTP post blocks and URL parameters, which can be easily tablized like this:

for k,v in string.gmatch(inbuf, "(%w+)=(%w+%.?%w+%.?%w+%.?%w+)") do
cfg[k] = v
end

(%w+)=(%w+) would've worked nicely except that I wanted to store dotted-quad IP addresses in settings. The side-affect is that all config values must be at least 4 characters long, or must contain periods. Needless to say, no punctuation in key names and none other than . in values are supported.

I found that writing a file in append mode needs very little memory (that isn't already pre-allocated.) Assuming that gmatch always captures from left to right (which seems to be the case) you can append changes to the file without worrying about old values; the last one written will end up in the table when the file is parsed.

If there's a better way, I'd love to see it. :-)

-MM

Re: wifi.sta.getap() memory usage

PostPosted: Thu Apr 16, 2015 7:59 pm
by dnc40085
Thank you for your response,
I'm glad I'm not the only one running into this issue.

Although your solution may prove useful in other areas of this or other projects, I'm attempting to read the RSSI of the AP to send to the AP hosted server.

My goal is to use this data on the client side to conserve battery power by choosing when to connect to the ap and when to start sending data to the server, and on the server server side for proximity detection of the client device to choose when to turn on/off the output(s).

I should have stated these details in my original post, but forgot to, my apologies.

Re: wifi.sta.getap() memory usage

PostPosted: Fri Apr 17, 2015 3:40 am
by TerryE
The problem is your function listap. It's not a local function so the name is registered in the globals table, and therefore the memory for the code and function headers can't be garbage collected. If you have Lua installed on your PC, then you can do a luac -l of the dofile code to see what is generated.

If you want resources GCed, then declare them local.

Avoid writing stuff to flash, because it has a limited write life. If you do want to write configuration data to flash then at least check that it's changed before writing it. I know this is two pass, but your esp8266 will last a lot longer if you adopt this strategy.