|
| 1 | +# SPDX-FileCopyrightText: 2023 ladyada for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import time |
| 5 | +import ssl |
| 6 | +import json |
| 7 | +import socketpool |
| 8 | +import wifi |
| 9 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 10 | +from adafruit_aws_iot import MQTT_CLIENT |
| 11 | + |
| 12 | +# Add a secrets.py to your filesystem that has a dictionary called "secrets". DO NOT share that |
| 13 | +# file or commit it into Git or other source control. The "secrets" dictionary should have the |
| 14 | +# following keys: |
| 15 | +# "ssid" - Your WiFi ssid |
| 16 | +# "password" - Your WiFi password |
| 17 | +# "device_cert_path" - Path to the Device Certificate from AWS IoT ("<THING_NAME>.cert.pem") |
| 18 | +# "device_key_path" - Path to the RSA Private Key from AWS IoT ("<THING_NAME>.private.key") |
| 19 | +# "broker" - The endpoint for the AWS IoT broker ("<PREFIX>.iot.<REGION>.amazonaws.com") |
| 20 | +# "port" - The port for the "broker" above (8883) |
| 21 | +# "client_id" - The client id. Your device's Policy needs to allow this client ("basicPubSub") |
| 22 | +# |
| 23 | +# pylint: disable=no-name-in-module,wrong-import-order |
| 24 | +try: |
| 25 | + from secrets import secrets |
| 26 | +except ImportError: |
| 27 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 28 | + raise |
| 29 | + |
| 30 | +### Code ### |
| 31 | + |
| 32 | +# Your device's Policy needs to allow this topic |
| 33 | +topic = "sdk/test/python" |
| 34 | + |
| 35 | + |
| 36 | +# Define callback methods which are called when events occur |
| 37 | +# pylint: disable=unused-argument, redefined-outer-name |
| 38 | +def connect(client, userdata, flags, rc): |
| 39 | + # This function will be called when the client is connected |
| 40 | + # successfully to the broker. |
| 41 | + print("Connected to MQTT Broker!") |
| 42 | + print("Flags: {0} - RC: {1}".format(flags, rc)) |
| 43 | + |
| 44 | + # Subscribe to topic circuitpython/aws |
| 45 | + print("Subscribing to topic {}".format(topic)) |
| 46 | + aws_iot.subscribe(topic) |
| 47 | + |
| 48 | + |
| 49 | +def disconnect(client, userdata, rc): |
| 50 | + # This method is called when the client disconnects |
| 51 | + # from the broker. |
| 52 | + print("Disconnected from MQTT Broker!") |
| 53 | + |
| 54 | + |
| 55 | +def subscribe(client, userdata, topic, granted_qos): |
| 56 | + # This method is called when the client subscribes to a new topic. |
| 57 | + print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos)) |
| 58 | + |
| 59 | + # Create a json-formatted message |
| 60 | + message = {"message": "Hello from AWS IoT CircuitPython"} |
| 61 | + # Publish message to topic |
| 62 | + aws_iot.publish(topic, json.dumps(message)) |
| 63 | + |
| 64 | + |
| 65 | +def unsubscribe(client, userdata, topic, pid): |
| 66 | + # This method is called when the client unsubscribes from a topic. |
| 67 | + print("Unsubscribed from {0} with PID {1}".format(topic, pid)) |
| 68 | + |
| 69 | + |
| 70 | +def publish(client, userdata, topic, pid): |
| 71 | + # This method is called when the client publishes data to a topic. |
| 72 | + print("Published to {0} with PID {1}".format(topic, pid)) |
| 73 | + |
| 74 | + |
| 75 | +def message(client, topic, msg): |
| 76 | + # This method is called when the client receives data from a topic. |
| 77 | + print("Message from {}: {}".format(topic, msg)) |
| 78 | + |
| 79 | + |
| 80 | +print("Connecting to %s" % secrets["ssid"]) |
| 81 | +wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 82 | +print("Connected to %s!" % secrets["ssid"]) |
| 83 | + |
| 84 | +# Create a socket pool |
| 85 | +pool = socketpool.SocketPool(wifi.radio) |
| 86 | +ssl_context = ssl.create_default_context() |
| 87 | + |
| 88 | +# Set AWS Device Certificate and AWS RSA Private Key |
| 89 | +ssl_context.load_cert_chain( |
| 90 | + certfile=secrets["device_cert_path"], keyfile=secrets["device_key_path"] |
| 91 | +) |
| 92 | + |
| 93 | +# Set up a MiniMQTT Client |
| 94 | +mqtt_client = MQTT.MQTT( |
| 95 | + broker=secrets["broker"], |
| 96 | + port=secrets["port"], |
| 97 | + is_ssl=True, # ssl is required |
| 98 | + client_id=secrets["client_id"], |
| 99 | + socket_pool=pool, |
| 100 | + ssl_context=ssl_context, |
| 101 | +) |
| 102 | + |
| 103 | +# Initialize AWS IoT MQTT API Client |
| 104 | +aws_iot = MQTT_CLIENT(mqtt_client) |
| 105 | + |
| 106 | +# Connect callback handlers to AWS IoT MQTT Client |
| 107 | +aws_iot.on_connect = connect |
| 108 | +aws_iot.on_disconnect = disconnect |
| 109 | +aws_iot.on_subscribe = subscribe |
| 110 | +aws_iot.on_unsubscribe = unsubscribe |
| 111 | +aws_iot.on_publish = publish |
| 112 | +aws_iot.on_message = message |
| 113 | + |
| 114 | +print("Attempting to connect to %s" % mqtt_client.broker) |
| 115 | +aws_iot.connect() |
| 116 | + |
| 117 | +# Start a blocking message loop... |
| 118 | +# NOTE: NO code below this loop will execute |
| 119 | +# NOTE: Network reconnection is NOT handled within this loop |
| 120 | +while True: |
| 121 | + aws_iot.loop() |
| 122 | + |
| 123 | + time.sleep(1) |
0 commit comments