wlan.scan() failed, or silently "hangs" after a while

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
DMWidgets
Posts: 8
Joined: Fri Feb 16, 2018 4:28 pm

wlan.scan() failed, or silently "hangs" after a while

Post by DMWidgets » Fri Feb 16, 2018 6:49 pm

edit: pared the code down to a simple "write results of wlan.scan() to file" as a starting point to debug the issue. Still eventually run into the same problem; the script hangs after a while.

Running latest firmware on an ESP8266 and I'm having an issue that doesn't seem to be giving me an exception when network.scan() fails.

Currently continuously calling a simple loop that writes any newly found access points to a txt file using the scan() function. Earlier versions of my function would eventually return an "OSerror wlan.scan() failed", but I don't seem to get that specific error anymore. Instead now the function will run quite happily until an indeterminate time in the future where it quietly stops working.

(Tried a couple ways to format this so it's readable. Haven't figured out the right tags yet...)

def wifirecorder():
global bssid, total
added = 0
res_file = open('results.txt', 'a')
results = sta_if.scan()
if not results:
pass
for ap in results:
if not ap[1] in bssid:
total += 1
added += 1
bssid.append(ap[1])
res_file.write(
str(ap[0].decode()) + ',' + str(ubinascii.hexlify(ap[1]).decode()) + ',' + str(
ap[2]) + ',' + str(ap[3]) + ',' + str(ap[4]) + ',' + str(
ap[5]) + '\n')
print('added ' + str(added) + ' access points')
res_file.close()
gc.collect()
[/code]

If I let the board scan from a stationary location, like just sitting here on my desk, it will run seemingly forever. Once I walk it around in the apartment complex in order to test its actual use-case, it breaks. I can seem to extend the life of the cycle by walking slowly enough to introduce 10~ new APs per scan but if the ESP suddenly starts finding a lot more brand-new APs, it seems to kinda crap out. My best guess is somewhere within that loop the board is running out of memory. That, or I'm needlessly over-working the board with something in my janky code....or the issue is originating from the scan() method.

Anyone have an idea? Or if not, what I could append to my function to try and catch where/how it's failing?

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: wlan.scan() failed, or silently "hangs" after a while

Post by cefn » Mon Feb 19, 2018 1:39 pm

The post is back, and hopefully the user too, maybe thanks to viewtopic.php?f=2&t=4413

DMWidgets and I shared a bunch by email while things were offline. In particular I drew their attention to the string concatenation and likelihood of fragmentation leading to out of memory errors from e.g.

Code: Select all

str(ap[0].decode()) + ',' + str(ubinascii.hexlify(ap[1]).decode()) + ',' + str(
ap[2]) + ',' + str(ap[3]) + ',' + str(ap[4]) + ',' + str(
ap[5]) + '\n')
See "String Concatenation" in http://docs.micropython.org/en/v1.9.3/e ... ained.html

Having done what they can with fixed buffers instead of string concatenation, they are now exploring the memory overhead of a menu module they were importing to try and improve things.

DMWidgets
Posts: 8
Joined: Fri Feb 16, 2018 4:28 pm

Re: wlan.scan() failed, or silently "hangs" after a while

Post by DMWidgets » Mon Feb 19, 2018 4:11 pm

Props to Cefn for his help while I was "indisposed" during that confusing turn of events.

I changed the way the string gets concatenated/lowered the gc threshold and monitored the memory usage while trying to replicate the error. After running the specific function alone on a spare board I've determined, between its highs and lows, it just eats up too much ram when scan() returns 50~ APs on occasion. the overhead of my menu module and related functions chews up about 22k ram, if I'm looking at things correctly, so I'll have to figure out how to pare that down. It has it's own issue of eventually reaching recursion depth which I don't know how I'll fix.

While the function now runs fine 95% of the time, I'm still getting an "OSerror: scan() failed" issue which I don't know how to pick apart to troubleshoot so in the mean-time I've just been calling the function again on failure. I've spent too many hours on this whole issue anyhow! :D

Post Reply