Is there a way to use multiple feeds with Adafruit MQTT python client subscriptions?


Is there a way to use multiple feeds with Adafruit MQTT python client subscriptions?



I'm using Adafruit IO MQTT python client to execute code based upon feeds. I want one feed to execute one function and a different to execute another. I've looked at this and searched around but nothing I have found shows how to subscribe to multiple feeds.




2 Answers
2



You can simply call subscribe multiple times, as in:


subscribe


for feed_id in ['feed1', 'feed2']:
client.subscribe(feed_id)



This is a basic feature of the MQTT protocol. Your on_message_ handler will receive the feed id as a parameter.


on_message_





Thanks for that. I still don't quite understand how I specify which function is run for a given feed
– MPZinke
Jul 3 at 4:32





You use the feed id parameter in your on_message_ handler to determine what function to execute.
– larsks
Jul 3 at 11:16


on_message_



You have to build that yourself



Just add an if statement to the on_message callback to pick a different function based on the topic (feed_id) e.g.


on_message


def message(client, feed_id, payload, retain):
if feed_id == 'foo/bar':
#call foo function
foo(paylaod)
elif feed_id == 'bar/foo':
#call bar function
bar(payload)





I get it now. Thank you!
– MPZinke
Jul 3 at 12:35






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages