Skip to content

ESP32 Loses Wi-Fi and Never Reconnects

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about ESP32 Loses Wi. We cover key concepts, practical examples, and best practices.

The Problem

ESP32 disconnects from Wi-Fi after some time and stays disconnected, requiring a manual reset.

Quick Fix

Wrong

void loop() {
  // send sensor data
  delay(10000);
}
Wi-Fi disconnects after 5 minutes. Device sends no data. Manual reset required.
void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.reconnect();
    int tries = 0;
    while (WiFi.status() != WL_CONNECTED && tries < 20) {
      delay(500); tries++;
    }
  }
  // send sensor data
  delay(500);
}
Reconnecting...
Connected to MyNetwork
Sensor data: temp=22.5C

Prevention

Call WiFi.setAutoReconnect(true) in setup. Check WiFi.status() in every loop iteration. Add exponential backoff between reconnection attempts. Use WiFi.onEvent() to register disconnection callbacks. Monitor RSSI and reconnect proactively on weak signal.

DodaTech engineers apply these same patterns when building Doda Browser's networking stack, DodaZIP's firmware packaging pipeline, and Durga Antivirus Pro's sensor communication layer.

FAQ

### How do I detect a Wi-Fi disconnection event?

Use WiFi.onEvent([](WiFiEvent_t e){ Serial.println("Disconnected"); }, WiFiEvent_t::Arduino_EVENT_WIFI_STA_DISCONNECTED); for immediate notification.

What causes frequent Wi-Fi disconnections?

Causes include power saving (call WiFi.setSleep(WIFI_PS_NONE)), channel interference, router DHCP lease expiry, and weak signal below -80 dBm.

Should I call WiFi.disconnect() before reconnect?

Yes, call WiFi.disconnect() then WiFi.reconnect() to force a fresh DHCP handshake. This clears stuck connection states.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro