circuitpython: QT PY ESP32-S2 core crash when Wi-Fi router is power-cycled

CircuitPython version

Adafruit CircuitPython 8.0.0-beta.4-21-g8f414eb4e on QT PY ESP32-S2.  

Code/REPL

from secrets import secrets
from time import sleep
import wifi

def connect_to_wifi():
    """Connect to Wi-Fi."""
    print(f'Connecting to {secrets["ssid"]}...')
    # Test Wi-Fi signal strength
    rssi = -255
    for network in wifi.radio.start_scanning_networks():
        if network.ssid == secrets["ssid"] and network.rssi > rssi:
            rssi = network.rssi
    wifi.radio.stop_scanning_networks()
    print(f'Wi-Fi rssi: {rssi} db')

    while True:
        try:
            wifi.radio.connect(secrets["ssid"], secrets["password"])
            print(f'Connected to {secrets["ssid"]}')
            break
        except Exception as wifi_err:
            print(f"Wi-fi connection error: {wifi_err}")
        sleep(1)

connect_to_wifi()

gateway_ip = wifi.radio.ipv4_gateway

while True:
    # Check Wi-Fi is connected
    ping = wifi.radio.ping(gateway_ip, timeout=2)
    if ping is None:
        print("Gateway could not be pinged!")
        connect_to_wifi()
        continue
    print(f"Ping: {ping}") 
    sleep(1)

Behavior

Had 4 QT-PY ESP32-S2’s running in my office. They all core crashed when I restarted my Wi-Fi router. Simplified my code and reproduced the problem multiple times. The code runs until the router is rebooted or powered down. Then it throws the following error:

]0;🐍Wi-Fi: off | Done | 8.0.0-beta.4-21-g8f414eb4e\Auto-reload is off. Running in safe mode! Not running saved code.

You are in safe mode because: CircuitPython core code crashed hard. Whoops! Crash into the HardFault_Handler. Please file an issue with the contents of your CIRCUITPY drive at https://github.com/adafruit/circuitpython/issues

Description

I don’t think this is a duplicate of “ping too frequently results in Safe Mode #5980” because I am waiting 1 second between pings and the code doesn’t crash from frequent pings. Instead, it crashes while the Wi-Fi is trying to reconnect. Furthermore, I’ve had a more complicated version of the code running for several days on 4 QT-PY’s and only encountered a core crash when the router went down.

Additional information

Removing the RSSI check that uses wifi.radio.start_scanning_networks() causes the crashes less frequently but they can still occur during reconnect. Occasionally, the code will not crash and reconnect properly. I suspect the longer the router is down the more likely the core crash. My router takes over a minute to restart.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 42 (14 by maintainers)

Most upvoted comments

@rdagger See https://learn.adafruit.com/building-circuitpython/ and particularly https://learn.adafruit.com/building-circuitpython/espressif-build. If you have or can set up a Linux box, that’s generally easiest. The DEBUG=1 build might be too big, and you may need to disable some features. Setting CIRCUITPY_ULAB=0 is often enough, because that’s large. We can help you with builds in https://adafru.it/discord in #circuitpython-dev.