forked from arduino/arduino-iot-cloud-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
104 lines (84 loc) · 5 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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 arduino_iot_cloud import ArduinoCloudClient
from arduino_iot_cloud import Location
from arduino_iot_cloud import Schedule
from arduino_iot_cloud import ColoredLight
from arduino_iot_cloud import Task
from random import uniform
import argparse
import arduino_iot_cloud.ussl as ssl
KEY_PATH = "pkcs11:token=arduino"
CERT_PATH = "pkcs11:token=arduino"
CA_PATH = "ca-root.pem"
DEVICE_ID = b"fcbb21b8-b5b8-4961-a89f-a3f1abda7957"
def on_switch_changed(client, value):
# This is a write callback for the switch that toggles the LED variable. The LED
# variable can be accessed via the client object passed in the first argument.
client["led"] = value
def on_clight_changed(client, clight):
logging.info(f"ColoredLight changed. Swi: {clight.swi} Bri: {clight.bri} Sat: {clight.sat} Hue: {clight.hue}")
def user_task(client):
# NOTE: this function should not block.
# This is a user-defined task that updates the colored light. Note any registered
# cloud object can be accessed using the client object passed to this function.
# The composite ColoredLight object fields can be assigned to individually, using dot:
client["clight"].hue = round(uniform(0, 100), 1)
client["clight"].bri = round(uniform(0, 100), 1)
if __name__ == "__main__":
# Parse command line args.
parser = argparse.ArgumentParser(description="arduino_iot_cloud.py")
parser.add_argument("-d", "--debug", action="store_true", help="Enable debugging messages")
args = parser.parse_args()
# Assume the host has an active Internet connection.
# Configure the logger.
# All message equal or higher to the logger level are printed.
# To see more debugging messages, pass --debug on the command line.
logging.basicConfig(
datefmt="%H:%M:%S",
format="%(asctime)s.%(msecs)03d %(message)s",
level=logging.DEBUG if args.debug else logging.INFO,
)
# Create a client object to connect to the Arduino IoT cloud.
# To use a secure element, set the token's "pin" and URI in "keyfile" and "certfile", and
# the CA certificate (if any) in "ssl_params". Alternatively, a username and password can
# be used to authenticate, for example:
# client = ArduinoCloudClient(device_id=b"DEVICE_ID", username=b"DEVICE_ID", password=b"SECRET_KEY")
client = ArduinoCloudClient(
device_id=DEVICE_ID,
ssl_params={
"pin": "1234",
"keyfile": KEY_PATH, "certfile": CERT_PATH, "ca_certs": CA_PATH, "cert_reqs": ssl.CERT_REQUIRED
},
)
# 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.
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)
# This is an example of a composite cloud object (a cloud object with multiple variables). In this case
# a colored light with switch, hue, saturation and brightness attributes. Once initialized, the object's
# attributes can be accessed using dot notation. For example: client["clight"].swi = False.
client.register(ColoredLight("clight", swi=True, on_write=on_clight_changed))
# This is another example of a composite cloud object, a map location with lat and long attributes.
client.register(Location("treasureisland", lat=31.264694, lon=29.979987))
# This object allows scheduling recurring events from the cloud UI. On activation of the event, if the
# on_active callback is provided, it gets called with the client object and the schedule object value.
# Note: The activation status of the object can also be polled using client["schedule"].active.
client.register(Schedule("schedule", on_active=lambda client, value: logging.info(f"Schedule activated {value}!")))
# The client can also schedule user code in a task and run it along with the other cloud objects.
# To schedule a user function, use the Task object and pass the task name and function in "on_run"
# to client.register().
client.register(Task("user_task", on_run=user_task, interval=1.0))
# Start the Arduino IoT cloud client.
client.start()