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

Hello, i got this error all the time. My esp8266 can’t connect to my mqtt broker (mosquitto). 1 week ago verything worked but now idk what happened. In node red my mqtt in nodes are connected with mosquitto broker but my esp can’t connect to mqtt broker ;/ I tried other versions of pubsubclient library and hasn’t helped,
Any help?

WiFi connected
IP address:
192.168.0.100
Attempting MQTT connection…failed, rc=-2 try again in 5 seconds

I got HA running on my windows PC where I got virtual box.
I allowed in firewall the port1883
image

MOSQUITTO LOGS:

[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] mosquitto.sh: executing... 
[16:34:07] INFO: Setting up user belkocik
[16:34:07] INFO: Certificates found: SSL is available
[cont-init.d] mosquitto.sh: exited 0.
[cont-init.d] nginx.sh: executing... 
[cont-init.d] nginx.sh: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
[16:34:08] INFO: Starting NGINX for authentication handling...
[16:34:08] INFO: Starting mosquitto MQTT broker...
1636558448: mosquitto version 1.6.12 starting
1636558448: |-- *** auth-plug: startup
[16:34:09] INFO: Successfully send discovery information to Home Assistant.
[16:34:09] INFO: Successfully send service information to the Supervisor.
1636558448: Config loaded from /etc/mosquitto/mosquitto.conf.
1636558448: Loading plugin: /usr/share/mosquitto/auth-plug.so
1636558448:  ├── Username/password checking enabled.
1636558448:  ├── TLS-PSK checking enabled.
1636558448:  └── Extended authentication not enabled.
1636558448: Opening ipv4 listen socket on port 1883.
1636558448: Opening ipv6 listen socket on port 1883.
1636558448: Opening websockets listen socket on port 1884.
1636558448: Opening ipv4 listen socket on port 8883.
1636558448: Opening ipv6 listen socket on port 8883.
1636558448: Opening websockets listen socket on port 8884.
1636558448: Warning: Mosquitto should not be run as root/administrator.
1636558448: mosquitto version 1.6.12 running
1636558448: New connection from 127.0.0.1 on port 1883.
1636558448: Socket error on client <unknown>, disconnecting.
1636558459: New connection from 192.168.0.108 on port 1883.
1636558459: New client connected from 192.168.0.108 as mqtt_a7aefbfca2f759b1 (p2, c1, k60, u'belkocik').

I don’t see a connection attempt in your mqtt log.


Did you use the correct username/password in your script ?

yes, i checked it 5 times haha. even copy and paste from mosquitto config

I see that people suggest to write these lines

listener 1883
allow_anonymous true

in mosquitto.conf but I got HA installed on virtual machine and I can’t find this file ;/

Did it like:


and it doesn’t help

allow_anonymous true

does not work with the HA Add-on

You need to put something like Wireshark on your PC to capture network traffic. from the logs you’ve provided it looks like the network connection is not reaching your PC from the esp8266 device.
Some WiFi systems have a setting called “client isolation” or similar which allows wireless clients to access the internet, but not other local network devices - that’s the sort of thing which could be causing you issues.

Maybe the problem is that I can only access my HA interface with single url? I can only access the Home Assistant with my duck dns domain. BUT my node-red mqtt nodes are connected to my mqtt mosquitto broker server


I got IP from Virtual Box HAOS and I can’t access it like: https://192.168.0.138:8123

Your node-reds don’t have to pass the Windows firewall.

Kinda strange. Everything was working a week ago. There was no updates on libraries/HAOS, esp8266’s code wasn’t changed. I haven’t updated anything. ;/
edit: what I noticed that on another esp where I got esphome installed I got warning like


“WARNING Can’t connect to ESPHome API for esphome-sensors.local: Error resolving IP address: [Errno -2] Name or service not known” and there is OFFLINE label:

But the sensor works: it shows the motion
image

When unplug the usb and plug other power 3.3v (like I did 1 week ago when everything was working) and then a wemos d1 mini flashes demoniacally in blue.
Previously It was just 1-2 blinks that shows that it connected to wifi and then it was static blue light and blinks when it sends tx or rx.
it when it connects to wifi (on external power - not usb)- it blinks demonically

What is wrong with that ;/ I can connect to my mqtt broker with MQTT Explorer and publish a message:


Previously my esp connects to my mqtt broker successfully, and now it doesn’t. What is up with my windows? I tried to turn off firewall and then it doesn’t connect too…
EDIT: I got something wrong with my remote access duckdns etc… I changed the ip of my home assistant and now I am able to connect esp to my mqtt broker…

Hi , i have a problem like you, But now i can fix it

  1. you must set “ssid” and “password” matching Internet usage
  2. check Check if the IP address of the mqtt server matches the code.
  3. check number port, user and password (if have)

Hi, Could you send me the full code of this project please, I need to connect my ESP32 to Node-RED by using MQTT installed in my raspberry Pi.

i have the same problem can i send my code?

First:

By default, Mosquitto only listen to local connections : add following lines to /etc/mosquitto/mosquitto.conf =>
Listener 1883 0.0.0.0
allow_anonymous true

Second:
restart your broker(MQTT server) using this command :

sudo systemctl restart mosquitto.service