wlan.scan() not working on FiPy
-
So I get wlan.scan() to work on Gpy, but when I try the same code on a FiPy (Legacy firmware, 1.20.0.rc13) I just get "OSERROR: Scan operation Failed!"
Minimum failure example.
from network import WLAN wlan = WLAN(mode=WLAN.STA) wlan.scan()
Tried with wifi_on_boot set as both False and true, no change. Please advise on how to get passed this problem.
-
@andreas The typical structure you'll find in most MicroPythos-APIs for hardware devices is:
- there is a xxx_make_new (in this case wlan_make_new(), line 1279, mods/modwlan.c), which is called when the constructor in python is called. This function shows all the init arguments with its default values. It calls xxx_init_helper().
- there is a xxx_init (in this case wlan_init(), line 1305, which more or less only calls xxx_init_helper().
- and finally there is xxx_init_helper (here wlan_init_helper(), line 1145, which actually scans the supplied parameters and sets up the device. Parameters which are not supplied are set to the default values.
-
@robert-hh said in wlan.scan() not working on FiPy:
Yes and not. You can use the constructor to set all init parameters, and you can do that in the init call. What fails is an attempt to split the configuration up between calling the constructor and calling init().
I would second that. I just had a look under the hood and wanted to share the pointer to the code and my findings back with you.
The reason why I've investigated this again is that we still experience WiFi connection problems on the very first attempt after a hard reset. This is deterministic for us on a FiPy sitting on an expansion board on our workbench.
Invoking
.connect()
twice with a short delay in between makes things work. While this feels weird, we haven't been able to find a different workaround.
-
@andreas Yes and not. You can use the constructor to set all init parameters, and you can do that in the init call. What fails is an attempt to split the configuration up between calling the constructor and calling init(). Because init will use it's default values for all parameters not set.
Saying that, I did not look into the code again. I'm just assuming the usual coding practice in MicroPython.
-
Dear Robert,
@robert-hh said in wlan.scan() not working on FiPy:
Calling the constructor WLAN(....) implies calling init. Calling both with different parameters is troublesome.
In order to get everything right, I've just investigated this very aspect. According to the lowlevel code at [1], I found that the WiFi peripheral is actually not initialized when the constructor is called without arguments.
In this case,
.init()
will do just that and so a two-stage approach feels sensible where there is room in between for setting up the details in a discrete, step-by-step manner as outlined within [2].I.e., we want to configure the WiFi mode and whether an external antenna is connected or not based on individual configuration settings. Doing it that way, we will not have to get into spaghetti code having to call the constructor with different combinations of arguments.
Please let me know if I might have overlooked something here.
With kind regards,
Andreas.[1] https://github.com/pycom/pycom-micropython-sigfox/blob/69dd8b5d/esp32/mods/modwlan.c#L1294-L1301
[2] https://github.com/hiveeyes/hiveeyes-micropython-firmware/blob/4cf2c516/terkin/network/wifi.py#L82-L111
-
@robert-hh Thanks for letting me know. I will try to optimize our datalogger codebase regarding this aspect.
-
@andreas Calling the constructor WLAN(....) implies calling init. Calling both with different parameters is troublesome.
-
Dear @sandris,
@sandris said in wlan.scan() not working on FiPy:
OSError: Scan operation Failed!
Maybe try
wlan.init()
first. See also https://forum.pycom.io/topic/5258/firmware-release-v1-20-1/41.With kind regards,
Andreas.
-
Hi thx, just a little typo there yes.
-
@sandris said in wlan.scan() not working on FiPy:
from network import WLAN
wlan = WLAN(mode=WLAN.STA)
wlan.scan():In this example the line wlan_scan() end with a colon. Since that is an syntax error, I assume that your actual code is different.
-
@oligauc said in wlan.scan() not working on FiPy:
pycom-fwtool-cli.exe
No luck. I've tried with two diffrent FiPys and both fail.
-
@sandris Can you do an erase from the Terminal / command prompt please and retry.
pycom-fwtool-cli.exe -p 'your COM port' erase_all
and re-flash