Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pico W sometimes won't start AP immediately after stopping Station #7906

Closed
anecdata opened this issue Apr 25, 2023 · 4 comments · Fixed by #7982
Closed

Pico W sometimes won't start AP immediately after stopping Station #7906

anecdata opened this issue Apr 25, 2023 · 4 comments · Fixed by #7982
Labels
bug network rp2040 Raspberry Pi RP2040
Milestone

Comments

@anecdata
Copy link
Member

anecdata commented Apr 25, 2023

CircuitPython version

Adafruit CircuitPython 8.1.0-beta.1-46-gb5a26c7f9 on 2023-04-24; Raspberry Pi Pico W with rp2040

Code/REPL

import time
import wifi
import os
import traceback
import microcontroller
import supervisor

time.sleep(3)

while True:
    try:
        print(f"starting station...", end=" ")
        wifi.radio.start_station()
        while not wifi.radio.connected:
            wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
            print(f"connected={wifi.radio.connected} {wifi.radio.ipv4_address}")
        print(f"stopping station...")
        wifi.radio.stop_station()  # no delay between stop station stop and start AP
        wifi.radio.start_ap('test', 'lksdfjhksdfjlsdfjgh')
        print(f"ap_active={wifi.radio.ap_active} {wifi.radio.ipv4_address_ap}")
        time.sleep(5)
        microcontroller.reset()
    except Exception as ex:
        print(f"")
        traceback.print_exception(ex, ex, ex.__traceback__)

Behavior

This arose on Discord (https://discord.com/channels/327254708534116352/537365702651150357/1100202386905112576 or discord://discord.com/channels/327254708534116352/537365702651150357/1100202386905112576). About 10% of the time, wifi.radio.start_ap() will fail with RuntimeError: Wifi is in station mode. if it immediately follows wifi.radio.stop_station().

A small time delay, or:

        wifi.radio.stop_station()
        while wifi.radio.connected:
            pass
        wifi.radio.start_ap('test', 'lksdfjhksdfjlsdfjgh')

seems to suffice to let start_ap() succeed consistently.

Is this the solution, or could wifi.radio.stop_station() not return until the station is fully stopped?

Description

No response

Additional information

Station-only loops like:

while True:
    wifi.radio.stop_station()
    wifi.radio.start_station()

or

while True:
    try:
        wifi.radio.stop_station()
        wifi.radio.start_station()
        while not wifi.radio.connected:
            wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
            print(f"connected={wifi.radio.connected} {wifi.radio.ipv4_address}")
    except Exception as ex:
        traceback.print_exception(ex, ex, ex.__traceback__)

do not exhibit any timing issues. Only when switching from Station to AP.

Addendum: Pico W starts up in Station mode. Even if it never connects to an AP, this can still happen. In that case, it doesn't make sense to check wifi.radio.connected, so a time delay may be the only workaround.

@anecdata anecdata added the bug label Apr 25, 2023
@tannewt tannewt added the rp2040 Raspberry Pi RP2040 label Apr 25, 2023
@tannewt tannewt added this to the Long term milestone Apr 25, 2023
@bill88t
Copy link

bill88t commented May 15, 2023

The delay could be considered a 'necessary' part of stopping STA.
So perhaps it should just be added in the stop_station code.

If you guys agree on that I will go ahead and PR it.

@dhalbert
Copy link
Collaborator

That makes sense. If the delay is known, the code can wait. It could also poll some status itself and return quicker if it has stopped, but that may not be possible. It shouldn't poll indefinitely in case the status is never satisfied.

@anecdata
Copy link
Member Author

anecdata commented May 15, 2023

How about looping this check:

if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA) != CYW43_LINK_DOWN) {

in wifi.radio.stop_station()? I wonder if the wifi leave AP is needed then.

@bill88t
Copy link

bill88t commented May 15, 2023

Sounds good on paper, but will have to be tested. I will do so.
I will also add a regular timeout so it doesn't get stuck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug network rp2040 Raspberry Pi RP2040
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants