介绍
我在亚马逊主机上安装了mosquitto作为mqtt服务器,在树莓派上找了一个python客户端库paho,安装后找了一个例程:
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
运行正常,但当我将服务器地址改为我在aws上的地址时,服务器报
Invalid protocol "MQTT"...
解决方案
经多方查证,问题的根源处在mosquitto,我服务器(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
安装新版本后问题解决。
网友评论