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.