首页 > 解决方案 > RaspberryPi 零 W Paho-mqtt 无法与本地主机一起使用

问题描述

我刚刚设置了我的 rpi 零 w。我一直在尝试使以下脚本正常工作。该脚本在我的 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:

拉斯比版本:

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"

标签: pythonraspberry-pimqttpaho

解决方案


推荐阅读