Eclipse Paho Python客户端无法连接

时间:2014-12-12 02:27:34

标签: python eclipse raspberry-pi mqtt paho

当我尝试运行下面的代码时,屏幕保持空白,并不表示客户端已连接到代理。

#! /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应用程序发送消息。 我也忘了提到这一切都在覆盆子pi上安装了mosquitto,mosquitto-clients和paho-mqtt。

1 个答案:

答案 0 :(得分:1)

尝试使用Paho网站上的官方示例; https://eclipse.org/paho/clients/python/或更多; http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.python.git/tree/examples

看起来你在函数on_connect()

中缺少一些参数