Page 1 of 1

Strange behaviour with Thonny/standalone

Posted: Tue Sep 27, 2022 2:14 pm
by warren
Hi

I have some simple code running on a Pico W which does the following:

Establishes a Wifi connection to my network
Repeatedly takes temperatures from a sensor
Sends them to my MQTT broker
Flashes the onboard Pi Pico W LED each time it publishes.

I have a main.py programme, and several other libraries that are loaded (including umqtt.robust2)

If I plug the board into my USB port when Thonny is open, I can bring up the main.py file. If I then click the 'play' button, the code works perfectly:

I can see in my Mosquitto logs that a new connction has been made from the Pico. The pico connects to the broker, Temperatures are published and each time the LED flashes.

If however I close thonny, and then disconnect and reconnect the Pi Pico (i.e. in standalone mode), it still seems to boot, and flash the LED - implying it *thinks* it is publishing. If at this point I restart Thonny, I can see the publication message being printed over repl.

BUT nothing is actually published. There is no 'new connection' message in the Mosquitto log...

It *seems* as my code genuinely thinks it is connected, and is publishing, but it is not doing so...

The same thing happens if I just plug it into a battery pack (eliminating USB data connections entirely - just two power leads)

What could possibly account for this difference in behaviour in stand-alone mode?

Thanks!

Re: Strange behaviour with Thonny/standalone

Posted: Tue Sep 27, 2022 4:40 pm
by scruss
(this forum is going read-only soon. If you're not happy using Discussions ยท micropython on github, maybe re-ask on MicroPython - Raspberry Pi Forums)

Thonny quietly sets the Pico's system clock on connection. In standalone mode, this doesn't happen. Maybe your MQTT broker is expecting a mostly-correct timestamp? Since you're connecting to a network, you could use the ntptime.settime() method (it's a built-in library) to set the clock on boot.

It might be worth adding an LED showing whether you have established a network connection or not

Re: Strange behaviour with Thonny/standalone

Posted: Wed Sep 28, 2022 4:10 pm
by tonyek
hi I'm having very similar issues with the pico w. Did the answer below solve the situation.

Re: Strange behaviour with Thonny/standalone

Posted: Sat Oct 01, 2022 12:50 pm
by beetle
Ha, just came across this when doing a final test to check if my picoW will come up when I get a momentary power cut which we can be prone to when the trees that line the powerline route have not been trimmed. If the picoW is down for about 10 seconds or so then it will reconnect to the wifi ok, otherwise a connection loop will continually return a status of -1.

A search brought me here, but after a little experiment I find this can be catered for by ensuring a wifi disconnect and a soft reboot is made when a status of -1 is detected.

See the following code. If connected to power after approx 10 seconds I get one blink (indicating joining a network) followed by a steady light to indicate wifi is connected.

If I disconnect the power but the swiftly reconnect it, I get one blink (joining) followed by 4 blinks (link fail -1). The code will then issue a wifi disconnect followed by a soft reboot and then I get another one blink (again joining) followed by a steady light (wifi up) :D

If I remove the disconnect and soft reboot code then I continually get the 4 blinks infinitum, and no wifi connection :?

Note this code is in the main.py file.

Code: Select all

import time
from machine import Pin, soft_reset as Reset
import network

ssid = '****'
password = '****'

wifi_LED = Pin("LED", Pin.OUT)
wifi_LED.off()

def status_LED():
    """
    note network status values:
    0   CYW43_LINK_DOWN     blink 7
    1   CYW43_LINK_JOIN 	blink 1
    2   CYW43_LINK_NOIP     blink 2  
    3   CYW43_LINK_UP       no blink set LED on when isconnect is True.
    -1  CYW43_LINK_FAIL     blink 4   
    -2  CYW43_LINK_NONET    blink 5    
    -3  CYW43_LINK_BADAUTH  blink 6   
    """
    status = wlan.status()
    if status < 0:
        status = (status * -1) + 3
    elif status == 0:
        status = 7   
    for blink in range(status):
        wifi_LED.on()
        time.sleep(1)
        wifi_LED.off()
        time.sleep(1)
    if status == 4:
        wlan.active(False)
        wlan.disconnect()
        Reset()
    
# enable station interface mode
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# turn off wifi power saving mode
wlan.config(pm = 0xa11140)
# connect to wifi
wlan.connect(ssid, password)

while not wlan.isconnected():
    print('NO network found')
    status_LED()
    time.sleep(3)

print('network conntected')
wifi_LED.on()