建立了连接,但无法在PAHO MQTT客户端检索消息有效载荷
我的客户端无法从 MQTT 服务器接收消息负载。我在我的电脑上设置了 mosquitto 作为服务器和发布者,并执行以下命令:
mosquitto_pub -h 192.168.1.2 -t topic/test -m "testing" -i simulator -d
我自己订阅有效并收到有效负载。
但是,我使用 paho mqtt 的客户端仅收到成功的连接,而没有有效负载消息。所以我这里有以下代码:
def on_connect(client, userdata, flags, rc):
print("connection ok")
client.subscribe("topic/test")
def on_message(client, userdata, msg):
print("Message received! " + str(msg.payload))
if msg.payload == "testing":
print("Message received")
# Do something
...
def main():
#MQTT
broker = "192.168.1.2" #broker ip
client = mqtt.Client("simulator")
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker, 1883, 60)
client.loop_start()
while(1):
...
所以它确实设法打印“连接正常”,但不是“已收到消息!...”
My client is unable to receive message payload from the MQTT server. I had set mosquitto on my PC to act as both the server and publisher and execute the following command:
mosquitto_pub -h 192.168.1.2 -t topic/test -m "testing" -i simulator -d
Subscribing myself works and payload is received.
However, my client which is using paho mqtt only received successful connection without the payload message. So i have the following code here:
def on_connect(client, userdata, flags, rc):
print("connection ok")
client.subscribe("topic/test")
def on_message(client, userdata, msg):
print("Message received! " + str(msg.payload))
if msg.payload == "testing":
print("Message received")
# Do something
...
def main():
#MQTT
broker = "192.168.1.2" #broker ip
client = mqtt.Client("simulator")
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker, 1883, 60)
client.loop_start()
while(1):
...
So it did managed to print "connection ok", but not "Messaged received! ..."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
mosquitto_pub
和 Python 中使用相同的client-id
(simulator
)。根据规范:所以发生的情况是:
mosquitto_pub
连接,代理断开 python 客户端连接。mosquitto_pub
发布消息(但由于没有订阅任何内容,QOS 0 消息将被代理丢弃)。尝试在
mosquitto_pub
中使用不同的客户端 ID(当我以这种方式测试时,您的代码工作正常 - 在while 中添加了
)。time.sleep(1)
(1)You are using the same
client-id
(simulator
) withmosquitto_pub
and in your python. As per the spec:So what is happening is:
mosquitto_pub
connects, broker disconnects python client.mosquitto_pub
publishes the message (but as nothing is subscribed the QOS 0 message will be dropped by the broker).Try using a different client-id with
mosquitto_pub
(your code works fine when I test it in that way - added atime.sleep(1)
in thewhile(1)
).