diff --git a/examples/micropython_async_wifi.py b/examples/micropython_async_wifi.py new file mode 100644 index 0000000..60713e4 --- /dev/null +++ b/examples/micropython_async_wifi.py @@ -0,0 +1,84 @@ +# This file is part of the Python Arduino IoT Cloud. +# Any copyright is dedicated to the Public Domain. +# https://creativecommons.org/publicdomain/zero/1.0/ + +import time +import logging +from time import strftime +from machine import Pin + +from secrets import DEVICE_ID +from secrets import SECRET_KEY + +from arduino_iot_cloud import Task +from arduino_iot_cloud import ArduinoCloudClient +from arduino_iot_cloud import async_wifi_connection + + +def read_temperature(client): + return 50.0 + + +def read_humidity(client): + return 100.0 + + +def on_switch_changed(client, value): + led = Pin("LED_BLUE", Pin.OUT) + # Note the LED is usually inverted + led.value(not value) + # Update the value of the led cloud variable. + client["led"] = value + + +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, + ) + + # 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=SECRET_KEY + ) + + # 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("sw1", 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) + + # This is a periodic cloud object that gets updated at fixed intervals (in this case 1 seconed) with the + # value returned from its on_read function (a formatted string of the current time). Note this object's + # initial value is None, it will be initialized by calling the on_read function. + client.register( + "clk", + value=None, + on_read=lambda x: strftime("%H:%M:%S", time.localtime()), + interval=1.0, + ) + + # Register some sensor readings. + client.register("humidity", value=None, on_read=read_humidity, interval=1.0) + client.register("temperature", value=None, on_read=read_temperature, interval=1.0) + + # This function is registered as a background task to reconnect to WiFi if it ever gets + # disconnected. Note, it can also be used for the initial WiFi connection, in synchronous + # mode, if it's called without any args (i.e, async_wifi_connection()) at the beginning of + # this script. + client.register( + Task("wifi_connection", on_run=async_wifi_connection, interval=60.0) + ) + + # Start the Arduino IoT cloud client. + client.start() diff --git a/examples/micropython.py b/examples/micropython_basic.py similarity index 93% rename from examples/micropython.py rename to examples/micropython_basic.py index da8bd3d..f9b2e49 100644 --- a/examples/micropython.py +++ b/examples/micropython_basic.py @@ -2,7 +2,7 @@ # Any copyright is dedicated to the Public Domain. # https://creativecommons.org/publicdomain/zero/1.0/ import time -import ssl +import ssl # noqa import network import logging from time import strftime @@ -11,7 +11,7 @@ from arduino_iot_cloud import Schedule from arduino_iot_cloud import ColoredLight from arduino_iot_cloud import Task -from arduino_iot_cloud import CADATA +from arduino_iot_cloud import CADATA # noqa from random import uniform from secrets import WIFI_SSID from secrets import WIFI_PASS @@ -75,13 +75,13 @@ def 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=SECRET_KEY) - client = ArduinoCloudClient( - device_id=DEVICE_ID, - ssl_params={ - "keyfile": KEY_PATH, "certfile": CERT_PATH, "cadata": CADATA, "cert_reqs": ssl.CERT_REQUIRED - } - ) + client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY) + # client = ArduinoCloudClient( + # device_id=DEVICE_ID, + # ssl_params={ + # "keyfile": KEY_PATH, "certfile": CERT_PATH, "cadata": CADATA, "cert_reqs": ssl.CERT_REQUIRED + # } + # ) # Register cloud objects. # Note: The following objects must be created first in the dashboard and linked to the device.