Esp32 Wifi connection issue across multiple devices 'Auth expired' '4-way-handshake'

I have the same problem across multiple ESPHome devices which comes up time and again:

At some point a device running ESPHome will stop being able to connect to my WiFi network.

In the logs I then get multiple different error messages as to why the wifi won’t connect.

This is NOT an issue of incorrectly configured credentials and happens across multiple networks and HA instances I run in different homes. Different router hardware, different internet providers, different locations.

The problem also occurs when I simply flash an empty default config onto the device without anything else or when I use the captive portal in the fallback AP to reconfigure the WiFi credentials. Same goes for using the “prepare for first use” setup method using the esphome webtool.

However, if i use the arduino IDE to flash a basic webserver sketch from the examples onto the same device with identical credentials, the board connects immediately without any issues. No other variables are changed. Same cable, same location, same power source etc.

So I am pretty confident that the issue is somewhere between the router and ESPHome.

Also, I unfortunately don’t have any logs of this, but sometimes the device will randomly connect to the wifi after leaving it powered on for a day or two, and it will work normally until it is power cycled again and fails to connect.

Below is an excerpt from the logs of one of the affected devices:

[18:46:25][W][wifi:652]: Error while connecting to network.
[18:46:25][D][wifi:696]: Retrying with hidden networks...
[18:46:29][I][wifi:312]: WiFi Connecting to 'FBI Surveillance Van'...
[18:46:29][W][wifi:668]: Connecting to WiFi network failed. Are the credentials wrong?
[18:46:29][I][wifi:312]: WiFi Connecting to 'FBI Surveillance Van'...
[18:46:29][W][wifi:668]: Connecting to WiFi network failed. Are the credentials wrong?
[18:46:34][I][wifi:312]: WiFi Connecting to 'FBI Surveillance Van'...
[18:46:35][W][wifi_esp32:530][arduino_events]: Event: Disconnected ssid='FBI Surveillance Van' bssid=4A:D9:E7:0B:0F:60[redacted] reason='Association Leave'
[18:46:35][W][wifi:673]: WiFi Unknown connection status 0
[18:46:35][D][wifi:696]: Retrying with hidden networks...
[18:46:35][I][wifi:312]: WiFi Connecting to 'FBI Surveillance Van'...
[18:46:38][W][wifi_esp32:530][arduino_events]: Event: Disconnected ssid='FBI Surveillance Van' bssid=4A:D9:E7:0B:0F:60[redacted] reason='Association Expired'
[18:46:38][W][wifi:673]: WiFi Unknown connection status 0
[18:46:38][D][wifi:696]: Retrying with hidden networks...
[18:46:43][I][wifi:312]: WiFi Connecting to 'FBI Surveillance Van'...
[18:46:55][W][wifi_esp32:530][arduino_events]: Event: Disconnected ssid='FBI Surveillance Van' bssid=4A:D9:E7:0B:0F:60[redacted] reason='4-Way Handshake Timeout'
[18:46:55][W][wifi:673]: WiFi Unknown connection status 0
[18:46:55][W][wifi:689]: Restarting WiFi adapter...
[18:46:55][I][wifi:312]: WiFi Connecting to 'FBI Surveillance Van'...
[18:47:01][W][wifi_esp32:530][arduino_events]: Event: Disconnected ssid='FBI Surveillance Van' bssid=4A:D9:E7:0B:0F:60[redacted] reason='4-Way Handshake Timeout'
[18:47:01][W][wifi:673]: WiFi Unknown connection status 0
[18:47:06][D][wifi:481]: Starting scan...
[18:47:12][D][wifi:496]: Found networks:
[18:47:12][I][wifi:540]: - 'FBI Surveillance Van' (E0:E1:A9:29:35:F7) [redacted]▂▄▆█
[18:47:12][D][wifi:541]:     Channel: 6
[18:47:12][D][wifi:542]:     RSSI: -79 dB
[18:47:12][I][wifi:540]: - 'FBI Surveillance Van' (4A:D9:E7:0B:0F:60) [redacted]▂▄▆█
[18:47:12][D][wifi:541]:     Channel: 6
[18:47:12][D][wifi:542]:     RSSI: -49 dB
[18:47:12][D][wifi:545]: - 'FRITZ!Box 6660 Cable DX'[redacted] (1C:ED:6F:5A:64:30) [redacted]▂▄▆█
[18:47:12][D][wifi:545]: - 'FRITZ!Box 6660 Cable DX'[redacted] (DC:39:6F:9A:FB:1C) [redacted]▂▄▆█
[18:47:12][I][wifi:312]: WiFi Connecting to 'FBI Surveillance Van'...
[18:47:12][I][wifi:181]: Starting fallback AP!
[18:47:14][C][wifi:262]: Setting up AP...
[18:47:14][C][wifi:264]:   AP SSID: 'Tetstete Fallback Hotspot'
[18:47:14][C][wifi:265]:   AP Password: 'JsjaMfKRc3Pr'
[18:47:15][W][wifi_esp32:530][arduino_events]: Event: Disconnected ssid='FBI Surveillance Van' bssid=E0:E1:A9:29:35:F7[redacted] reason='Auth Expired'
[18:47:15][C][api:025]: Setting up Home Assistant API server...
[18:47:15][I][app:062]: setup() finished successfully!
[18:47:15][W][wifi:652]: Error while connecting to network.
[18:47:15][I][wifi:312]: WiFi Connecting to 'FBI Surveillance Van'...
[18:47:15][I][safe_mode:041]: Boot seems successful; resetting boot loop counter
[18:47:15][D][esp32.preferences:114]: Saving 1 preferences to flash...
[18:47:15][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[18:47:15][W][component:157]: Component api set Warning flag: unspecified
[18:47:15][I][app:100]: ESPHome version 2024.7.1 compiled on Jul 23 2024, 18:38:36
[18:47:15][C][wifi:599]: WiFi:
[18:47:15][C][wifi:427]:   Local MAC: A0:B7:65:4C:4B:DC
[18:47:15][C][wifi:432]:   SSID: ''[redacted]
[18:47:15][C][wifi:435]:   IP Address: 192.168.178.222
[18:47:15][C][wifi:439]:   BSSID: 00:00:00:00:00:00[redacted]
[18:47:15][C][wifi:440]:   Hostname: 'tetstete'
[18:47:15][C][wifi:442]:   Signal strength: -78 dB ▂▄▆█
[18:47:15][C][wifi:446]:   Channel: 6
[18:47:15][C][wifi:447]:   Subnet: 255.255.255.0
[18:47:15][C][wifi:448]:   Gateway: 192.168.178.1
[18:47:15][C][wifi:449]:   DNS1: 0.0.0.0
[18:47:15][C][wifi:450]:   DNS2: 0.0.0.0
[18:47:15][C][logger:185]: Logger:
[18:47:15][C][logger:186]:   Level: DEBUG
[18:47:15][C][logger:188]:   Log Baud Rate: 115200
[18:47:15][C][logger:189]:   Hardware UART: UART0
[18:47:15][C][captive_portal:088]: Captive Portal:
[18:47:15][C][mdns:116]: mDNS:
[18:47:15][C][mdns:117]:   Hostname: tetstete
[18:47:15][C][esphome.ota:073]: Over-The-Air updates:
[18:47:15][C][esphome.ota:074]:   Address: 192.168.178.222:3232
[18:47:15][C][esphome.ota:075]:   Version: 2
[18:47:15][C][esphome.ota:078]:   Password configured
[18:47:15][C][safe_mode:018]: Safe Mode:
[18:47:15][C][safe_mode:020]:   Boot considered successful after 60 seconds
[18:47:15][C][safe_mode:021]:   Invoke after 10 boot attempts
[18:47:15][C][safe_mode:023]:   Remain in safe mode for 300 seconds
[18:47:15][C][api:139]: API Server:
[18:47:16][C][api:140]:   Address: 192.168.178.222:6053
[18:47:16][C][api:142]:   Using noise encryption: YES
[18:47:16][W][wifi_esp32:530][arduino_events]: Event: Disconnected ssid='FBI Surveillance Van' bssid=E0:E1:A9:29:35:F7[redacted] reason='Auth Expired'
[18:47:16][W][wifi:652]: Error while connecting to network.
[18:47:21][D][wifi:481]: Starting scan...
[18:47:28][D][wifi:496]: Found networks:
[18:47:28][I][wifi:540]: - 'FBI Surveillance Van' (4A:D9:E7:0B:18:6D) [redacted]▂▄▆█
[18:47:28][D][wifi:541]:     Channel: 6
[18:47:28][D][wifi:542]:     RSSI: -86 dB
[18:47:28][I][wifi:540]: - 'FBI Surveillance Van' (E0:E1:A9:29:35:F7) [redacted]▂▄▆█
[18:47:28][D][wifi:541]:     Channel: 6
[18:47:28][D][wifi:542]:     RSSI: -78 dB
[18:47:28][I][wifi:540]: - 'FBI Surveillance Van' (4A:D9:E7:0B:0F:60) [redacted]▂▄▆█
[18:47:28][D][wifi:541]:     Channel: 6
[18:47:28][D][wifi:542]:     RSSI: -49 dB
[18:47:28][D][wifi:545]: - 'FRITZ!Box 6660 Cable DX'[redacted] (1C:ED:6F:5A:64:30) [redacted]▂▄▆█
[18:47:28][D][wifi:545]: - 'FRITZ!Box 7583 AV'[redacted] (3C:A6:2F:C7:49:50) [redacted]▂▄▆█
[18:47:28][D][wifi:545]: - 'FRITZ!Box 6660 Cable DX'[redacted] (DC:39:6F:9A:FB:1C) [redacted]▂▄▆█
[18:47:28][D][wifi:545]: - 'FRITZ!Box 7583 PY'[redacted] (2C:91:AB:C7:21:48) [redacted]▂▄▆█
[18:47:28][D][wifi:545]: - 'FRITZ!Box 6660 Cable DX'[redacted] (DC:15:C8:88:F8:7E) [redacted]▂▄▆█
[18:47:28][I][wifi:312]: WiFi Connecting to 'FBI Surveillance Van'...
[18:47:28][W][component:237]: Component wifi took a long time for an operation (107 ms).
[18:47:28][W][component:238]: Components should block for at most 30 ms.
[18:47:34][W][wifi_esp32:527][arduino_events]: Event: Disconnected ssid='FBI Surveillance Van' reason='Probe Request Unsuccessful'
[18:47:34][W][wifi:662]: WiFi network can not be found anymore.
[18:47:34][I][wifi:312]: WiFi Connecting to 'FBI Surveillance Van'...
[18:47:34][W][wifi:662]: WiFi network can not be found anymore.

As you can see, there are multiple different kinds of error messages which come up once this problem starts appearing, but the most common one usually is “auth expired”

I have tried all the suggestions which come up in
this thread , except for reconfiguring the channels on the router as the device has no problem connecting with the arduino script and i don’t want to mess with my network equipment.

I will say however, that similar to doubledutch in that thread, the device somtimes spontaneously connects after hours or days, but will have the same issue upon power cycling/restart and will sometimes also randomly disconnect again.

This issue has been appearing across multiple boards from multiple different batches of different manufacturers and various configs.

Often I just start from scratch by getting a new esp and a new config. I solder all my boards in such a way that i can swap the boards by now just because of this.

But the amount of ESPs I am going through is slowly getting ridiculousl, and I am rather annoyed that I can’t rely on my devices for critical applications because of this.

Also, I am running out of non-esphome projects to use all of these microcontrollers on and there frankly HAS to be a satisfying solution to this.

I’ll gladly put in whatever it takes to solve this issue as far as I am able to, but this is relaly driving me nuts and I NEED a solution to this

I have managed to circumvent the issue by assigning the device a static IP which has never been used on my network before and by also changing the name: in the config, generating a hostname which has also never previously been used on my network before.

This is an acceptable workaround, but if someone can explain/retrace what exactly happened here and how to avoid it in the future, I’d be really interested in what is happening here