ESP32 Keeps Disconnecting From Wi-Fi in the Field: How I Make IoT Devices Reconnect Reliably
Devices that work on the office network but drop offline at customer sites are one of the most common IoT complaints. Here is how I fix it.
The problem clients bring: "Our ESP32 devices work perfectly in the office, but at customer sites they go offline every few days and only come back after a power cycle." Support tickets pile up and field visits get expensive.
What is really going wrong
- The firmware connects once at boot and never handles disconnect events properly.
- Router reboots, DHCP lease changes and channel changes are not handled, so the device sits in a half-connected state.
- The MQTT or HTTP client hangs on a dead socket with no timeout.
- Power dips during Wi-Fi transmit bursts cause brown-out resets that look like random crashes.
How I solve it
- Build a proper connection state machine that reacts to every disconnect reason and retries with back-off.
- Add timeouts and keep-alives to the network client so a dead connection is detected in seconds, not hours.
- Add a watchdog that resets only the network stack, and the whole device only as a last resort.
- Log the reset reason and disconnect causes, so field units report what actually happened.
- Check the power supply under transmit load and fix the hardware if it sags.
What the client gets
Devices that recover on their own from router reboots and network changes, plus field logs that turn the next support ticket into a quick diagnosis instead of a site visit.