I have an Inky Frame 4 powered over USB. I noticed the WLAN connection fails exactly every other reset (by reset button on the pico). For example:
Power on. I get a joke of the day.
Reset. Error
Reset. Joke
Reset. Error
Reset. Joke
I upgraded the MicroPython first which didn’t change anything, and added simple logging to certain instructions which told me that it is caused by wlan.status() alternating betweein -1 and 3.
Does anyone have any idea what could cause this, or what I could try to figure it out?
What is interesting though, if I open Thonny, and I open main.py from the device, and I run it using soft reboot (F5), I can run it again and again and it always connects properly!
MicroPython v1.21.0, inky_frame v1.21.0 on 2023-10-06; Raspberry Pi Pico W with RP2040
Type "help()" for more information.
Soft reboot is not the same as a hard reset, so this is not surprising.
What could be the cause: the WLAN of the Pico needs some time to setup, but it timeouts too early. At the second try, things are faster because your router already knows the MAC and is somewhat faster.
I experience this kind of behavior too (running CircuitPython). I connect to WLAN in a loop and this helps, since usually the second try works.
Yes indeed. I mean ‘interesting’ as it’s a hint that it’s not a direct result of my code causing this behavior.
This hypothesis is definitely the most probable. I changed the example code from inky_helper.py like so:
# Handle connection error. Switches the Warn LED on.
if wlan.status() != 3:
stop_network_led()
led_warn.on()
print('failed to connect')
with open("wlan.log", "a") as f:
f.write(f"SSID: {SSID}, status: {wlan.status()}\n")
f.flush()
# Retry
time.sleep(1)
led_warn.off()
network_connect(SSID, PSK)
else:
print('connected')
And now the WLAN always connects after flashing the warning LED once! Thank you!
I don’t think this is the case though, because the connection waits for 10 seconds to be established, but when it fails, it turns to status: -1 within 1 seconds. But when it succeeds, it takes 3 seconds to get status: 3.
If it simply takes a bit longer, I would understand. But it fails so fast. I would imagine the following code to block long enough for the wlan to be ready for wlan.connect() to succeed:
# Enable the Wireless
wlan.active(True)
Thanks to your suggestion I got it working. Although I’d prefer to code something that blocks until the wlan is really ready to connect.