0

My PICO W hangs sometimes after calling deinit(). I'm using Micropython v1.25.0 (2025-04-15)

Please see this example script:

import network
import time
from time import sleep
import urequests
import gc

WLAN_SSID = 'MYSSID'
WLAN_PASSWORD = '123'

wlan = network.WLAN(network.STA_IF)

counter = 1
while True:
    print('Start round {}'.format(counter))

    wlan.active(True)
    wlan.connect(WLAN_SSID, WLAN_PASSWORD)

    for i in range(20):
        if wlan.isconnected():
            break
        print('Trying to connect... Status is: ', wlan.status())
        time.sleep(1)

    if wlan.isconnected():
        response = urequests.get('https://www.google.com')
        response.close()

    print('Deactivate WLAN...')
    print('Memory alloc {}'.format(gc.mem_alloc()))
    print('Memory free {}'.format(gc.mem_free()))

    wlan.disconnect()
    wlan.active(False)
    wlan.deinit()

    print('Sleeping...')
    time.sleep(5)
    counter += 1

This script works for 5-30 times until it suddenly hangs on print('Deactivate WLAN...'). Reason is the call to deinit()

I need to call deinit(), since I want to put the PICO into deepsleep mode afterwards.

Any ideas what's going wrong here?

Thanks!

5
  • Hard to tell, but since you don't validate the network is connected after leaving the loop, in the event the network didn't establish a connection, you are calling .disconnect(), setting .active(False) and calling deinit() anyway. I don't use micropython, but it would be worth checking to determine the consequence of making those calls if a connection wasn't established. Simply wrapping those calls in another if wlan.isconnected(): would protect against that case. You can added error output within that if block to provide a diagnostic as well. Commented Jul 6 at 2:51
  • Thanks for your input! I tested this as well, but same behaviour. This wouldn't be a real solution for me either, since I really need to power down the WIFI chip (with deinit()) in order to bring the pico into deepsleep mode. I think this is a bug either in micropython or in the chip itself. I opened an issue here github.com/micropython/micropython/issues/17621 Commented Jul 6 at 16:15
  • 1
    That's part of the missing information. Which WiFi library for the Pico W is micropython using? I've done most work with Pico W with C prior to the 2.0 release of the SDK. At that time there were two primary WiFi libraries you could use. At that point in time sleep on the Pico was experimental, but work was underway to complete it. If I were writing in C, I'd insert more diagnostic printf to find the exact point of failure. You can see if micropython offers anything (IP received, etc..) that you can verify. Pico W is a great little board, and a fun challenge with WiFi. Commented Jul 6 at 22:51
  • The microypthon devs have confirmed that this is a bug and they already provided a fix for it. github.com/micropython/micropython/pull/17624 The fix should be merged into the 1.26 release of micropython. Commented Jul 15 at 9:25
  • Well done. Amazing when you do everything right, things can still go wrong :( They have had a few issues with recent changes. Trying to build picotool yesterday, we found bug #2552 (uint8_t does not name a type). Likewise, it was also fixed -- you just had to switch to the "develop" branch of the Pico-SDK git repository.... Commented Jul 16 at 2:00

1 Answer 1

0

I had something similar happen. I wound up changing from MicroPython version 1.25.0 to 1.24.1. So far, it seems to have solved my problem. I have to deinit in between listening to an NRF24L01 module. Can't run both both at the same time, so I have to listen, then stop listening, then init wifi, do networky things, then deinit wifi, then start listening again. After several cycles with the 1.25.0, it seemed to hang up when trying to deinit or init wifi.

Maybe downgrading is your solution??

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.