【问题标题】:Eclipse Paho Python Client not connectingEclipse Paho Python 客户端未连接
【发布时间】:2014-12-12 02:27:33
【问题描述】:

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

#! /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 的树莓派上。

【问题讨论】:

    标签: python eclipse raspberry-pi mqtt paho


    【解决方案1】:

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

    看起来您在函数 on_connect() 中缺少一些参数

    【讨论】:

    • 我修改了我的代码以添加缺少的参数并尝试了官方示例。他们都没有工作。
    • 你安装的是什么版本? pip show paho-mqtt ?另外,如果您将官方示例与公共 mqtt 服务器 iot.eclipse.org 一起使用,您是否有同样的问题?你的 mqtt 代理是什么版本的?
    • 该命令对我不起作用,我可以很好地连接到公共 mqtt 服务器。我使用的代理是 mosquitto 0.15 版
    • 然后升级代理,那是一个较旧的代理。 (最新版本是 1.3.5)按照本指南添加和升级它,jpmens.net/2013/09/01/installing-mosquitto-on-a-raspberry-pi 应该在那之后工作。
    • 我按照你说的更新了它,它成功了!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2017-08-19
    相关资源
    最近更新 更多