樹(shù)莓派使用paho連接mosquitto失敗

介紹

我在亞馬遜主機(jī)上安裝了mosquitto作為mqtt服務(wù)器,在樹(shù)莓派上找了一個(gè)python客戶(hù)端庫(kù)paho,安裝后找了一個(gè)例程:
https://github.com/eclipse/paho.mqtt.python#constructor-reinitialise
nano test.py:

import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, 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("$SYS/#")

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

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("iot.eclipse.org", 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()

chmod +x test.py
python ./test.py

運(yùn)行正常,但當(dāng)我將服務(wù)器地址改為我在aws上的地址時(shí),服務(wù)器報(bào)

Invalid protocol "MQTT"...

解決方案

經(jīng)多方查證,問(wèn)題的根源處在mosquitto,我服務(wù)器(ubuntu14.04)上的版本太舊,在mtqq v3.1和v3.11版本兼容性上出了問(wèn)題,那解決之道就是更新mosquitto版本了:

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo apt-get update
sudo apt-get install mosquitto

安裝新版本后問(wèn)題解決。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容