|
| 1 | +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +import time |
| 4 | +from random import randint |
| 5 | + |
| 6 | +import ssl |
| 7 | +import socketpool |
| 8 | +import wifi |
| 9 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 10 | +from adafruit_io.adafruit_io import IO_MQTT |
| 11 | + |
| 12 | +### WiFi ### |
| 13 | + |
| 14 | +# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and |
| 15 | +# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other |
| 16 | +# source control. |
| 17 | +# pylint: disable=no-name-in-module,wrong-import-order |
| 18 | +try: |
| 19 | + from secrets import secrets |
| 20 | +except ImportError: |
| 21 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 22 | + raise |
| 23 | + |
| 24 | +# Set your Adafruit IO Username and Key in secrets.py |
| 25 | +# (visit io.adafruit.com if you need to create an account, |
| 26 | +# or if you need your Adafruit IO key.) |
| 27 | +aio_username = secrets["aio_username"] |
| 28 | +aio_key = secrets["aio_key"] |
| 29 | + |
| 30 | +print("Connecting to %s" % secrets["ssid"]) |
| 31 | +wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 32 | +print("Connected to %s!" % secrets["ssid"]) |
| 33 | + |
| 34 | +# Define callback functions which will be called when certain events happen. |
| 35 | +# pylint: disable=unused-argument |
| 36 | +def connected(client): |
| 37 | + # Connected function will be called when the client is connected to Adafruit IO. |
| 38 | + # This is a good place to subscribe to feed changes. The client parameter |
| 39 | + # passed to this function is the Adafruit IO MQTT client so you can make |
| 40 | + # calls against it easily. |
| 41 | + print("Connected to Adafruit IO! Listening for DemoFeed changes...") |
| 42 | + # Subscribe to changes on a feed named DemoFeed. |
| 43 | + client.subscribe("DemoFeed") |
| 44 | + |
| 45 | + |
| 46 | +def subscribe(client, userdata, topic, granted_qos): |
| 47 | + # This method is called when the client subscribes to a new feed. |
| 48 | + print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos)) |
| 49 | + |
| 50 | + |
| 51 | +def unsubscribe(client, userdata, topic, pid): |
| 52 | + # This method is called when the client unsubscribes from a feed. |
| 53 | + print("Unsubscribed from {0} with PID {1}".format(topic, pid)) |
| 54 | + |
| 55 | + |
| 56 | +# pylint: disable=unused-argument |
| 57 | +def disconnected(client): |
| 58 | + # Disconnected function will be called when the client disconnects. |
| 59 | + print("Disconnected from Adafruit IO!") |
| 60 | + |
| 61 | + |
| 62 | +# pylint: disable=unused-argument |
| 63 | +def message(client, feed_id, payload): |
| 64 | + # Message function will be called when a subscribed feed has a new value. |
| 65 | + # The feed_id parameter identifies the feed, and the payload parameter has |
| 66 | + # the new value. |
| 67 | + print("Feed {0} received new value: {1}".format(feed_id, payload)) |
| 68 | + |
| 69 | + |
| 70 | +# Create a socket pool |
| 71 | +pool = socketpool.SocketPool(wifi.radio) |
| 72 | + |
| 73 | +# Initialize a new MQTT Client object |
| 74 | +mqtt_client = MQTT.MQTT( |
| 75 | + broker="io.adafruit.com", |
| 76 | + username=secrets["aio_username"], |
| 77 | + password=secrets["aio_key"], |
| 78 | + socket_pool=pool, |
| 79 | + ssl_context=ssl.create_default_context(), |
| 80 | +) |
| 81 | + |
| 82 | +# Initialize an Adafruit IO MQTT Client |
| 83 | +io = IO_MQTT(mqtt_client) |
| 84 | + |
| 85 | +# Connect the callback methods defined above to Adafruit IO |
| 86 | +io.on_connect = connected |
| 87 | +io.on_disconnect = disconnected |
| 88 | +io.on_subscribe = subscribe |
| 89 | +io.on_unsubscribe = unsubscribe |
| 90 | +io.on_message = message |
| 91 | + |
| 92 | +# Connect to Adafruit IO |
| 93 | +print("Connecting to Adafruit IO...") |
| 94 | +io.connect() |
| 95 | + |
| 96 | +# Below is an example of manually publishing a new value to Adafruit IO. |
| 97 | +last = 0 |
| 98 | +print("Publishing a new message every 10 seconds...") |
| 99 | +while True: |
| 100 | + # Explicitly pump the message loop. |
| 101 | + io.loop() |
| 102 | + # Send a new message every 10 seconds. |
| 103 | + if (time.monotonic() - last) >= 5: |
| 104 | + value = randint(0, 100) |
| 105 | + print("Publishing {0} to DemoFeed.".format(value)) |
| 106 | + io.publish("DemoFeed", value) |
| 107 | + last = time.monotonic() |
0 commit comments