0

Subscribing to an List of feeds
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Subscribing to an List of feeds

by niccolorgu on Sun Jan 17, 2021 11:25 am

I have set up a raspberry pi and am connecting to AdaFruit feeds using Adafruit IO MQTT API.
I am very new to programming on the Pi.

I have the following code working and would like to know how to find the list item within the
def subscribe(client, userdata, mid, granted_qos):

I have set up the feeds as
# Set to the ID of the feed to subscribe to for updates.
FEED_ID = ['temperature', 'humidity', 'pressure']

my code appears to work but I can't see how the subscribe function is receiving the specific feed
it is called by the connected(client) function and I can access the list item by using FEED_ID[mid-1]
however it may just be a coincidence that mid-1 equates to the correct list item

I have two code blocks, the first listens for feed changes and the second creates the feeds.

Code: Select all | TOGGLE FULL SIZE
# Example of using the MQTT client class to subscribe to a feed and print out
# any changes made to the feed.
# Modified from examples by
# Author: Tony DiCola
# https://github.com/adafruit/Adafruit_IO_Python/tree/master/examples/mqtt

# Import standard python modules.
import sys

# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient

# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username)
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'

# Set to the ID of the feed to subscribe to for updates.
FEED_ID = ['temperature', 'humidity', 'pressure']


# Define callback functions which will be called when certain events happen.
def connected(client):
    # Connected function will be called when the client is connected to Adafruit IO.
    # This is a good place to subscribe to feed changes.  The client parameter
    # passed to this function is the Adafruit IO MQTT client so you can make
    # calls against it easily.
    # Subscribe to changes on a feed named FEED_ID.
    for feed in FEED_ID:
      print('Connected to Adafruit IO!  Listening for {0} changes...'.format(feed))
      client.subscribe(feed)

def subscribe(client, userdata, mid, granted_qos):
    # This method is called when the client subscribes to a new feed.
    print('Subscribed to {0} with QoS {1}'.format(FEED_ID[mid-1], granted_qos[0]))
   
def disconnected(client):
    # Disconnected function will be called when the client disconnects.
    print('Disconnected from Adafruit IO!')
    sys.exit(1)

def message(client, feed_id, payload):
    # Message function will be called when a subscribed feed has a new value.
    # The feed_id parameter identifies the feed, and the payload parameter has
    # the new value.
    print('Feed {0} received new value: {1}'.format(feed_id, payload))

# Create an MQTT client instance.
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Setup the callback functions defined above.
client.on_connect    = connected
client.on_disconnect = disconnected
client.on_message    = message
client.on_subscribe  = subscribe

# Connect to the Adafruit IO server.
client.connect()

# Start a message loop that blocks forever waiting for MQTT messages to be
# received.  Note there are other options for running the event loop like doing
# so in a background thread--see the mqtt_client.py example to learn more.
client.loop_blocking()


Code: Select all | TOGGLE FULL SIZE
# Example of using the MQTT client class to subscribe to and publish feed values.
# Modified from examples by
# Author: Tony DiCola
# https://github.com/adafruit/Adafruit_IO_Python/tree/master/examples/mqtt

# Import standard python modules.
import random
import sys
import time
from sense_hat import SenseHat

# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient

# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username)
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'

# Set to the ID of the feed to publish to for updates.
FEED_ID = ['temperature', 'humidity', 'pressure']

# Define callback functions which will be called when certain events happen.
def connected(client):
    # Connected function will be called when the client is connected to Adafruit IO.
    # This is a good place to subscribe to feed changes.  The client parameter
    # passed to this function is the Adafruit IO MQTT client so you can make
    # calls against it easily.
    print('Connected to Adafruit IO!  Listening for {} changes...'.format(FEED_ID))
    # Subscribe to changes on a feed named FEED_ID.
    for feed in FEED_ID:
      client.subscribe(feed)

def disconnected(client):
    # Disconnected function will be called when the client disconnects.
    print('Disconnected from Adafruit IO!')
    sys.exit(1)

def message(client, feed_id, payload):
    # Message function will be called when a subscribed feed has a new value.
    # The feed_id parameter identifies the feed, and the payload parameter has
    # the new value.
    print('Feed {0} received new value: {1}'.format(feed_id, payload))

# Create an MQTT client instance.
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Setup the callback functions defined above.
client.on_connect    = connected
client.on_disconnect = disconnected
client.on_message    = message

# Get an instanse of SenseHat
sense = SenseHat()

# Connect to the Adafruit IO server.
client.connect()

# Now the program needs to use a client loop function to ensure messages are
# sent and received.  There are a few options for driving the message loop,
# depending on what your program needs to do.

# This is why the python modules random, sys, time have been imported
# The first option is to run a thread in the background so you can continue
# doing things in your program.
client.loop_background()
# Now send new values every 10 seconds.
print('Publishing a new message every 10 seconds (press Ctrl-C to quit)...')
try:
  while True: 
    for feed in FEED_ID:
        value = eval("sense.get_" + feed + "()")
        client.publish(feed, value)
    time.sleep(10)
# If you do not have a feed called FEED_ID, one will be created with the first message.
except KeyboardInterrupt: # detect ctrl+c and handel it
  client.disconnect()

niccolorgu
 
Posts: 4
Joined: Wed Jan 13, 2021 5:37 pm

Re: Subscribing to an List of feeds

by brubell on Wed Jan 20, 2021 11:31 am

Instead of subscribing to multiple feeds inside a list, try adding those feeds to a Group. Then, simply subscribe to the group.

brubell
 
Posts: 1381
Joined: Fri Jul 17, 2015 10:33 pm

Please be positive and constructive with your questions and comments.