覆盆子pi连接到mqtt

时间:2017-04-03 20:20:48

标签: python python-2.7 mqtt paho

import sys
import paho.mqtt.publish as publish

publish.single("paho/test/single", "boo", hostname="iot.eclipse.org")

我使用上面的Python代码发布到一个主题。 我得到的错误是:

Traceback (most recent call last):
  File "/home/pi/bgvf.py", line 8, in <module>
    publish.single("paho/test/single", "boo", hostname="iot.eclipse.org")
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/publish.py", line 223, in single
    multiple([msg], hostname, port, client_id, keepalive, will, auth, tls, protocol, transport)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/publish.py", line 174, in multiple
    client.connect(hostname, port, keepalive)
  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
error: [Errno 111] Connection refused

可能是什么原因?

1 个答案:

答案 0 :(得分:-1)

mqc = mqttc.Client()

mqc.on_connect = on_connect
mqc.on_message = on_message
mqc.on_publish = on_publish
mqc.connect('localhost',1883,60)

添加此代码。希望这会有所帮助