我想使用hivemq 作为我的 IOT 项目的在线代理,但问题是我无法从我的树莓派连接到 hiveMQ。我什至无法从我的树莓派打开 hiveMQ 网站。
我尝试使用这个基本的 python 脚本订阅 hiveMQ 代理
import paho.mqtt.client as mqtt
MQTT_HOST = "broker.mqttdashboard.com"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 5
MQTT_TOPIC = "testTopic"
def on_connect(mosq, obj, rc):
mqttc.subscribe(MQTT_TOPIC, 0)
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed to MOTT Topic...")
def on_message(mosq, obj, msg):
print(msg.payload)
mqttc = mqtt.Client()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
mqttc.loop_forever()
但它抛出了一个错误:
Traceback (most recent call last):
File "subscriber.py", line 23, in <module>
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 686, in connect
return self.reconnect()
File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 808, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
File "/usr/lib/python3.4/socket.py", line 509, in create_connection
raise err
File "/usr/lib/python3.4/socket.py", line 500, in create_connection
sock.connect(sa)
OSError: [Errno 113] No route to host
另一方面,我还使用相同的 python 脚本订阅了我的本地 MOSQUITTO 代理,只需将 HOST 更改为我本地机器的 ip,它工作正常......