Skip to content

Adafruit ESP32-S2 Feather hard crashes with wifi.radio.ping #5745

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

Closed
BlueBackbite opened this issue Dec 17, 2021 · 6 comments
Closed

Adafruit ESP32-S2 Feather hard crashes with wifi.radio.ping #5745

BlueBackbite opened this issue Dec 17, 2021 · 6 comments
Labels
bug crash espressif applies to multiple Espressif chips
Milestone

Comments

@BlueBackbite
Copy link

CircuitPython version

Adafruit CircuitPython 7.1.0-beta.3 on 2021-12-13; Adafruit Feather ESP32S2 with ESP32S2
Board ID:adafruit_feather_esp32s2

Code/REPL

# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
# SPDX-FileContributor: Modified by Reppad
#
# SPDX-License-Identifier: MIT

import os
import board
import neopixel
import ipaddress
import wifi
import rtc
import time
import random
import socketpool

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

try:
    import json as json_module
except ImportError:
    import ujson as json_module


# Track time we've been unable to ping certain IPs
time_modem_ping = time.time()
# VERIFY this is the default modem management IP, it ussually is, and *ussually* responds to ping
modemIP = ipaddress.ip_address("192.168.100.1")

time_external_ping = time.time()
external_ips = [ipaddress.ip_address("1.1.1.1"),ipaddress.ip_address("8.8.8.8"),ipaddress.ip_address("8.8.4.4"),ipaddress.ip_address("1.0.0.1"),ipaddress.ip_address("208.67.222.222"),ipaddress.ip_address("208.67.220.220")]

# Delay the number of attempts we make, so we're not spamming ping all the time.
time_delay = time.time()

# Track when last power cycle happened.
time_reset = time.time()

#how often in seconds to check for connectivity (Will ping every x seconds) Don't flood the network with pings!
check_interval = 1

# count the number of pings
ping_counter = 0


#Set our hostname? (I think this works)
wifi.radio.hostname = "ModPowerMon"

print("Connect wifi")
wifi.radio.connect(secrets['ssid'],secrets['password'])
HOST = repr(wifi.radio.ipv4_address)
GATEWAY = repr(wifi.radio.ipv4_gateway)
DNS = repr(wifi.radio.ipv4_dns)
MYNAME = repr(wifi.radio.hostname)
PORT = 80        # Port to listen on
print(HOST,PORT)

# Use below for Most Boards
status_light = neopixel.NeoPixel(
    board.NEOPIXEL, 1, brightness=0.2
) 

# Run checks to see if we can reach outside the network...
def check_connectivity():
    global time_reset, time_modem_ping, time_external_ping, ping_counter
    sing_ip = random.choice(external_ips)
    print("pinging ",sing_ip)
    ping_result = wifi.radio.ping(sing_ip)
    if ping_result is None:
        #unable to ping site, try modem
        ping_modem_result = wifi.radio.ping(modemIP)
        if ping_modem_result is None and time.time() - time_modem_ping > 3600:
            # Toggle our latching relay to cut power to the modem for 2 minutes
            print("Haven't been able to reach any outside networks, for more than an hour")
            time_reset = time.time()
            time_external_ping = time.time()
            time_modem_ping = time.time()
        else:
            time_modem_ping = time.time()
    else:
        ping_counter += 1
        time_external_ping = time.time()
        ping_modem_result = wifi.radio.ping(modemIP)
        if ping_modem_result is None:
            print("Was able to ping external IP but not modem, that's weird.")
        else:
            ping_counter += 1
            time_modem_ping = time.time()
            print("Was able to ping external IP and modem, should be good to go!",ping_counter)

print(f"hostname: {MYNAME} gateway IP: {GATEWAY} DNS: {DNS}")


while True:
    # Our main loop where we have the server poll for incoming requests
    # Could do any other background tasks here, like reading sensors
    if time.time() > time_external_ping + check_interval and time.time() > time_delay + check_interval:
        time_delay = time.time()
        check_connectivity()

Behavior

After 30 pings, the board hard faults into safe mode.
The same behavior happens when using larger time intervals between pings.

Description

No response

Additional information

Nothing else is attached to the board.

@anecdata
Copy link
Member

anecdata commented Dec 17, 2021

I've reproduced your result (safe mode after about 15 loops).

But this ran a few hundred loops without issue:

import supervisor
import time
import random
import wifi
import socketpool
import ipaddress
from secrets import secrets

SERVERS = ("1.1.1.1", "1.0.0.1",
           "1.1.1.2", "1.0.0.2",
           "8.8.8.8", "8.8.4.4",
           "9.9.9.9", "149.112.112.112",
           "208.67.220.220", "208.67.222.222",
           "208.67.220.123", "208.67.222.123")

print("Connecting...")
wifi.radio.connect(secrets['ssid'],secrets['password'])
pool = socketpool.SocketPool(wifi.radio)

loop = 0
while True:
    ipv4 = wifi.radio.ipv4_gateway
    result = wifi.radio.ping(ipv4)
    print(f'LAN {loop:>3} {supervisor.ticks_ms():>9} {str(ipv4):15}     {result}s')

    port = random.choice((53, 80, 443))
    # ipv4 = ipaddress.ip_address(random.choice(SERVERS))  # this is fine if all SERVERS are IP addresses
    ipv4 = ipaddress.ip_address(pool.getaddrinfo(random.choice(SERVERS), port)[0][4][0])
    result = wifi.radio.ping(ipv4)
    print(f'WAN {loop:>3} {supervisor.ticks_ms():>9} {str(ipv4):15} {port:>3} {result}s')
    print()
    loop += 1
    time.sleep(1)

I'm not sure at this point what would account for the difference.

@BlueBackbite
Copy link
Author

I would think it's not ping then, perhaps something with the global variables being set in-loop or issues with time?

If I remove the global variables and just have the function return True or False, it crashes in 2 loops or 4 pings.

I just got into python. I have no clue.

@anecdata
Copy link
Member

Can you try to see what minimal example still produces the hard fault? Get rid of everything not essential to the demo.

@BlueBackbite
Copy link
Author

It hard faults with this code very fast

import ipaddress
import wifi

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

modemIP = ipaddress.ip_address("192.168.100.1")
# count the number of pings
ping_counter = 0

print("Connect wifi")
wifi.radio.connect(secrets['ssid'],secrets['password'])

def check_connectivity():
    ping_result = wifi.radio.ping(modemIP)
    if ping_result is None:
        print("ping modem fail")
    else:
        print("Was able to ping modem, should be good to go!")

while True:
    ping_counter += 1
    print("loops",ping_counter)
    check_connectivity()

But if you remove the function completely, it simply hangs:

import ipaddress
import wifi

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

modemIP = ipaddress.ip_address("192.168.100.1")
# count the number of pings
ping_counter = 0

print("Connect wifi")
wifi.radio.connect(secrets['ssid'],secrets['password'])

while True:
    ping_counter += 1
    print("loops",ping_counter)
    ping_result = wifi.radio.ping(modemIP)
    if ping_result is None:
        print("ping modem fail")
    else:
        print("Was able to ping modem, should be good to go!")

@tannewt tannewt added crash espressif applies to multiple Espressif chips labels Dec 20, 2021
@tannewt tannewt added this to the 7.x.x milestone Dec 20, 2021
@dhalbert
Copy link
Collaborator

dhalbert commented Feb 14, 2022

Closing in favor of #5980 (dupe).

@anecdata
Copy link
Member

Sorry, forgot about this one, I should have searched first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug crash espressif applies to multiple Espressif chips
Projects
None yet
Development

No branches or pull requests

4 participants