|
| 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 | +import logging |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import asyncio |
| 8 | +from arduino_iot_cloud import ArduinoCloudClient |
| 9 | +import argparse |
| 10 | +import arduino_iot_cloud.ussl as ssl |
| 11 | + |
| 12 | + |
| 13 | +def exception_handler(loop, context): |
| 14 | + pass |
| 15 | + |
| 16 | + |
| 17 | +def on_value_changed(client, value): |
| 18 | + logging.info(f"The answer to life, the universe, and everything is {value}") |
| 19 | + loop = asyncio.get_event_loop() |
| 20 | + loop.set_exception_handler(exception_handler) |
| 21 | + sys.exit(0) |
| 22 | + |
| 23 | + |
| 24 | +if __name__ == "__main__": |
| 25 | + # Parse command line args. |
| 26 | + parser = argparse.ArgumentParser(description="arduino_iot_cloud.py") |
| 27 | + parser.add_argument( |
| 28 | + "-d", "--debug", action="store_true", help="Enable debugging messages" |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + "-c", "--crypto-device", action="store_true", help="Use crypto device" |
| 32 | + ) |
| 33 | + args = parser.parse_args() |
| 34 | + |
| 35 | + # Configure the logger. |
| 36 | + # All message equal or higher to the logger level are printed. |
| 37 | + # To see more debugging messages, pass --debug on the command line. |
| 38 | + logging.basicConfig( |
| 39 | + datefmt="%H:%M:%S", |
| 40 | + format="%(asctime)s.%(msecs)03d %(message)s", |
| 41 | + level=logging.DEBUG if args.debug else logging.INFO, |
| 42 | + ) |
| 43 | + |
| 44 | + # Create a client object to connect to the Arduino IoT cloud. |
| 45 | + # To use a secure element, set the token's "pin" and URI in "keyfile" and "certfile", and |
| 46 | + # the CA certificate (if any) in "ssl_params". Alternatively, a username and password can |
| 47 | + # be used to authenticate, for example: |
| 48 | + # client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY) |
| 49 | + if args.crypto_device: |
| 50 | + client = ArduinoCloudClient( |
| 51 | + device_id=os.environ["DEVICE_ID"], |
| 52 | + ssl_params={ |
| 53 | + "pin": "1234", |
| 54 | + "keyfile": "pkcs11:token=arduino", |
| 55 | + "certfile": "pkcs11:token=arduino", |
| 56 | + "ca_certs": "ca-root.pem", |
| 57 | + "cert_reqs": ssl.CERT_REQUIRED, |
| 58 | + "engine_path": "/lib/x86_64-linux-gnu/engines-3/libpkcs11.so", |
| 59 | + "module_path": "/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so", |
| 60 | + }, |
| 61 | + ) |
| 62 | + else: |
| 63 | + client = ArduinoCloudClient( |
| 64 | + device_id=os.environ["DEVICE_ID"], |
| 65 | + username=os.environ["DEVICE_ID"], |
| 66 | + password=os.environ["SECRET_KEY"], |
| 67 | + ) |
| 68 | + |
| 69 | + # Register cloud objects. |
| 70 | + # Note: The following objects must be created first in the dashboard and linked to the device. |
| 71 | + # This cloud object is initialized with its last known value from the cloud. When this object is updated |
| 72 | + # from the dashboard, the on_switch_changed function is called with the client object and the new value. |
| 73 | + client.register("answer", value=None, on_write=on_value_changed) |
| 74 | + |
| 75 | + # Start the Arduino IoT cloud client. |
| 76 | + client.start() |
0 commit comments