使用 Pixel OS 在树莓派 3 上尝试 运行 mqtt_client 时出错
Getting error when trying to run mqtt_client on raspbberry pi 3 with Pixel OS
请在下面找到我正在尝试 运行 的脚本:
def initialize(global_listener):
global MQTT_CLIENT
global SUBSCRIBERS_ACTIVE_COUNT
SUBSCRIBERS_ACTIVE_COUNT = 0
MQTT_CLIENT = setup_mqtt_client()
global_listener.set_client(MQTT_CLIENT)
subscribe_and_listen(MQTT_CLIENT)
def subscribe_and_listen(client):
# Start subscribe, with QoS level 1
global UUID
client.subscribe(str(UUID), 1)
client.subscribe("status_channel", 1)
client.subscribe("status_channel/"+str(UUID), 1)
rc = 0
while rc == 0:
rc = client.loop()
handle_exception(client=client, rc=rc)
LOG.debug("rc: " + str(rc))
def setup_mqtt_client():
client = paho.Client(client_id=UUID, clean_session=True, userdata=None, protocol=paho.MQTTv31)
# Assign event callbacks
client.on_message = on_message
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish
client.on_subscribe = on_subscribe
client.on_log = on_log
client.will_set("status_channel/ws_hot", payload=get_last_will_payload(UUID), qos=1, retain=False)
url = urlparse.urlparse(os.environ.get('CLOUDMQTT_URL', 'tcp://<mqtt-server-ip>:1883'))
# Connect
client.username_pw_set("<username>", "<password>")
client.connect(url.hostname, url.port)#, keepalive=20)
return client
但是在 运行 上面的脚本中,我收到以下错误:
Traceback (most recent call last):
File "src/mqtt_client.py", line 142, in <module>
initialize(JOB_HANDLER)
File "src/mqtt_client.py", line 115, in initialize
MQTT_CLIENT = setup_mqtt_client()
File "src/mqtt_client.py", line 106, in setup_mqtt_client
client.connect(url.hostname, url.port)#, keepalive=20)
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 686, in connect
return self.reconnect()
File "/usr/local/lib/python2.7/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/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 113] No route to host
我到处都找不到答案。请帮忙。
P.S。 : 我试过禁用防火墙并清除 raspberry pi 上的 iptables 以缓解不适。
Raspberry Pi Jessie 与 Pixel 的分发存在一些问题。 运行 以下更新固件的命令解决了我的问题
rpi-更新
完整更新最多可能需要 10-15 分钟才能完成。裸露它,并在完成固件更新后重新启动
请在下面找到我正在尝试 运行 的脚本:
def initialize(global_listener):
global MQTT_CLIENT
global SUBSCRIBERS_ACTIVE_COUNT
SUBSCRIBERS_ACTIVE_COUNT = 0
MQTT_CLIENT = setup_mqtt_client()
global_listener.set_client(MQTT_CLIENT)
subscribe_and_listen(MQTT_CLIENT)
def subscribe_and_listen(client):
# Start subscribe, with QoS level 1
global UUID
client.subscribe(str(UUID), 1)
client.subscribe("status_channel", 1)
client.subscribe("status_channel/"+str(UUID), 1)
rc = 0
while rc == 0:
rc = client.loop()
handle_exception(client=client, rc=rc)
LOG.debug("rc: " + str(rc))
def setup_mqtt_client():
client = paho.Client(client_id=UUID, clean_session=True, userdata=None, protocol=paho.MQTTv31)
# Assign event callbacks
client.on_message = on_message
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish
client.on_subscribe = on_subscribe
client.on_log = on_log
client.will_set("status_channel/ws_hot", payload=get_last_will_payload(UUID), qos=1, retain=False)
url = urlparse.urlparse(os.environ.get('CLOUDMQTT_URL', 'tcp://<mqtt-server-ip>:1883'))
# Connect
client.username_pw_set("<username>", "<password>")
client.connect(url.hostname, url.port)#, keepalive=20)
return client
但是在 运行 上面的脚本中,我收到以下错误:
Traceback (most recent call last):
File "src/mqtt_client.py", line 142, in <module>
initialize(JOB_HANDLER)
File "src/mqtt_client.py", line 115, in initialize
MQTT_CLIENT = setup_mqtt_client()
File "src/mqtt_client.py", line 106, in setup_mqtt_client
client.connect(url.hostname, url.port)#, keepalive=20)
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 686, in connect
return self.reconnect()
File "/usr/local/lib/python2.7/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/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 113] No route to host
我到处都找不到答案。请帮忙。
P.S。 : 我试过禁用防火墙并清除 raspberry pi 上的 iptables 以缓解不适。
Raspberry Pi Jessie 与 Pixel 的分发存在一些问题。 运行 以下更新固件的命令解决了我的问题
rpi-更新
完整更新最多可能需要 10-15 分钟才能完成。裸露它,并在完成固件更新后重新启动