Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Hangs when reopening WiFi connection #167

@peterhinch

Description

@peterhinch

This sample works if run after a power cycle. It hangs if a WiFi connection is already present (e.g. on a second run). It works if the commented-out sleep() statements are inserted. The aim here is to achieve a reliable (re)connection regardless of initial conditions and after a WiFi outage.

import uasyncio as asyncio
from utime import sleep
import network
s = network.WLAN(network.STA_IF)
s.active(True)

async def wifi_connect():
    print('WiFi connect')
    s.disconnect()
#    sleep(0.1)
    await asyncio.sleep(1)
    s.connect('SSID', 'PASSWORD')  # EDIT this
    print('Awaiting conection')
    while not s.isconnected():
#        sleep(0.1)
        await asyncio.sleep(1)
    print('Got conection, pausing')
    for _ in range(3):
#        sleep(0.1)
        await asyncio.sleep(1)
    print('conection done')
    return

loop = asyncio.get_event_loop()
loop.run_until_complete(wifi_connect())

Is there an issue of yielding to the underlying RTOS? sleep() statements are of course inappropriate in asynchronous code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions