简体   繁体   中英

raspberry pi connecting to mqtt

import sys
import paho.mqtt.publish as publish

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

I am using the above Python code to publish to a topic. The error I am getting is:

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

What can be the reason?

mqc = mqttc.Client()

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

Add this code. hope this will helpfull

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM