MQTT connection failed

Dear All,

I need help in debugging the MQTT connection failure issue.
Find the details as below.

  • I have used NodeMCU ESP8266 to upload the attached code, I am getting below error, when tried to connect MQTT

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

Please help me to figure out the MQTT failure.

did you setup mqtt with password openhabian ?

yes, I have given MQTT password as “openhabian”

So you are connecting the ESP8266 into the 2.4gig wifi with correct ssid and password

You need to install a mqtt debugger like mqtt explorer or mqttfx then connect with your credentials that you used in your Arduino program.

This will tell you if the mqtt broker is running. Then you can watch the messages as the 8266 connects, if it does connect. You can also watch the openHAB messages.

Make sure your Arduino program has the proper connection type, password and username. I am betting that is where the error is.

yes, I have home Wi-Fi connected, but when it comes to MQTT, it fails.
Attaching screenshot for reference.

It’s working fine when checked with Basic IU. Find the screenshot attached for reference.

Also I am using the code from gthub

Does you mqtt have username and/or password? The arduino does not. It needs to match.

Thanks for your reply Danny.
I have added the MQTT username and password in the code. Are you mentioning the same?
Sharing screenshot for reference.

Are you using the correct PubSubClient.h library for the project as things change sometimes braking the old way of doing things.

Are you awear of Tasmota you can use it to acheave what you are looking to do. You set it up through webpage and it will be easy to install.

I use this tool to flash my devices. tasmotizer

Just run the program
Select serial port
Select image Release
Erase before flashing

Then in Send config put in all the details.

Goto get ip
Navigate to ip in browser

1 Like

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