Skip to content

Commit 7d0b951

Browse files
author
brentru
committed
adding group subscriptions
1 parent da00252 commit 7d0b951

File tree

2 files changed

+80
-13
lines changed

2 files changed

+80
-13
lines changed

Adafruit_IO/mqtt_client.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,24 @@ def _mqtt_disconnect(self, client, userdata, rc):
9999

100100
def _mqtt_message(self, client, userdata, msg):
101101
logger.debug('Client on_message called.')
102-
# Parse out the feed id and call on_message callback.
103-
# Assumes topic looks like "username/feeds/id"
102+
"""Parse out the topic and call on_message callback
103+
assume topic looks like `username/topic/id`
104+
"""
104105
parsed_topic = msg.topic.split('/')
105106
if self.on_message is not None:
106-
feed = parsed_topic[2]
107+
topic = parsed_topic[2]
107108
payload = '' if msg.payload is None else msg.payload.decode('utf-8')
108109
elif self.on_message is not None and parsed_topic[0] == 'time':
109-
feed = parsed_topic[0]
110+
topic = parsed_topic[0]
111+
payload = msg.payload.decode('utf-8')
112+
elif self.on_message is not None and parsed_topic[1] == 'groups':
113+
topic = parsed_topic[3]
110114
payload = msg.payload.decode('utf-8')
111-
self.on_message(self, feed, payload)
115+
self.on_message(self, topic, payload)
112116

113117
def _mqtt_subscribe(client, userdata, mid, granted_qos):
114118
"""Called when broker responds to a subscribe request."""
115119

116-
117120
def connect(self, **kwargs):
118121
"""Connect to the Adafruit.IO service. Must be called before any loop
119122
or publish operations are called. Will raise an exception if a
@@ -179,15 +182,21 @@ def subscribe(self, feed_id, feed_user=None):
179182
the on_message function will be called with the feed_id and new value.
180183
181184
Params:
182-
- feed_id: The id of the feed to update.
183-
- feed_user (optional): The user id of the feed. Used for feed sharing.
185+
- feed_id: The id of the feed to subscribe to.
186+
- feed_user (optional): The user id of the feed. Used for feed sharing functionality.
184187
"""
185188
if feed_user is not None:
186189
(res, mid) = self._client.subscribe('{0}/feeds/{1}'.format(feed_user, feed_id))
187190
else:
188191
(res, mid) = self._client.subscribe('{0}/feeds/{1}'.format(self._username, feed_id))
189192
return res, mid
190193

194+
def subscribe_group(self, group_id):
195+
"""Subscribe to changes on the specified group. When the group is updated
196+
the on_message function will be called with the group_id and the new value.
197+
"""
198+
self._client.subscribe('{0}/groups/{1}'.format(self._username, group_id))
199+
191200
def subscribe_time(self, time):
192201
"""Subscribe to changes on the Adafruit IO time feeds. When the feed is
193202
updated, the on_message function will be called and publish a new value:
@@ -205,11 +214,11 @@ def subscribe_time(self, time):
205214
return
206215

207216
def unsubscribe(self, feed_id):
208-
"""Unsubscribes from a specified MQTT feed.
209-
Note: this does not prevent publishing to a feed, it will unsubscribe
210-
from receiving messages via on_message.
211-
"""
212-
(res, mid) = self._client.unsubscribe('{0}/feeds/{1}'.format(self._username, feed_id))
217+
"""Unsubscribes from a specified MQTT feed.
218+
Note: this does not prevent publishing to a feed, it will unsubscribe
219+
from receiving messages via on_message.
220+
"""
221+
(res, mid) = self._client.unsubscribe('{0}/feeds/{1}'.format(self._username, feed_id))
213222

214223
def publish(self, feed_id, value=None, feed_user=None):
215224
"""Publish a value to a specified feed.

examples/mqtt/mqtt_group_subscribe.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Example of using the MQTT client class to subscribe to and publish feed values.
2+
# Author: Tony DiCola
3+
4+
# Import standard python modules.
5+
import random
6+
import sys
7+
import time
8+
9+
# Import Adafruit IO MQTT client.
10+
from Adafruit_IO import MQTTClient
11+
12+
# Set to your Adafruit IO key.
13+
# Remember, your key is a secret,
14+
# so make sure not to publish it when you publish this code!
15+
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
16+
17+
# Set to your Adafruit IO username.
18+
# (go to https://accounts.adafruit.com to find your username)
19+
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
20+
21+
# name of the group to subscribe to changes on
22+
group_name = 'grouptest'
23+
24+
# Define callback functions which will be called when certain events happen.
25+
def connected(client):
26+
# Connected function will be called when the client is connected to Adafruit IO.
27+
# This is a good place to subscribe to feed changes. The client parameter
28+
# passed to this function is the Adafruit IO MQTT client so you can make
29+
# calls against it easily.
30+
# Subscribe to changes on a group_id.
31+
print('Subscribing to ', group_name)
32+
client.subscribe_group(group_name)
33+
34+
def disconnected(client):
35+
# Disconnected function will be called when the client disconnects.
36+
print('Disconnected from Adafruit IO!')
37+
sys.exit(1)
38+
39+
def message(client, topic_id, payload):
40+
# Message function will be called when a subscribed topic has a new value.
41+
# The topic_id parameter identifies the topic, and the payload parameter has
42+
# the new value.
43+
print('{0} received new value: {1}'.format(topic_id, payload))
44+
45+
46+
# Create an MQTT client instance.
47+
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
48+
49+
# Setup the callback functions defined above.
50+
client.on_connect = connected
51+
client.on_disconnect = disconnected
52+
client.on_message = message
53+
54+
# Connect to the Adafruit IO server.
55+
client.connect()
56+
57+
# Run a message loop forever to listen
58+
client.loop_blocking()

0 commit comments

Comments
 (0)