Skip to content

Script hangs after 12 hours with 1-second refresh #72

@Bodobolero

Description

@Bodobolero

I am running the following script on Arduino RP2040 Connect with Arduino IoT Cloud.
The script reads temperature and humidity from DHT20 over I2C using https://files.seeedstudio.com/wiki/Grove-Temperature-Humidity-Sensor/Pico-micropython-master.zip dht20.py.

The script runs fine for about 12 hours (about 40.000 loop iterations) and then gets stuck.
I suspect this is due to heap fragmentation in ArduinoIoTCloud python module or other module it uses.

It would be helpful to add a watchdog like in the C implementation that reboots the board in case of becoming unresponsive.

Here is my script to repro the problem

from machine import I2C, Pin
from dht20 import DHT20
import time
import network
import logging
from arduino_iot_cloud import ArduinoCloudClient

from secrets import WIFI_SSID
from secrets import WIFI_PASSWORD
from secrets import DEVICE_ID
from secrets import CLOUD_PASSWORD

led = Pin("LED", Pin.OUT)  # Configure the desired LED pin as an output.
i2c = I2C(0, scl=Pin(13), sda=Pin(12))
dht20 = DHT20(i2c)


def on_switch_changed(client, value):
    # Toggles the hardware LED on or off.
    led.value(value)

    # Sets the value of the cloud variable "led" to the current state of the LED
    # and thus mirrors the hardware state in the cloud.
    client["led"] = value

def read_temperature(client):
    global dht20
    temperature = dht20.dht20_temperature()
    logging.info(f"DHT20: {temperature} C")
    return temperature

def read_humidity(client):
    global dht20
    humidity = dht20.dht20_humidity()
    logging.info(f"DHT20: {humidity} %")
    return humidity
    

def wifi_connect():
    if not WIFI_SSID or not WIFI_PASSWORD:
        raise (
            Exception("Network is not configured. Set SSID and passwords in secrets.py"))
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(WIFI_SSID, WIFI_PASSWORD)
    while not wlan.isconnected():
        logging.info("Trying to connect. Note this may take a while...")
        time.sleep_ms(500)
    logging.info(f"WiFi Connected {wlan.ifconfig()}")


if __name__ == "__main__":

    # Configure the logger.
    # All message equal or higher to the logger level are printed.
    # To see more debugging messages, set level=logging.DEBUG.
    logging.basicConfig(
        datefmt="%H:%M:%S",
        format="%(asctime)s.%(msecs)03d %(message)s",
        level=logging.INFO,
    )

    logging.info(f"Connect to WiFi")

    # NOTE: Add networking code here or in boot.py
    wifi_connect()

    # Create a client object to connect to the Arduino IoT cloud.
    # For MicroPython, the key and cert files must be stored in DER format on the filesystem.
    # Alternatively, a username and password can be used to authenticate:
    client = ArduinoCloudClient(
        device_id=DEVICE_ID, username=DEVICE_ID, password=CLOUD_PASSWORD)

    # Register cloud objects.
    # Note: The following objects must be created first in the dashboard and linked to the device.
    # This cloud object is initialized with its last known value from the cloud. When this object is updated
    # from the dashboard, the on_switch_changed function is called with the client object and the new value.
    client.register("ledSwitch", value=None,
                    on_write=on_switch_changed, interval=0.250)

    # This cloud object is updated manually in the switch's on_write_change callback to update the LED state in the cloud.
    client.register("led", value=None)

    # read only variables temperature and humidity
    client.register("temperature", value=None, on_read=read_temperature, interval=1.0)
    client.register("humidity", value=None, on_read=read_humidity, interval=1.0)


    logging.info(f"starting IoT client loop")

    # Start the Arduino IoT cloud client.
    client.start()

Maybe the problem is in my script and not in the arduino-iot-cloud-py implementation - however so far I did not succeed in deploying a Arduino IoT cloud project in production in Micropython while I have several Arduino IoT cloud "things" successfully deployed already using the Arduino Cloud C/C++ libraries.

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