MQTT客户端无法接收消息

时间:2016-10-15 16:12:08

标签: python publish-subscribe mqtt

我使用MQTT Paho项目中的以下代码订阅来自我的mqtt代理的消息。我使用<configuration> <property> <name>hbase.rootdir</name> <value>hdfs://localhost:54310/hbase</value> </property> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>localhost</value> </property> </configuration> 测试了连接,然后在那里收到消息。但是,当我运行以下代码时,它不会收到任何消息,也不会打印输出。我查看了主题和主持人。

<configuration>
 <property>
  <name>hadoop.tmp.dir</name>
  <value>/app/hadoop/tmp</value>
 </property>

 <property>
  <name>fs.default.name</name>
  <value>hdfs://localhost:54310</value>
 </property>
</configuration>

代理记录以下错误:

mosquitto_sub

编辑感谢@hardillb指出过时的MQTT版本。

完成以下操作后,一切正常:

  1. sudo apt-get purge mosquitto
  2. sudo apt-add-repository ppa:mosquitto-dev / mosquitto-ppa
  3. sudo apt-get update
  4. sudo apt-get install mosquitto

1 个答案:

答案 0 :(得分:1)

这很可能是因为你使用的是旧版本的mosquitto,而python正在期待一个支持MQTT 3.1.1的新版本

尝试更改代码,如下所示:

import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
    print("Connected with result code "+str(rc))
    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("test")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

# Change made HERE 
client = mqtt.Client(protocol=MQTTv31)

client.on_connect = on_connect
client.on_message = on_message

client.connect("localhost", 1883, 60)
client.loop_forever()

您还应该尽快升级您的经纪人,该版本已经过时并且有许多已知问题,当前版本是1.4.10