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.

Then I downloaded esptool, hooked up known working NodeMCU and dumped the firmware:
python esptool.py --port /dev/ttyUSB0 --baud 115200 read_flash 0x00000 0x400000 backup.img
 Disconnected the working NodeMCU, hooked up not working MCU and restored the firmware:
python esptool.py --port /dev/ttyUSB0 write_flash -fm qio 0x00000 backup.img
Resetted the NodeMCU and it worked. I was amazed? Why did it work? Why didn't it work before. Then I tried to flash the same code again with arduino, and it still worked. Why? I don't know. I think that the default firmware the came with NodeMCU was problematic.

Then I flashed the ESP12 with the same backup and it worked. Then flashed my own code to ESP12 with Arduino and it still worked.

For networking I have Tp-Link routers with Openwrt 18.06. And for MQTT server I had a server that was running on Xigmanas Virtualbox. I also had one OrangePi Zero that I tested with the NodeMCU.

Comments

Popular posts from this blog

ZTE MF29A router and Openwrt

Audi A6 C4 Webasto DBW46