【问题标题】:Subscribe and read several topics on mqtt mosquitto using python paho使用 python paho 订阅并阅读 mqtt mosquitto 的几个主题
【发布时间】:2017-09-13 07:14:48
【问题描述】:

我设法发表了几个主题并阅读了其中一个。我需要做的是聆听和阅读所有已发布的主题并获取消息。这是我使用的代码:

  1. 向 3 个主题发布消息:

    #!/usr/bin/env python3
    
    
    import paho.mqtt.client as mqtt
    
    client = mqtt.Client()
    client.connect("localhost",1883,60)
    client.publish("topic/1", "400 | 350 | 320 | 410");
    client.publish("topic/2", "200 | 350 | 420 | 110");
    client.publish("topic/3", "200 | 350 | 420 | 110");
    
    client.disconnect();
    
  2. 订阅和阅读 1 个主题的消息

    #!/usr/bin/env python3
    
    import paho.mqtt.client as mqttClient
    import time
    
    def on_connect(client, userdata, flags, rc):
    
    if rc == 0:
    
        print("Connected to broker")
    
        global Connected                #Use global variable
        Connected = True                #Signal connection 
    
    else:
    
        print("Connection failed")
    
    def on_message(client, userdata, message):
    print "Message received : "  + message.payload
    
    Connected = False   
    
    broker_address= "localhost"          
    port = 1883                          
    
    client = mqttClient.Client("Python")          
    client.on_connect= on_connect    
    client.on_message= on_message        
    client.connect(broker_address, port=port)     
    client.loop_start()        
    
    while Connected != True:   
        time.sleep(0.1)
    
    client.subscribe("topic/2")
    try:
    while True:
        time.sleep(1)
    
    except KeyboardInterrupt:
    print "exiting"
    client.disconnect()
    client.loop_stop()
    

【问题讨论】:

  • 您的发布代码也有问题,您需要在每次发布之间调用client.loop函数以确保它们都刷新到网络堆栈,或者使用单/多发布功能( pypi.python.org/pypi/paho-mqtt/1.1#id17)

标签: python mqtt mosquitto


【解决方案1】:

您可以多次调用client.subscribe()函数订阅多个主题。

您还应该将调用移动到订阅on_connect 回调,以消除对第一个循环的需要。

#!/usr/bin/env python3

import paho.mqtt.client as mqttClient
import time

def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected to broker")
        client.subscribe("topic/1")
        client.subscribe("topic/2")
        client.subscribe("topic/3")
        client.subscribe("topic/4")

    else:
        print("Connection failed")

def on_message(client, userdata, message):
    print("Message received : "  + str(message.payload) + " on " + message.topic)


broker_address= "localhost"          
port = 1883                          

client = mqttClient.Client("Python")          
client.on_connect= on_connect    
client.on_message= on_message        
client.connect(broker_address, port=port)     
client.loop_start()


try:
    while True:
        time.sleep(1)

except KeyboardInterrupt:
    print("exiting")
    client.disconnect()
    client.loop_stop()

编辑:

您还可以使用以下语法一次性订阅多个主题

client.subscribe([("topic/1", 0), ("topic/2", 0), ("topic/3", 0),("topic/4", 0)])

【讨论】:

  • 感谢您的建议。似乎仍然无法按预期工作,因为我只收到 topic/1 的回复。我会尝试找出原因,但再次感谢
  • 我已经对其进行了编辑以添加订阅多个主题的替代方式。
  • 我正在寻找一种方法来获取发送消息的主题名称(订阅多个主题时)。在库的来源中搜索on_message,甚至智能感知也无法预测message 上的topic 属性。您的回答完成了工作,谢谢!
猜你喜欢
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多