Skip to content

Commit d24bbd5

Browse files
committed
Add simple MQTT subscription example.
1 parent 77ef090 commit d24bbd5

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

examples/mqtt_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from Adafruit_IO import MQTTClient
1111

1212

13-
# Set to your Adafruit IO key.
14-
ADAFRUIT_IO_KEY = 'YOUR ADAFRUIT IO KEY'
13+
# Set to your Adafruit IO key & username below.
14+
ADAFRUIT_IO_KEY = 'YOUR ADAFRUIT IO KEY'
1515
ADAFRUIT_IO_USERNAME = 'YOUR ADAFRUIT IO USERNAME' # See https://accounts.adafruit.com
1616
# to find your username.
1717

examples/mqtt_subscribe.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Example of using the MQTT client class to subscribe to a feed and print out
2+
# any changes made to the feed. Edit the variables below to configure the key,
3+
# username, and feed to subscribe to for changes.
4+
5+
# Import standard python modules.
6+
import sys
7+
8+
# Import Adafruit IO MQTT client.
9+
from Adafruit_IO import MQTTClient
10+
11+
12+
# Set to your Adafruit IO key & username below.
13+
ADAFRUIT_IO_KEY = 'YOUR ADAFRUIT IO KEY'
14+
ADAFRUIT_IO_USERNAME = 'YOUR ADAFRUIT IO USERNAME' # See https://accounts.adafruit.com
15+
# to find your username.
16+
17+
# Set to the ID of the feed to subscribe to for updates.
18+
FEED_ID = 'DemoFeed'
19+
20+
21+
# Define callback functions which will be called when certain events happen.
22+
def connected(client):
23+
# Connected function will be called when the client is connected to Adafruit IO.
24+
# This is a good place to subscribe to feed changes. The client parameter
25+
# passed to this function is the Adafruit IO MQTT client so you can make
26+
# calls against it easily.
27+
print 'Connected to Adafruit IO! Listening for {0} changes...'.format(FEED_ID)
28+
# Subscribe to changes on a feed named DemoFeed.
29+
client.subscribe(FEED_ID)
30+
31+
def disconnected(client):
32+
# Disconnected function will be called when the client disconnects.
33+
print 'Disconnected from Adafruit IO!'
34+
sys.exit(1)
35+
36+
def message(client, feed_id, payload):
37+
# Message function will be called when a subscribed feed has a new value.
38+
# The feed_id parameter identifies the feed, and the payload parameter has
39+
# the new value.
40+
print 'Feed {0} received new value: {1}'.format(feed_id, payload)
41+
42+
43+
# Create an MQTT client instance.
44+
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
45+
46+
# Setup the callback functions defined above.
47+
client.on_connect = connected
48+
client.on_disconnect = disconnected
49+
client.on_message = message
50+
51+
# Connect to the Adafruit IO server.
52+
client.connect()
53+
54+
# Start a message loop that blocks forever waiting for MQTT messages to be
55+
# received. Note there are other options for running the event loop like doing
56+
# so in a background thread--see the mqtt_client.py example to learn more.
57+
client.loop_blocking()

0 commit comments

Comments
 (0)