Esp8266 disconnects from WIFI and doesn't reconnect automatically

If you are asking a question, please follow this template:

  1. Esp8266 disconnects from station wifi after a few hours and doesn’t reconnect automatically. Need to restart the chip by recycling power.

  2. Here is the log output

mongoose.c:4912         0x3fff12ec mbedtls_ssl_flush_output() returned -78 (-0x004e)
mongoose.c:4912         0x3fff12ec mbedtls_ssl_write_record() returned -78 (-0x004e)
mongoose.c:4912         0x3fff12ec mbedtls_ssl_send_alert_message() returned -78 (-0x004e)
mgos_mqtt_conn.c:256    MQTT0 Disconnect
mgos_mqtt_conn.c:549    MQTT0 connecting after 2096 ms
mgos_mqtt_conn.c:471    MQTT0 connecting to xxx.iot.us-east-2.amazonaws.com:8883
main.c:113              /zzz_UPSTREAM -> {"data":{"eparams":{"AIRMS":0.00,"AWATT":0.00,"BIRMS":0.00,"BWATT":0.00,"CIRMS":0.00,"CWATT":0.00},"meters":{"AMETER":0.01,"BMETER":0.01,"CMETER":0.01}}}
mongoose.c:12068        Failed to resolve 'xxx.iot.us-east-2.amazonaws.com', server 192.168.31.1
mgos_mqtt_conn.c:229    MQTT0 TCP connect error (-1)
mgos_mqtt_conn.c:256    MQTT0 Disconnect
mgos_mqtt_conn.c:549    MQTT0 connecting after 3640 ms
mgos_mqtt_conn.c:471    MQTT0 connecting to xxx.iot.us-east-2.amazonaws.com:8883
main.c:113              /zzz_UPSTREAM -> {"data":{"eparams":{"AIRMS":0.00,"AWATT":0.00,"BIRMS":0.00,"BWATT":0.00,"CIRMS":0.00,"CWATT":0.00},"meters":{"AMETER":0.01,"BMETER":0.01,"CMETER":0.01}}}
mongoose.c:12068        Failed to resolve 'xxx.iot.us-east-2.amazonaws.com', server 192.168.31.1
mgos_mqtt_conn.c:229    MQTT0 TCP connect error (-1)
mgos_mqtt_conn.c:256    MQTT0 Disconnect
mgos_mqtt_conn.c:549    MQTT0 connecting after 8318 ms
mgos_mqtt_conn.c:471    MQTT0 connecting to xxx.iot.us-east-2.amazonaws.com:8883
main.c:113              /zzz_UPSTREAM -> {"data":{"eparams":{"AIRMS":0.00,"AWATT":0.00,"BIRMS":0.00,"BWATT":0.00,"CIRMS":0.00,"CWATT":0.00},"meters":{"AMETER":0.01,"BMETER":0.01,"CMETER":0.01}}}
main.c:113              /zzz_UPSTREAM -> {"data":{"eparams":{"AIRMS":0.00,"AWATT":0.00,"BIRMS":0.00,"BWATT":0.00,"CIRMS":0.00,"CWATT":0.00},"meters":{"AMETER":0.01,"BMETER":0.01,"CMETER":0.01}}}
mongoose.c:12068        Failed to resolve 'xxx.iot.us-east-2.amazonaws.com', server 192.168.31.1
mgos_mqtt_conn.c:229    MQTT0 TCP connect error (-1)
mgos_mqtt_conn.c:256    MQTT0 Disconnect
mgos_mqtt_conn.c:549    MQTT0 connecting after 17464 ms
mgos_mqtt_conn.c:471    MQTT0 connecting to xxx.iot.us-east-2.amazonaws.com:8883
main.c:113              /zzz_UPSTREAM -> {"data":{"eparams":{"AIRMS":0.00,"AWATT":0.00,"BIRMS":0.00,"BWATT":0.00,"CIRMS":0.00,"CWATT":0.00},"meters":{"AMETER":0.01,"BMETER":0.01,"CMETER":0.01}}}
mongoose.c:12068        Failed to resolve 'xxx-ats.iot.us-east-2.amazonaws.com', server 192.168.31.1
mgos_mqtt_conn.c:229    MQTT0 TCP connect error (-1)
mgos_mqtt_conn.c:256    MQTT0 Disconnect
mgos_mqtt_conn.c:549    MQTT0 connecting after 30614 ms
mgos_mqtt_conn.c:471    MQTT0 connecting to xxx.iot.us-east-2.amazonaws.com:8883
main.c:113              /zzz_UPSTREAM -> {"data":{"eparams":{"AIRMS":0.00,"AWATT":0.00,"BIRMS":0.00,"BWATT":0.00,"CIRMS":0.00,"CWATT":0.00},"meters":{"AMETER":0.01,"BMETER":0.01,"CMETER":0.01}}}
mongoose.c:12068        Failed to resolve 'xxx.iot.us-east-2.amazonaws.com', server 192.168.31.1
mgos_mqtt_conn.c:229    MQTT0 TCP connect error (-1)
mgos_mqtt_conn.c:256    MQTT0 Disconnect
mgos_mqtt_conn.c:549    MQTT0 connecting after 55514 ms
mgos_mqtt_conn.c:471    MQTT0 connecting to xxx-east-2.amazonaws.com:8883
  • Please help me with the quick fix for this asap.

