ESP8266 connection to MQTT server (Attempting MQTT connection...failed, rc=-2)
Recently I had a problem when trying to program NodeMCU with Arduino. I flashed arduino with basically default pubsubclient test program. After programming NodeMCU connected successfully to Wifi, then tried to connect MQTT server and failed: WiFi connected Attempting MQTT connection...connected ............Attempting MQTT connection...connected ............Attempting MQTT connection...failed, rc=-2 try again in 5 seconds I researched and tested many Arduino scripts. All failed with the same error -2. I logged into the MQTT server and watched logs. The NodeMCU didn't even connect to the MQTT server. I was clueless. Luckily I bought a few NodeMCUs from different sellers. Opened up another NodeMCU and hooked it up. Flashed the exact same code to it and it connected and worked like charm. Then I tried to flash the first problematic NodeMCU with Arduino and exactly the same error -2. I had also a ESP12E that had the -2 error before. Flashed it with the same code, still error -2.