介紹
我在亞馬遜主機(jī)上安裝了mosquitto作為mqtt服務(wù)器,在樹莓派上找了一個(gè)python客戶端庫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)多方查證,問題的根源處在mosquitto甫匹,我服務(wù)器(ubuntu14.04)上的版本太舊,在mtqq v3.1和v3.11版本兼容性上出了問題惦费,那解決之道就是更新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
安裝新版本后問題解決兵迅。