Skip to content

Commit 3a1e1de

Browse files
authored
Merge pull request #76 from arduino/update_examples
examples: Add async WiFi connection example.
2 parents fe58cff + 8c9988d commit 3a1e1de

File tree

2 files changed

+93
-9
lines changed

2 files changed

+93
-9
lines changed

examples/micropython_async_wifi.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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()

examples/micropython.py renamed to examples/micropython_basic.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Any copyright is dedicated to the Public Domain.
33
# https://creativecommons.org/publicdomain/zero/1.0/
44
import time
5-
import ssl
5+
import ssl # noqa
66
import network
77
import logging
88
from time import strftime
@@ -11,7 +11,7 @@
1111
from arduino_iot_cloud import Schedule
1212
from arduino_iot_cloud import ColoredLight
1313
from arduino_iot_cloud import Task
14-
from arduino_iot_cloud import CADATA
14+
from arduino_iot_cloud import CADATA # noqa
1515
from random import uniform
1616
from secrets import WIFI_SSID
1717
from secrets import WIFI_PASS
@@ -75,13 +75,13 @@ def wifi_connect():
7575
# Create a client object to connect to the Arduino IoT cloud.
7676
# For MicroPython, the key and cert files must be stored in DER format on the filesystem.
7777
# Alternatively, a username and password can be used to authenticate:
78-
# client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY)
79-
client = ArduinoCloudClient(
80-
device_id=DEVICE_ID,
81-
ssl_params={
82-
"keyfile": KEY_PATH, "certfile": CERT_PATH, "cadata": CADATA, "cert_reqs": ssl.CERT_REQUIRED
83-
}
84-
)
78+
client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY)
79+
# client = ArduinoCloudClient(
80+
# device_id=DEVICE_ID,
81+
# ssl_params={
82+
# "keyfile": KEY_PATH, "certfile": CERT_PATH, "cadata": CADATA, "cert_reqs": ssl.CERT_REQUIRED
83+
# }
84+
# )
8585

8686
# Register cloud objects.
8787
# Note: The following objects must be created first in the dashboard and linked to the device.

0 commit comments

Comments
 (0)