Skip to content

Safemode entered because of WDT trigger instead of resetting the ESP32 #10011

@jfabernathy

Description

@jfabernathy

CircuitPython version

CP 9.2.3 on a Adafruit ESP32 Huzzah

Code/REPL

# The version is for the ESP32-S2 Feather with AHTx0 connected to onboard
# STEMMA-QT connector.
#
import os
import adafruit_connection_manager
import socketpool
import ssl
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import wifi
import time
import board
import digitalio
import alarm
import busio
import adafruit_ahtx0

PUBLISH_DELAY = 60
MQTT_TOPIC = "environmentTopic"
USE_DEEP_SLEEP = True

from microcontroller import watchdog as w
from watchdog import WatchDogMode
w.timeout = 70  # Set a timeout of 70 seconds
w.mode = WatchDogMode.RESET
w.feed()


def alarm_deepSleep(how_long):
    time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + how_long)
    alarm.exit_and_deep_sleep_until_alarms(time_alarm)


def debug_log(msg):
    print(msg)


def post_data(msg):
    try:
        mqtt_client.connect()
        mqtt_client.publish(MQTT_TOPIC, msg)
        mqtt_client.disconnect()
    except Exception as e:
        print("Exception in post_data ", str(e))
        alarm_deepSleep(2)

# Create sensor objects, using the board's default I2C bus.
i2c = board.I2C()

sensor = adafruit_ahtx0.AHTx0(i2c)

print(f"My MAC address: {[hex(i) for i in wifi.radio.mac_address]}")
print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}")
print(f"My IP address: {wifi.radio.ipv4_address}")

mqtt_username = os.getenv("mqtt_name")
mqtt_password = os.getenv("mqtt_password")

# Create a socket pool
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
    broker=os.getenv('mqtt_broker'),
    port=os.getenv('mqtt_port'),
    username=mqtt_username,
    password=mqtt_password,
    socket_pool=pool,
    ssl_context=ssl_context,
)

x = round(sensor.temperature * 1.8 + 32.0, 1)
y = round(sensor.relative_humidity, 1)
temp = str(x)
humid = str(y)

loop_count = alarm.sleep_memory[5] | alarm.sleep_memory[6] << 8
loop_count += 1
alarm.sleep_memory[6] = loop_count >> 8
alarm.sleep_memory[5] = loop_count & 255

station = '"' + os.getenv("location") + '"'

# format the readings data as  InfluxDB expects: Array of 2 object:
# 1st is readings, 2nd is tag

data = (
    """[{"Temperature": """
    + temp
    + """ , "Humidity": """
    + humid
    + """,  "Loop Count": """
    + str(loop_count)
    + """}, {"Location": """
    + station
    + """}]"""
)
post_data(data)
debug_log(data)

debug_log("Just posted data, now into deep sleep!\n\n")

w.feed()

# Create an alarm that will trigger 60 seconds from now to awake from deep sleep
alarm_deepSleep(60)

Behavior

The code runs and deep sleeps for 60 seconds. There is a WDT set to 70 seconds to reset the device if triggered. I noticed that when the board was running just connected to a power adapter that after some hours of running I would find the red LED flashing slowly. I connected the ESP32 Hazzah Feather to a Linux PC running Thonny and when I'd see the red LED flashing, I see on Thonny the following.

rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:396
ho 0 tail 12 room 4
load:0x40078000,len:13892
load:0x40080400,len:4
load:0x40080404,len:3160
entry 0x40080558
Serial console setup

Auto-reload is off.
Running in safe mode! Not running saved code.

You are in safe mode because:
Internal watchdog timer expired.
Press reset to exit safe mode.

I do not have this problem with the same code on a ESP32-s2 Feather. However, I don't know that I've ever trigger the WDT reset on the EPS32-S2. I have a loop counter in Static RAM that gets incremented. It only gets reset when the microcontroller is reset and not coming out of Deep Sleep.

Description

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions