Mqtt will not connect

I am trying to use mqtt as a broker on my RPi4b running Bullseye. I have a ESP8266 running a very simple sketch and a very simple flow in NR. It was working earlier today, now the ESP mqtt will not connect. When I click deploy this is printed in the terminal:

10 Feb 22:35:50 - [info] Starting flows
10 Feb 22:35:50 - [info] Started flows
10 Feb 22:35:50 - [info] [mqtt-broker:rpi mqtt broker] Connected to broker: mqtt://localhost:1883
10 Feb 22:36:29 - [warn] 

Your flow credentials file is encrypted using a system-generated key.

If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.

You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.

10 Feb 22:36:29 - [info] Stopping flows
10 Feb 22:36:29 - [info] [mqtt-broker:rpi mqtt broker] Disconnected from broker: mqtt://localhost:1883

The esp reports in the Serial Monitor (Arduino)

 Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

I have the broker set to localhost, and the mqtt in node shows "connected". If I use the ip address of the RPi4b then it just says "connecting". There is nothing filled in on the security tab. I have power cycled everything except the router. I restarted mqtt in the terminal and testing it in terminals and it seems to work ok. I don't know why now, other than the router gave a different IP address to the ESP8266 via DHCP, could that be the problem? The RPi4b has the same IP as earlier. I tried it with windows firewall off. For some reason the forum tells me new users can't upload attachments but I have the flow and sketch ready to post if you'll let me.

This is a clue. What broker have you installed?

Assuming mosquitto, Did you set the listener in the conf file?

More details can be found in the 2.0 release notes here

In simple terms, put ...

listener 1883

in mosquitto.conf

1 Like

Yes I think that fixed it I also added "allow_anonymous true". It's working now, thanks.

The ESP32 is not connecting again. I changed the RPi WIFI connection to a different network ("Netgear") that uses a different router that does not have an internet connection. The IP address is like I changed the sketch for the ESP32 so that it uses the Netgear network and IP for the mqtt server. I didn't change anything else. Now mqtt does not connect no matter what I do. I tried setting the broker as localhost and as the actual IPaddress. I have my PC connected to the Netgear network which works all the time and logged into the RPI with VNC no problem. I have MQTT Explorer and it shows that the topic "LED" is responding when I toggle a switch on the NR dashboard but the ESP does not receive anything. The ESP32 IP on the 2nd network is It gives the same error:

Connecting to MQTT... Failed with state -2

Let me tell you something, it is often embarrassing being a newb! I figured out that I was using a header file that had the wifi credentials and those credentials were wrong for the network I was trying to connect to! So sorry for the bother again, hopefully this learning curve will flatten out.


You already programmed a microcontroller to send data over two different networks to Node-red on a Linux computer, not a trivial accomplishment.

You seem to be doing pretty well for a beginner!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.