|
| 1 | +# Example of using the Adafruit IO CircuitPython MQTT client |
| 2 | +# to subscribe to an Adafruit IO feed and publish random data |
| 3 | +# to be received by the feed. |
| 4 | +# |
| 5 | +# Example by Tony DiCola for Adafruit Industries |
| 6 | +# Modified by Brent Rubell for Adafruit Industries, 2019 |
| 7 | +import time |
| 8 | +from random import randint |
| 9 | + |
| 10 | +import board |
| 11 | +import busio |
| 12 | +from digitalio import DigitalInOut |
| 13 | + |
| 14 | +from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K |
| 15 | +import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket |
| 16 | +from adafruit_io.adafruit_io import IO_MQTT |
| 17 | +import adafruit_minimqtt as MQTT |
| 18 | + |
| 19 | +# Get MQTT details and more from a secrets.py file |
| 20 | +try: |
| 21 | + from secrets import secrets |
| 22 | +except ImportError: |
| 23 | + print("MQTT secrets are kept in secrets.py, please add them there!") |
| 24 | + raise |
| 25 | + |
| 26 | +cs = DigitalInOut(board.D10) |
| 27 | +spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) |
| 28 | + |
| 29 | +# Initialize ethernet interface with DHCP |
| 30 | +eth = WIZNET5K(spi_bus, cs) |
| 31 | + |
| 32 | +# Define callback functions which will be called when certain events happen. |
| 33 | +# pylint: disable=unused-argument |
| 34 | +def connected(client): |
| 35 | + # Connected function will be called when the client is connected to Adafruit IO. |
| 36 | + # This is a good place to subscribe to feed changes. The client parameter |
| 37 | + # passed to this function is the Adafruit IO MQTT client so you can make |
| 38 | + # calls against it easily. |
| 39 | + print("Connected to Adafruit IO! Listening for DemoFeed changes...") |
| 40 | + # Subscribe to changes on a feed named DemoFeed. |
| 41 | + client.subscribe("DemoFeed") |
| 42 | + |
| 43 | +def subscribe(client, userdata, topic, granted_qos): |
| 44 | + # This method is called when the client subscribes to a new feed. |
| 45 | + print('Subscribed to {0} with QOS level {1}'.format(topic, granted_qos)) |
| 46 | + |
| 47 | +def unsubscribe(client, userdata, topic, pid): |
| 48 | + # This method is called when the client unsubscribes from a feed. |
| 49 | + print('Unsubscribed from {0} with PID {1}'.format(topic, pid)) |
| 50 | + |
| 51 | +# pylint: disable=unused-argument |
| 52 | +def disconnected(client): |
| 53 | + # Disconnected function will be called when the client disconnects. |
| 54 | + print("Disconnected from Adafruit IO!") |
| 55 | + |
| 56 | +# pylint: disable=unused-argument |
| 57 | +def message(client, feed_id, payload): |
| 58 | + # Message function will be called when a subscribed feed has a new value. |
| 59 | + # The feed_id parameter identifies the feed, and the payload parameter has |
| 60 | + # the new value. |
| 61 | + print("Feed {0} received new value: {1}".format(feed_id, payload)) |
| 62 | + |
| 63 | +# Initialize MQTT interface with the ethernet interface |
| 64 | +MQTT.set_socket(socket, eth) |
| 65 | + |
| 66 | +# Initialize a new MQTT Client object |
| 67 | +mqtt_client = MQTT.MQTT(broker="http://io.adafruit.com", |
| 68 | + username=secrets["aio_user"], |
| 69 | + password=secrets["aio_key"]) |
| 70 | + |
| 71 | +# Initialize an Adafruit IO MQTT Client |
| 72 | +io = IO_MQTT(mqtt_client) |
| 73 | + |
| 74 | +# Connect the callback methods defined above to Adafruit IO |
| 75 | +io.on_connect = connected |
| 76 | +io.on_disconnect = disconnected |
| 77 | +io.on_subscribe = subscribe |
| 78 | +io.on_unsubscribe = unsubscribe |
| 79 | +io.on_message = message |
| 80 | + |
| 81 | +# Connect to Adafruit IO |
| 82 | +print("Connecting to Adafruit IO...") |
| 83 | +io.connect() |
| 84 | + |
| 85 | +# Below is an example of manually publishing a new value to Adafruit IO. |
| 86 | +last = 0 |
| 87 | +print("Publishing a new message every 10 seconds...") |
| 88 | +while True: |
| 89 | + # Explicitly pump the message loop. |
| 90 | + io.loop() |
| 91 | + # Send a new message every 10 seconds. |
| 92 | + if (time.monotonic() - last) >= 5: |
| 93 | + value = randint(0, 100) |
| 94 | + print("Publishing {0} to DemoFeed.".format(value)) |
| 95 | + io.publish("DemoFeed", value) |
| 96 | + last = time.monotonic() |
0 commit comments