• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Connect to local mqtt

Q&A Forum › Category: Other › Connect to local mqtt
0 Vote Up Vote Down
Anders Carlsson asked 1 year ago

Hello Sara!
I’m reading the Raspberry Pi Pico module12 and I’m trying to get it to connect to my local RPI4, running a Mosquito Broker and Node-Red and it works fine, I have been using it for a few years.

I have created a config.py with the correct password and user for my local RPI4 and I got this message when I try to run the script 12_3_mqtt_publish.py:

Waiting for Wi-Fi connection…
The connection was successful!
IP address: 192.168.1.192
Error connecting to MQTT: [Errnr 104] ECONNRESET
Error: [Errnr 104] ECONNRESET
So what am I doing wrong?
Greetings
Anders

Question Tags: Pi pico
24 Answers
0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

I put the port number in config.py and now I got this error:
Waiting for Wi-Fi connection…
The connection was successful!
IP address: 192.168.1.192
Error connecting to MQTT: -2
Error: -2

And of course I get no values in Node-Red

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

And the RPI4 is online and I can ping it and it also publishing other temperatures.

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
What’s the name of the Raspberry Pi Pico as an MQTT client?
Do you have other devices on the network with the same name?
Regards,
Sara

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

The name is: raspberrypi_picow
and no I have not any more devices on the network with the same name.

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

I don`t think I should have the portnumber in the config.py file? I think the simple.py have the portnumber 1883. And if I just have the ipadress in the config.py I got this error:
MPY: soft reboot
Connection successful!
IP address: 192.168.1.192
Error connecting to MQTT: [Errno 104] ECONNRESET
Error: [Errno 104] ECONNRESET
The RPI4 is set up from your description https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
can you show me how does your mosquito configuration file looks like?
Regards.
Sara

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

pid_file /run/mosquitto/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
listener 1883
allow_anonymous true

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

Ok I think I understand, I don`t have to use a password or a username for my others ESP32 for temperature measurements connecting to the brooker.
So to change in simple.py would it solve this? Or what to do?

Thanks Sara

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

I set up another RPI5 with mosquito and Node-Red and set it up with user and password. The config for this is :

per_listener_settings true
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /run/mosquitto/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
allow_anonymous false
listener 1883
password_file /etc/mosquitto/passwd

But the result was the same as before 🙁
MPY: soft reboot
Connection successful!
IP address: 192.168.1.192
Error connecting to MQTT: [Errno 104] ECONNRESET
Error: [Errno 104] ECONNRESET
Regards
Anders

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

The RPI5 din`t start the Mosquitto service so I took it away and took a RPI3 instead and now the service is running
Active: active (running) since Sun 2024-04-28 10:09:22 CEST; 2min 53s ago

And the config file for the RPI3:
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
per_listener_settings true
pid_file /run/mosquitto/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
allow_anonymous false
listener 1883
password_file /etc/mosquitto/passwd
But I got the same error on the RPI3:
MPY: soft reboot
Connection successful!
IP address: 192.168.1.192
Error connecting to MQTT: [Errno 104] ECONNRESET
Error: [Errno 104] ECONNRESET
 

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

This is from the mosquitto log file:
1714295274: mosquitto version 2.0.11 starting
1714295274: Config loaded from /etc/mosquitto/mosquitto.conf.
1714295274: Opening ipv4 listen socket on port 1883.
1714295274: Opening ipv6 listen socket on port 1883.
1714295274: mosquitto version 2.0.11 running

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

I took one ESP32 and DS18B20 for testing. And when I put in this it works:

mqttClient.setCredentials(“lovfall”, “password”);

So the brooker works and I get the temperatures into Node-Red.
So there is someting wrong with the pico program.
I will download the files again and try again.
Regards
Anders

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

I deleted all files in Pico and downloaded the files again, but no luck.
Connection successful!
IP address: 192.168.1.192
Error connecting to MQTT: [Errno 104] ECONNRESET
Error: [Errno 104] ECONNRESET

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

In the mosquitto log I can see that both ESP32 and the Node-Red (RPI3) is conneting.
But nothing about that the Pico is trying to connect.
Both the ESP32 and the Pico is using the same wireless network and also from the same distance.

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

To me it seems thath it`s not conneting at all, it goes so fast when the error code appears and you don`t get anyting in the mosquitto logfile.

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

Have someone manage this to work with an local brooker? Any suggestions would help. Because now I am out of any ideas.
Thanks

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi again.
 
I’m sorry for the delay in my response. I’m currently out of the office with limited access to the internet.
 
Where do you have your config.py file located? Double-check that the config.py file is located on the right place..
Please don’t remove the b’ character from where it is located on the file… can you also double.check it?
 
Regards,
Sara

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

Hello!
When I go in the PI Pico I see a folder called umqtt and in that folder I have the 2 files robust.py and simple.py.
Outside thath folder I have 12_3_mqtt_publish.py , BME280.py and the config.py.
And in the config.py you have the ssid and password and I get that the connection is succesful and I don`t think you would have that if the config.py file would be in the wrong place. Yes the 3 files  mqtt have the b’ And as I mention before I have deleted all the files in the Pi Pico and download them again to get everything “fresh”

And I always copy and past names of the files and ofcourse the code also. So I “never” type the names or code by hand.

And I have also tested, Displaying BME280 Sensor Data on OLED Display and others and all of them works.
And I have also put an incorrect password in the cofig.py to see if I will get something in the mosquitto logfile, but I don`t get anything in the log-file, that the Pi Pico is trying to connect. But both Node-Red and the ESP32 with the temperature messure is in the file.
Regards
Anders

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

This is my config.py for the RPI3:
wifi_ssid = ‘xxxxxx’
wifi_password = ‘xxxxxxxxxx’
mqtt_server = b’192.168.1.20′
mqtt_username = b’lovfall’
mqtt_password = b’xxxxxxxxxxxx’
The 192.168.1.20 is my temporary RPI3 with mosquitto and brooker, And you have to use username and password
I have connected one temperature measurement with an ESP32 and DS18B20 and it works I get readings in Node-Red.
And my ordernary brooker and mosquitto with 15 temperature measurements also work so they dosen`t interfering with each other.

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
Thanks for the provided information.
It seems you’re doing everything right… I’m not sure what can be wrong.
Ill be back at the office next week. So, I can try to test everything when I get back, to make sure nothing has changed meanwhile….
 
Regards,
Sara

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

Hello!
I have been testing with Pico as a Web Server to be sure the wifi works. And I get the web-page where I can control the led and also the readings from the BME280 sensor. So everyting is correct with the wifi also.

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
I just got back to the office and tested everything with the Mosquitto broker.
I found out the issue. 
Since you’re connecting to your local mosquito broker, you’re not using SSL. So, you need to change the following line on the Raspberry Pi Pico code:
 

MQTT_SSL = True

to

MQTT_SSL = False

 
In the configuration file you should use the mosquito MQTT IP address without the port.
 
It is working for me this way.
Let me know if this solves your issue.
 
Regards,
Sara

0 Vote Up Vote Down
Anders Carlsson answered 1 year ago

Hello Sara!
MQTT_SSL = False solve it.
Yes it works on both on my ordenaire RPI4 and the temperary RPI3, the only thing in the config.py I hade to change was the ip to the brookers. So the issu is solved.
Regards
Anders

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Great.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.