RaspberryPi Zero W Paho-mqtt不适用于本地主机

时间:2018-08-21 21:39:14

标签: python raspberry-pi mqtt paho

我刚刚将我的rpi设置为零。我一直在尝试使以下脚本起作用。该脚本确实可以在我的rpi 3B中运行,但是由于某些原因它不能在零w上运行。当我连接到除“ localhost”以外的任何其他地址时,该脚本都可以工作。

脚本:

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
        client.subscribe("help/hi")

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("localhost", 1883, 60)

client.loop_forever()

我得到的错误是:

Traceback (most recent call last):
  File "sample_client.py", line 19, in <module>
    client.connect("localhost", 1883, 60)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 768, in connect
    return self.reconnect()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 895, in reconnect
    sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
  File "/usr/lib/python2.7/socket.py", line 575, in create_connection
    raise err
socket.error: [Errno 111] Connection refused

当我将localhost更改为iot.eclipse.org时,它可以工作。知道会发生什么吗?我还需要安装其他东西吗?

更多详细信息: Python 2.7.13

pip show paho-mqtt的内容:

Name: paho-mqtt
Version: 1.3.1
Summary: MQTT version 3.1.1 client class
Home-page: http://eclipse.org/paho
Author: Roger Light
Author-email: roger@atchoo.org
License: Eclipse Public License v1.0 / Eclipse Distribution License v1.0
Location: /usr/local/lib/python2.7/dist-packages
Requires:

Rasbian版本:

PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

0 个答案:

没有答案