Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8096

MicroPython • Re: Why does this code only work over USB connected to Thonny?

$
0
0

Code:

... wlan = network.WLAN(network.STA_IF)wlan.active(True)wlan.connect(SSID,password)time.sleep(2)max_wait = 10while max_wait > 0:    if wlan.status() < 0 or wlan.status() >= 3:        break    max_wait -= 1.... 
The "while max_wait > 0:"-loop runs really quick, the effective wait time for wlan connection is only 2 seconds. If you add a time.sleep(1) in this while loop, the results could be better. You could even wait longer as without wifi the pico could not send away the notifications

When running this on thonny, the wifi chip has some time to settle between power up and start (manually) than when running from a wall adapter. So adding some more delay could be needed.

As you see the LED blink signals when running from a wall adapter, another obvious error can be excluded: the code file should be named main.py on pico.

What you could add to the code to improve error tracing is a 'log to file' feature.
Example, untested:

Code:

import timedef log_to_file(message:str):    print(message)    with open("logfile.dat", "at") as f:        f.write( f"{time.time():} {message}" + "\n");log_to_file("this is a test")
In a better implementation, there would be some checks for file size in order not to fill up the free space.

Statistics: Posted by ghp — Thu Apr 25, 2024 5:44 am



Viewing all articles
Browse latest Browse all 8096

Trending Articles