0

I trying to connect an esp32 (client) to a raspberry pi (broker), using MQTT, but running into difficulties.

On the broker side I have mosquitto (2.0.11) running. From the pi, i can publish and subscribe without issue. In three terminals i can:

#Terminal 1:
systemctl stop mosquitto #to stop the daemon
mosquitto #to see a live instance

#Terminal 2:
mosquitto_sub -t 'new' #Term 1 shows new connection

#Terminal 3: 
mosquitto_pub -t 'new' -m 'Test message' #Term 2 shows 'new message'; Term 1 shows new connection and disconnect.

So i believe the broker is running fine. I try to connect from the esp32 First I edit /etc/mosquitto/mosquitto.conf adding lines anonymous true and listener 1883.

Then I load the wifi in the boot.py:

def do_connect():
    import network
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)
        sta_if.connect('bubble', 'Littleenginethatcould!')
        while not sta_if.isconnected():
            pass
    print('network config:', sta_if.ifconfig())
    
do_connect() #returns the correct IP address, i can ping from my pi and shows on my router

Then I try and make a connection from the main.py:

from umqtt.simple import MQTTClient
from machine import Pin
from time import sleep

CLIENT_NAME = 'esp'
BROKER_ADDR = '192.168.0.10' #static address on the pi
mqttc = MQTTClient(CLIENT_NAME, BROKER_ADDR, keepalive=60)
mqttc.connect()

Last line throws the error:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "umqtt/simple.py", line 68, in connect
OSError: [Errno 104] ECONNRESET

Searching for this error online only gives two results which describe someone not using the correct IP for their broker. I am certain mine is correct. I SSH into it all the time.

I am still wrapping my head around esp32 so I try and connect my laptop to the pi. I install paho-mqtt using pip. I then try and run the following:

import paho.mqtt.client as mqtt
client = mqtt.Client('Inspiron')
client.connect('192.168.0.10') #static address on the pi

Now the last line throws the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/<user>/esp/micropython/lib/python3.10/site-packages/paho/mqtt/client.py", line 914, in connect
    return self.reconnect()
  File "/home/<user>/esp/micropython/lib/python3.10/site-packages/paho/mqtt/client.py", line 1044, in reconnect
    sock = self._create_socket_connection()
  File "/home/<user>/esp/micropython/lib/python3.10/site-packages/paho/mqtt/client.py", line 3685, in _create_socket_connection
    return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
  File "/usr/lib/python3.10/socket.py", line 845, in create_connection
    raise err
  File "/usr/lib/python3.10/socket.py", line 833, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

I thus don't seem able to access the broker from any machine other than that which is running the broker. I think i have edited the config file correctly, but still can't connect.

MorrisseyJ
  • 121
  • 1
  • 2
  • 6
  • the only thing that comes to mind is that you never specify the port - I know 1883 is the default for MQTT, but I don't know if those python libraries default to 1883 - edit: never mind, I see that umqtt.simple DOES default to 1883. Do you have any firewalling on your raspberry pi? Is the pi connected wired or wirelessly? If wirelessly, does your AP isolate wireless clients? – Jaromanda X Nov 14 '22 at 03:20
  • Thanks for this. pi is connected wirelessly. I can't find the AP isolation settings on my router, but i am already sending instructions to wifi lights and a chromecast from this pi, over wifi (though through some existing python module, so i don't know if its calling MQTT). No firewalling that i have set up. – MorrisseyJ Nov 14 '22 at 05:37
  • Does the pi (broker) log anything when the ESP32 tries to connect? – StarCat Nov 14 '22 at 07:14
  • @StarCat no, pi (broker) registers nothing. I just get the connect error on the esp32 – MorrisseyJ Nov 14 '22 at 15:21

2 Answers2

1

Answer to this was that when running mosquitto directly from the command line, rather than as a daemon, you have to call it with the -c flag, specifying the modified conf file. So:

mosquitto -c /etc/mosquitto/mosquitto.conf

If you don't do that, it will revert to the standard conf file which doesn't allow anonymous clients. When running as a daemon, it will call the conf file automatically, so updates to the file work fine. Thanks to @hardillb (https://stackoverflow.com/users/504554/hardillb)

MorrisseyJ
  • 121
  • 1
  • 2
  • 6
0

Hy, I had the same problem today after reisntall mosquitto on Raspberry Zero Edit your config.file "/etc/mosquitto/mosquitto.conf" and just add this line : listener 1883

Pierre
  • 1
  • Yes, this works, but only if running from the daemon. I was trying to debug by calling an instance of mosquitto which required specifying the conf file. See answer posted. Thanks for the response. – MorrisseyJ Nov 27 '22 at 23:10