当我尝试运行屏幕下方的代码时,屏幕仍然为空白,并且不表示客户端已连接到代理。
#! /usr/bin/python
import paho.mqtt.client as mqtt
broker = "localhost"
#define what happens after connection
def on_connect(rc):
print "Connection returned result: "+str(rc)
#On recipt of a message do action
def on_message(msg):
n = msg.payload
t = msg.topic
print t+" "+str(n)
# create broker
mqttc = mqtt.Client()
#define callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
#connect
mqttc.connect(broker, 1883, 60)
#Subscribe to topic
mqttc.subscribe("/sensor/rfid", 2)
#keep connected
mqttc.loop_forever()
我可以验证代理是否正常运行,因为我能够运行
mosquitto_sub -t /sensor/rfid
并获取从我的 Android 手机上的 MyMQTT 应用程序发送的消息。我还忘了提到这一切都在安装了 mosquitto、mosquitto-clients 和 paho-mqtt 的树莓派上。