|
| 1 | +# This file is part of the Python Arduino IoT Cloud. |
| 2 | +# Any copyright is dedicated to the Public Domain. |
| 3 | +# https://creativecommons.org/publicdomain/zero/1.0/ |
| 4 | + |
| 5 | +import time |
| 6 | +import logging |
| 7 | +from time import strftime |
| 8 | +from machine import Pin |
| 9 | + |
| 10 | +from secrets import DEVICE_ID |
| 11 | +from secrets import SECRET_KEY |
| 12 | + |
| 13 | +from arduino_iot_cloud import Task |
| 14 | +from arduino_iot_cloud import ArduinoCloudClient |
| 15 | +from arduino_iot_cloud import async_wifi_connection |
| 16 | + |
| 17 | + |
| 18 | +def read_temperature(client): |
| 19 | + return 50.0 |
| 20 | + |
| 21 | + |
| 22 | +def read_humidity(client): |
| 23 | + return 100.0 |
| 24 | + |
| 25 | + |
| 26 | +def on_switch_changed(client, value): |
| 27 | + led = Pin("LED_BLUE", Pin.OUT) |
| 28 | + # Note the LED is usually inverted |
| 29 | + led.value(not value) |
| 30 | + # Update the value of the led cloud variable. |
| 31 | + client["led"] = value |
| 32 | + |
| 33 | + |
| 34 | +if __name__ == "__main__": |
| 35 | + # Configure the logger. |
| 36 | + # All message equal or higher to the logger level are printed. |
| 37 | + # To see more debugging messages, set level=logging.DEBUG. |
| 38 | + logging.basicConfig( |
| 39 | + datefmt="%H:%M:%S", |
| 40 | + format="%(asctime)s.%(msecs)03d %(message)s", |
| 41 | + level=logging.INFO, |
| 42 | + ) |
| 43 | + |
| 44 | + # Create a client object to connect to the Arduino IoT cloud. |
| 45 | + # For MicroPython, the key and cert files must be stored in DER format on the filesystem. |
| 46 | + # Alternatively, a username and password can be used to authenticate: |
| 47 | + client = ArduinoCloudClient( |
| 48 | + device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY |
| 49 | + ) |
| 50 | + |
| 51 | + # Register cloud objects. |
| 52 | + # Note: The following objects must be created first in the dashboard and linked to the device. |
| 53 | + # This cloud object is initialized with its last known value from the cloud. When this object is updated |
| 54 | + # from the dashboard, the on_switch_changed function is called with the client object and the new value. |
| 55 | + client.register("sw1", value=None, on_write=on_switch_changed, interval=0.250) |
| 56 | + |
| 57 | + # This cloud object is updated manually in the switch's on_write_change callback to update |
| 58 | + # the LED state in the cloud. |
| 59 | + client.register("led", value=None) |
| 60 | + |
| 61 | + # This is a periodic cloud object that gets updated at fixed intervals (in this case 1 seconed) with the |
| 62 | + # value returned from its on_read function (a formatted string of the current time). Note this object's |
| 63 | + # initial value is None, it will be initialized by calling the on_read function. |
| 64 | + client.register( |
| 65 | + "clk", |
| 66 | + value=None, |
| 67 | + on_read=lambda x: strftime("%H:%M:%S", time.localtime()), |
| 68 | + interval=1.0, |
| 69 | + ) |
| 70 | + |
| 71 | + # Register some sensor readings. |
| 72 | + client.register("humidity", value=None, on_read=read_humidity, interval=1.0) |
| 73 | + client.register("temperature", value=None, on_read=read_temperature, interval=1.0) |
| 74 | + |
| 75 | + # This function is registered as a background task to reconnect to WiFi if it ever gets |
| 76 | + # disconnected. Note, it can also be used for the initial WiFi connection, in synchronous |
| 77 | + # mode, if it's called without any args (i.e, async_wifi_connection()) at the beginning of |
| 78 | + # this script. |
| 79 | + client.register( |
| 80 | + Task("wifi_connection", on_run=async_wifi_connection, interval=60.0) |
| 81 | + ) |
| 82 | + |
| 83 | + # Start the Arduino IoT cloud client. |
| 84 | + client.start() |
0 commit comments