为什么mqtt客户端订户不显示主题?

时间:2015-12-10 12:54:08

标签: python-3.x mqtt

我是MQTT的新手。我在两台电脑上安装了mosquitto和paho mqtt库。计算机通过本地网络中的路由器连接。我在一台计算机和另一台计算机上运行发布者和mosquitto。

# publisher.py code
# ===================================================================
#!/usr/local/bin/python3

import paho.mqtt.publish as publish

publish.single("paho/single", payload="boo", hostname="192.168.0.154")
# ===================================================================

用户代码

# subscriber.py

# =================================================================== 
#!/usr/local/bin/python3

import paho.mqtt.client as mqtt

mqttc = mqtt.Client("100")
mqttc.connect("192.168.0.154", 1883, 60)
mqttc.subscribe("paho/single", 0)

mqttc.loop_forever()
# ===================================================================

我可以看到订阅者和发布者在运行时都可以连接到mosquitto。但我看不到用户终端显示的主题发布。请帮忙。

1 个答案:

答案 0 :(得分:3)

您已订阅该主题,但您尚未告知客户端代码在邮件到达时如何处理该邮件。

以下更新应打印消息和主题

# subscriber.py

# =================================================================== 
#!/usr/local/bin/python3

import paho.mqtt.client as mqtt

def on_message(client, userdata, msg):
  print(msg.topic+" "+str(msg.payload))


mqttc = mqtt.Client("100")
mqttc.on_message = on_message
mqttc.connect("192.168.0.154", 1883, 60)
mqttc.subscribe("paho/single", 0)

mqttc.loop_forever()
# ===================================================================