ayanez17
Posts: 3
Joined: Tue Aug 21, 2018 9:44 pm

Paho-mqtt does not work with raspberry pi zero w

Tue Aug 21, 2018 9:49 pm

I just set up my rpi zero w. Ive been trying to get the following script to work. The script does work in my rpi 3B but for some reason it does not work on the zero w. The script does work when I connect to any other address but "localhost".

Script:

Code: Select all

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()
The error I get is:

Code: Select all

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

When I change

Code: Select all

localhost
to

Code: Select all

 iot.eclipse.org
it works. Any idea what could be going on? Is there anything else I need to install?

More details:

Code: Select all

 Python 2.7.13
Contents of pip show paho-mqtt:

Code: Select all

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: [email protected]
License: Eclipse Public License v1.0 / Eclipse Distribution License v1.0
Location: /usr/local/lib/python2.7/dist-packages
Requires:
Raspbian Version:

Code: Select all

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"
Again this works perfectly on the rpi 3B but not on the zero w. Thanks in advance :)

saltydog
Posts: 39
Joined: Mon Dec 24, 2012 10:40 am

Re: Paho-mqtt does not work with raspberry pi zero w

Wed Aug 22, 2018 5:16 am

Do you have a mqtt sever running? Mosquitto ??

ayanez17
Posts: 3
Joined: Tue Aug 21, 2018 9:44 pm

Re: Paho-mqtt does not work with raspberry pi zero w

Wed Aug 22, 2018 5:29 am

I was following along this tutorial: https://www.youtube.com/watch?v=Pb3FLznsdwI I don't remember running a mosquitto server in the raspberry pi 3 B, but I'm for sure not running one currently in the pi zero W. I'm trying to get this client script running so I can use Homebridge as a publisher. Let me know if I can provide anymore information :)

ayanez17
Posts: 3
Joined: Tue Aug 21, 2018 9:44 pm

Re: Paho-mqtt does not work with raspberry pi zero w

Wed Aug 22, 2018 5:41 am

AHHH. Thats what is was. I looked in the /etc directory and I had mosquitto installed and running. I installed it in the zero w and everything works now :)

Return to “Python”