我正在使用 Adafruit IO MQTT python 客户端来执行基于提要的代码。我希望一个提要执行一个功能,而另一个提要执行另一个功能。我看过 这个 并四处搜索,但我没有找到显示如何订阅多个提要的内容。
您可以简单地多次调用
subscribe
,如:
for feed_id in ['feed1', 'feed2']:
client.subscribe(feed_id)
这是MQTT协议的一个基本特性。您的
on_message_
处理程序将接收 feed id 作为参数。
你必须自己建造它
只需将 if 语句添加到
on_message
回调中,即可根据主题(feed_id)选择不同的函数,例如
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)
你能找到解决这个问题的方法吗?正如之前评论中提到的,我不确定在哪里发布这些代码。