Skip to content

examples: Add async WiFi connection example. #76

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

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions examples/micropython_async_wifi.py
Original file line number Diff line number Diff line change
@@ -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()
18 changes: 9 additions & 9 deletions examples/micropython.py → examples/micropython_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down