# Mqtt will not connect

**URL:** <https://discourse.nodered.org/t/mqtt-will-not-connect/85409>\
**Category:** General\
**Created:** [11 February 2024 03:52 UTC](https://discourse.nodered.org/t/mqtt-will-not-connect/85409 "2024-02-11T03:52:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![perigalacticon](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@perigalacticon](https://discourse.nodered.org/u/perigalacticon)\
**Post date:** [11 February 2024 03:52 UTC](https://discourse.nodered.org/t/mqtt-will-not-connect/85409/1 "2024-02-11T03:52:09Z")

</div>

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:

```auto
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)

```auto
 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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [11 February 2024 05:05 UTC](https://discourse.nodered.org/t/mqtt-will-not-connect/85409/2 "2024-02-11T05:05:31Z")

</div>

> [@perigalacticon](#):
>
> If I use the ip address of the RPi4b then it just says "connecting

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](https://mosquitto.org/blog/2020/12/version-2-0-0-released/)

In simple terms, put ...

`listener 1883 0.0.0.0`

in mosquitto.conf

---

<div class="post-metadata">

**Author:** ![perigalacticon](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@perigalacticon](https://discourse.nodered.org/u/perigalacticon)\
**Post date:** [11 February 2024 05:26 UTC](https://discourse.nodered.org/t/mqtt-will-not-connect/85409/3 "2024-02-11T05:26:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![perigalacticon](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@perigalacticon](https://discourse.nodered.org/u/perigalacticon)\
**Post date:** [12 February 2024 22:47 UTC](https://discourse.nodered.org/t/mqtt-will-not-connect/85409/4 "2024-02-12T22:47:08Z")

</div>

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 192.168.1.13. 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 192.168.1.215. It gives the same error:

```auto
Connecting to MQTT... Failed with state -2

```

---

<div class="post-metadata">

**Author:** ![perigalacticon](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@perigalacticon](https://discourse.nodered.org/u/perigalacticon)\
**Post date:** [12 February 2024 22:54 UTC](https://discourse.nodered.org/t/mqtt-will-not-connect/85409/5 "2024-02-12T22:54:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [12 February 2024 23:09 UTC](https://discourse.nodered.org/t/mqtt-will-not-connect/85409/6 "2024-02-12T23:09:21Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 February 2024 23:09 UTC](https://discourse.nodered.org/t/mqtt-will-not-connect/85409/7 "2024-02-26T23:09:36Z")

</div>

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