Doesn’t look like you have shared enough logs. I’d assume you need to at least share the events related to WiFi (rather than MQTT) for anyone to be able to help

This is the total log. The problem starts from these lines
mongoose.c:4912 0x3fff12ec mbedtls_ssl_flush_output() returned -78 (-0x004e)
mongoose.c:4912 0x3fff12ec mbedtls_ssl_write_record() returned -78 (-0x004e)
mongoose.c:4912 0x3fff12ec mbedtls_ssl_send_alert_message() returned -78 (-0x004e)

After that, the log sort of keeps repeating.

There is no evidence in your log of wifi disconnection.
This line

Failed to resolve 'xxx.iot.us-east-2.amazonaws.com', server 192.168.31.1

shows a DNS issue rather than a wifi one.

Here is even more detailed log. The issue starts when esp8266 disconnects from WIFI and doesn’t automatically reconnect.

main.c:113 /zzz_UPSTREAM -> {“data”:{“eparams”:{“AIRMS”:0.00,“AWATT”:0.00,“BIRMS”:0.00,“BWATT”:0.00,“CIRMS”:0.00,“CWATT”:0.00},“meters”:{“AMETER”:0.01,“BMETER”:0.01,“CMETER”:0.01}}}
main.c:113 /zzz_UPSTREAM -> {“data”:{“eparams”:{“AIRMS”:0.00,“AWATT”:0.00,“BIRMS”:0.00,“BWATT”:0.00,“CIRMS”:0.00,“CWATT”:0.00},“meters”:{“AMETER”:0.01,“BMETER”:0.01,“CMETER”:0.01}}}
mongoose.c:4912 0x3fff12ec mbedtls_ssl_flush_output() returned -78 (-0x004e)
mongoose.c:4912 0x3fff12ec mbedtls_ssl_write_record() returned -78 (-0x004e)
mongoose.c:4912 0x3fff12ec mbedtls_ssl_send_alert_message() returned -78 (-0x004e)
mgos_mqtt_conn.c:256 MQTT0 Disconnect
mgos_mqtt_conn.c:549 MQTT0 connecting after 2096 ms
mgos_mqtt_conn.c:471 MQTT0 connecting to xxx.us-east-2.amazonaws.com:8883
main.c:113 /zzz_UPSTREAM -> {“data”:{“eparams”:{“AIRMS”:0.00,“AWATT”:0.00,“BIRMS”:0.00,“BWATT”:0.00,“CIRMS”:0.00,“CWATT”:0.00},“meters”:{“AMETER”:0.01,“BMETER”:0.01,“CMETER”:0.01}}}
mongoose.c:12068 Failed to resolve ‘xxx.us-east-2.amazonaws.com’, server 192.168.31.1
mgos_mqtt_conn.c:229 MQTT0 TCP connect error (-1)
mgos_mqtt_conn.c:256 MQTT0 Disconnect
mgos_mqtt_conn.c:549 MQTT0 connecting after 3640 ms
mgos_mqtt_conn.c:471 MQTT0 connecting to xxx.iot.us-east-2.amazonaws.com:8883
main.c:113 /zzz_UPSTREAM -> {“data”:{“eparams”:{“AIRMS”:0.00,“AWATT”:0.00,“BIRMS”:0.00,“BWATT”:0.00,“CIRMS”:0.00,“CWATT”:0.00},“meters”:{“AMETER”:0.01,“BMETER”:0.01,“CMETER”:0.01}}}
mongoose.c:12068 Failed to resolve ‘xxx.iot.us-east-2.amazonaws.com’, server 192.168.31.1
mgos_mqtt_conn.c:229 MQTT0 TCP connect error (-1)
mgos_mqtt_conn.c:256 MQTT0 Disconnect
mgos_mqtt_conn.c:549 MQTT0 connecting after 8318 ms
mgos_mqtt_conn.c:471 MQTT0 connecting to xxx.iot.us-east-2.amazonaws.com:8883
main.c:113 /zzz_UPSTREAM -> {“data”:{“eparams”:{“AIRMS”:0.00,“AWATT”:0.00,“BIRMS”:0.00,“BWATT”:0.00,“CIRMS”:0.00,“CWATT”:0.00},“meters”:{“AMETER”:0.01,“BMETER”:0.01,“CMETER”:0.01}}}
main.c:113 /zzz_UPSTREAM -> {“data”:{“eparams”:{“AIRMS”:0.00,“AWATT”:0.00,“BIRMS”:0.00,“BWATT”:0.00,“CIRMS”:0.00,“CWATT”:0.00},“meters”:{“AMETER”:0.01,“BMETER”:0.01,“CMETER”:0.01}}}
mongoose.c:12068 Failed to resolve ‘xxx.iot.us-east-2.amazonaws.com’, server 192.168.31.1
mgos_mqtt_conn.c:229 MQTT0 TCP connect error (-1)
mgos_mqtt_conn.c:256 MQTT0 Disconnect
mgos_mqtt_conn.c:549 MQTT0 connecting after 17464 ms
mgos_mqtt_conn.c:471 MQTT0 connecting to xxx.iot.us-east-2.amazonaws.com:8883
main.c:113 /zzz_UPSTREAM -> {“data”:{“eparams”:{“AIRMS”:0.00,“AWATT”:0.00,“BIRMS”:0.00,“BWATT”:0.00,“CIRMS”:0.00,“CWATT”:0.00},“meters”:{“AMETER”:0.01,“BMETER”:0.01,“CMETER”:0.01}}}
mongoose.c:12068 Failed to resolve ‘xxx.iot.us-east-2.amazonaws.com’, server 192.168.31.1
mgos_mqtt_conn.c:229 MQTT0 TCP connect error (-1)
mgos_mqtt_conn.c:256 MQTT0 Disconnect
mgos_mqtt_conn.c:549 MQTT0 connecting after 30614 ms
mgos_mqtt_conn.c:471 MQTT0 connecting to xxx.iot.us-east-2.amazonaws.com:8883
main.c:113 /zzz_UPSTREAM -> {“data”:{“eparams”:{“AIRMS”:0.00,“AWATT”:0.00,“BIRMS”:0.00,“BWATT”:0.00,“CIRMS”:0.00,“CWATT”:0.00},“meters”:{“AMETER”:0.01,“BMETER”:0.01,“CMETER”:0.01}}}
mongoose.c:12068 Failed to resolve ‘xxx.iot.us-east-2.amazonaws.com’, server 192.168.31.1
mgos_mqtt_conn.c:229 MQTT0 TCP connect error (-1)
mgos_mqtt_conn.c:256 MQTT0 Disconnect
mgos_mqtt_conn.c:549 MQTT0 connecting after 55514 ms
mgos_mqtt_conn.c:471 MQTT0 connecting to xxx.iot.us-east-2.amazonaws.com:8883
main.c:113 /zzz_UPSTREAM -> {“data”:{“eparams”:{“AIRMS”:0.00,“AWATT”:0.00,“BIRMS”:0.00,“BWATT”:0.00,“CIRMS”:0.00,“CWATT”:0.00},“meters”:{“AMETER”:0.01,“BMETER”:0.01,“CMETER”:0.01}}}
esp_main.c:145 SDK: state: 5 -> 2 (3c0)
esp_main.c:145 SDK: rm 0
mgos_wifi.c:118 WiFi STA: Disconnected, reason: 3
mgos_net.c:82 WiFi STA: disconnected
mgos_net.c:86 WiFi STA: connecting
esp_main.c:145 SDK: scandone
esp_main.c:145 SDK: state: 2 -> 2 (b0)
mongoose.c:12068 Failed to resolve ‘xxx.iot.us-east-2.amazonaws.com’, server 192.168.31.1
mgos_mqtt_conn.c:229 MQTT0 TCP connect error (-1)
mgos_mqtt_conn.c:256 MQTT0 Disconnect
mgos_mqtt_conn.c:549 MQTT0 connecting after 57762 ms
esp_main.c:145 SDK: state: 2 -> 0 (2)
mgos_wifi.c:118 WiFi STA: Disconnected, reason: 2
mgos_net.c:82 WiFi STA: disconnected
mgos_wifi.c:345 WiFi STA: Connect timeout
mgos_wifi.c:88 WiFi STA: Using config 0 (Xiaomi_EFB4)
mgos_wifi.c:270 WiFi STA: Connecting to Xiaomi_EFB4
mgos_net.c:86 WiFi STA: connecting
esp_main.c:145 SDK: scandone
esp_main.c:145 SDK: state: 0 -> 2 (b0)
esp_main.c:145 SDK: state: 2 -> 3 (0)
esp_main.c:145 SDK: state: 3 -> 5 (10)
esp_main.c:145 SDK: add 0
esp_main.c:145 SDK: aid 3
esp_main.c:145 SDK: cnt
esp_main.c:145 SDK: state: 5 -> 2 (fc0)
esp_main.c:145 SDK: rm 0
mgos_wifi.c:118 WiFi STA: Disconnected, reason: 15
mgos_net.c:82 WiFi STA: disconnected
mgos_mqtt_conn.c:471 MQTT0 connecting to xxx.iot.us-east-2.amazonaws.com:8883
mgos_wifi.c:345 WiFi STA: Connect timeout
mgos_wifi.c:88 WiFi STA: Using config 0 (Xiaomi_EFB4)
esp_main.c:145 SDK: state: 2 -> 0 (0)
mgos_wifi.c:270 WiFi STA: Connecting to Xiaomi_EFB4
mgos_wifi.c:118 WiFi STA: Disconnected, reason: 202
mgos_net.c:86 WiFi STA: connecting
mgos_net.c:82 WiFi STA: disconnected
esp_main.c:145 SDK: scandone
esp_main.c:145 SDK: state: 0 -> 2 (b0)
esp_main.c:145 SDK: state: 2 -> 3 (0)
esp_main.c:145 SDK: state: 3 -> 5 (10)
esp_main.c:145 SDK: add 0
esp_main.c:145 SDK: aid 3
esp_main.c:145 SDK: cnt
main.c:113 /zzz_UPSTREAM -> {“data”:{“eparams”:{“AIRMS”:0.00,“AWATT”:0.00,“BIRMS”:0.00,“BWATT”:0.00,“CIRMS”:0.00,“CWATT”:0.00},“meters”:{“AMETER”:0.01,“BMETER”:0.01,“CMETER”:0.01}}}
mongoose.c:12068 Failed to resolve ‘xxx.iot.us-east-2.amazonaws.com’, server 192.168.31.1
mgos_mqtt_conn.c:229 MQTT0 TCP connect error (-1)
mgos_mqtt_conn.c:256 MQTT0 Disconnect
mgos_mqtt_conn.c:549 MQTT0 connecting after 58023 ms
esp_main.c:145 SDK: state: 5 -> 2 (fc0)
esp_main.c:145 SDK: rm 0
mgos_wifi.c:118 WiFi STA: Disconnected, reason: 15
mgos_net.c:82 WiFi STA: disconnected
mgos_wifi.c:345 WiFi STA: Connect timeout
mgos_wifi.c:88 WiFi STA: Using config 0 (Xiaomi_EFB4)
esp_main.c:145 SDK: state: 2 -> 0 (0)
mgos_wifi.c:270 WiFi STA: Connecting to Xiaomi_EFB4
mgos_wifi.c:118 WiFi STA: Disconnected, reason: 202
mgos_net.c:86 WiFi STA: connecting
mgos_net.c:82 WiFi STA: disconnected
esp_main.c:145 SDK: scandone
esp_main.c:145 SDK: state: 0 -> 2 (b0)
esp_main.c:145 SDK: state: 2 -> 3 (0)
esp_main.c:145 SDK: state: 3 -> 5 (10)
esp_main.c:145 SDK: add 0
esp_main.c:145 SDK: aid 3
esp_main.c:145 SDK: cnt
esp_main.c:145 SDK: state: 5 -> 2 (fc0)
esp_main.c:145 SDK: rm 0
mgos_wifi.c:118 WiFi STA: Disconnected, reason: 15
mgos_net.c:82 WiFi STA: disconnected
mgos_wifi.c:345 WiFi STA: Connect timeout
mgos_wifi.c:88 WiFi STA: Using config 0 (Xiaomi_EFB4)
esp_main.c:145 SDK: state: 2 -> 0 (0)
mgos_wifi.c:270 WiFi STA: Connecting to Xiaomi_EFB4
mgos_wifi.c:118 WiFi STA: Disconnected, reason: 202
mgos_net.c:86 WiFi STA: connecting
mgos_net.c:82 WiFi STA: disconnected
esp_main.c:145 SDK: scandone
esp_main.c:145 SDK: state: 0 -> 2 (b0)
esp_main.c:145 SDK: state: 2 -> 3 (0)
esp_main.c:145 SDK: state: 3 -> 5 (10)
esp_main.c:145 SDK: add 0
esp_main.c:145 SDK: aid 3
esp_main.c:145 SDK: cnt
mgos_mqtt_conn.c:471 MQTT0 connecting to xxx.iot.us-east-2.amazonaws.com:8883
esp_main.c:145 SDK: state: 5 -> 2 (fc0)
esp_main.c:145 SDK: rm 0
mgos_wifi.c:118 WiFi STA: Disconnected, reason: 15
mgos_net.c:82 WiFi STA: disconnected
main.c:113 /zzz_UPSTREAM -> {“data”:{“eparams”:{“AIRMS”:0.00,“AWATT”:0.00,“BIRMS”:0.00,“BWATT”:0.00,“CIRMS”:0.00,“CWATT”:0.00},“meters”:{“AMETER”:0.01,“BMETER”:0.01,“CMETER”:0.01}}}
mongoose.c:12068 Failed to resolve ‘xxx.iot.us-east-2.amazonaws.com’, server 192.168.31.1
mgos_mqtt_conn.c:229 MQTT0 TCP connect error (-1)
mgos_mqtt_conn.c:256 MQTT0 Disconnect
mgos_mqtt_conn.c:549 MQTT0 connecting after 64674 ms
mgos_wifi.c:345 WiFi STA: Connect timeout
mgos_wifi.c:88 WiFi STA: Using config 0 (Xiaomi_EFB4)
esp_main.c:145 SDK: state: 2 -> 0 (0)
mgos_wifi.c:270 WiFi STA: Connecting to Xiaomi_EFB4
mgos_wifi.c:118 WiFi STA: Disconnected, reason: 202
mgos_net.c:86 WiFi STA: connecting
mgos_net.c:82 WiFi STA: disconnected
esp_main.c:145 SDK: scandone
esp_main.c:145 SDK: state: 0 -> 2 (b0)
esp_main.c:145 SDK: state: 2 -> 3 (0)
esp_main.c:145 SDK: state: 3 -> 5 (10)

