MQTT connection failed and not connected

Hai,
Iam trying to make contact closure using mqtt server and wifi configuraion using Wifi manager with set static ip and save the config using json.
wifi configuration are set ok but MQTT couldn't connected and failed.
i use below library version
WiFiManager -2.0.15-rc.1
PubSubClient -2.6

please help me to complete my project......

This doesn't make sense. You let the WiFiManager configure the network and then immediately delete that configuration?

Did you check the internet connectivity on your WiFi? As you're hiding the MQTT service you're using: Did you check (not believe) it accepts non-TLS connections on port 1883? Did you check that from a PC in the same WiFi network?

ok, i removed and tested again mqtt connection is failed. below the serial monitor log..

20:35:59.099 -> Attempting MQTT connection...failed, rc=-2 try again in 1 second

20:36:02.092 -> Attempting MQTT connection...failed, rc=-2 try again in 1 second

20:36:05.092 -> Attempting MQTT connection...failed, rc=-2 try again in 1 second

20:36:08.071 -> Attempting MQTT connection...failed, rc=-2 try again in 1 second

20:36:11.071 -> Attempting MQTT connection...failed, rc=-2 try again in 1 second

20:36:14.101 -> Attempting MQTT connection...failed, rc=-2 try again in 1 second

20:36:17.088 -> Attempting MQTT connection...failed, rc=-2 try again in 1 second

20:36:20.113 -> Attempting MQTT connection...failed, rc=-2 try again in 1 second

20:36:23.108 -> Attempting MQTT connection...failed, rc=-2 try again in 1 second

20:36:26.114 -> Attempting MQTT connection...failed, rc=-2 try again in 1 second

20:36:29.100 -> Attempting MQTT connection...failed, rc=-2 try again in 1 second

20:36:32.104 -> Attempting MQTT connection...failed, rc=-2 try again in 1 second

20:36:35.071 -> Attempting MQTT connection...failed, rc=-2 try again in 1 second

Did you make the checks I suggested in post #2?

-2 means connection failed. As you don't use a TLS connection your ESP doesn't have a way to connect to the given MQTT server. That either means the ESP isn't connected to the WiFi, the WiFi doesn't have a connection to the Internet (if the MQTT server isn't on the same WiFi) or the router does filter that traffic out (by a firewall or something similar).

I would try to remove the WiFiManager code and connect directly to the WiFi so you're sure that you got a connection and that connection isn't intercepted by the WiFiManager, for whatever reason. If that still doesn't work, check your network. In that case it's probably not a wrong sketch but a failing network configuration (not only the WiFi, it might be in some device after that, but that depends on how your network is structured, we have no clue about that.

Thanks for reply...
I found the solution in the way of adding dns in the static ip configuration.

 IPAddress _ip, _gw, _sn, _dns;
  _ip.fromString(static_ip);
  _gw.fromString(static_gw);
  _sn.fromString(static_sn);
 _sn.fromString(static_dns);

  wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn, _dns);

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