In my experience I’ve seen thinks like this when my WiFi router starts to play up. (202 and 3)
From memory I would get reason 15 when I got the WiFi password wrong, but that doesn’t seem to match what you are saying.

When I’ve been doing lots of connects/disconnects as part of dev work I’ve seen this, then resolved by restarting my router.

The thing is that esp8266 connects to the same router when restarted. If it is a router issue, the chip shouldn’t connect even after restarting. And this is happening with many different routers. The chips show the same behavior after every 6-12 hours. This issue never happens with ESP32 chip.

Does it solve with a reset or a power-on reset ?
Looks like your comms are lost and the OS decides to disconnect and reconnect after realizing something is wrong.
I’ve seen this happening with some Access Points and after a new program load.
How is your heap before this happens ? Are you checking MQTT send function’s return value ?

I’ve come across exactly this issue on a small number of our production devices (ESP32) and never managed to get to the bottom of it. We find that devices disconnect from MQTT server periodically, MOS reports that it’s unable to connect to the MQTT server and the TCP connection fails. UDP stays ‘up’ and we’re able to receive logs from the device over the internet over UDP. The only way to resolve the issue is to reboot the device, which causes it to immediately reconnect.

The only thing I’ve been able to find that gives any clues at all is that many of my customers seeing this issue had an LG Smart TV connected to the same router. Absolutely no idea why it’s related, but turning off the TV usually caused the disconnect issues to stop. Weird, huh?

Weird… I’ve read of some multicast issues with some WiFi routers due to Smart TVs, but…
Are you able to sniff some traffic when this is happening ? Sometimes when routers go nuts they discard some protocol or even some address, perhaps a new address assignment clears something inside… I’d like to see if the TCP SYNs are going out as expected to properly blame the router
On the other side, did you check heap memory usage ? MQTT can queue and queue hoping that it will finally get through.

Unfortunately it’s only ever happened on remote production devices with clients who don’t know how to setup packet filtering so I’m working blind. We did put in a script to report on heap whenever it failed but that didn’t seem to be an issue.

This is a serious issue on Esp8266. Mongoose os is simply not built for it I guess. It has cost me a lot of money as many of my devices have rendered useless. Restarting the device connects it to the mqtt server right away but it keeps stopping after every 1 or 2 days. I suggest dont use mos with es8266

I don’t believe it’s got anything to do with Mongoose-os. Our devices are ESP32, not ESP8266 - I believe it’s a problem somewhere in the ESP-IDF, but somehow linked to a very obscure external influence on the local network - until we can find somebody who’s having the issue who is able to get some Wireshark packet filtering to send across to the team at Espressif, I don’t think we’ll solve the issue. It only occurs on about 0.5% of my production devices.