Back to Troubleshooting

ESP32 Wi-Fi Disconnects Due to Sleep Mode

Understand how ESP32's Wi-Fi sleep settings affect connectivity and how to disable or tune them for better network stability.

Common Causes

  • Wi-Fi Power Save Mode Enabled by Default

    ESP32 enables Wi-Fi modem sleep by default, which periodically disables parts of the transceiver to save power.

  • 🔋

    Incompatible Router Beacon Interval

    Some routers use beacon timings that don’t align with the ESP32’s modem sleep wake cycles, leading to missed packets and disconnects.

  • ⚠️

    Blocking Operations During Sleep

    If your application performs blocking operations or disables background tasks, the Wi-Fi stack may not wake in time to stay synchronized.

  • 🧯

    Unstable Power Supply Exacerbated by Sleep Transitions

    Voltage dips during sleep-to-wake transitions can cause instability if the power supply is marginal.

Symptoms

Disconnects Happen Periodically

Wi-Fi connection drops after several minutes, especially when idle or under low traffic.

Works When Actively Sending Data

Connection stays alive during active transfers, but drops when idle or in low-power states.

Improves After Disabling Sleep

Adding WiFi.setSleep(false) fixes random disconnects, confirming sleep-related issues.

Ping or MQTT Loss During Idle

Devices lose pings or fail MQTT keepalive due to modem sleeping during handshake windows.

Solutions

Disable Wi-Fi Sleep Mode

Call WiFi.setSleep(false) after WiFi.begin() to keep the radio always on and maintain continuous link.

Tune Keepalive Intervals

Increase MQTT or TCP keepalive intervals and beacon listen windows to tolerate modem sleep better.

Switch Router to 20 MHz Only

Some ESP32 modules perform better when the Wi-Fi channel is set to 20 MHz instead of 40 MHz in the router settings.

Improve Power Supply Stability

Ensure the 3.3V regulator can handle sleep-wake transitions without brownouts or glitches.

More Details

ESP32 Wi-Fi Disconnects Due to Sleep Mode #

Your ESP32 boots and connects to Wi-Fi successfully, but the connection drops after a few minutes of inactivity. Or your MQTT messages stop arriving after a while. If this sounds familiar, Wi-Fi sleep mode might be the culprit.

By default, the ESP32 uses modem sleep — a low-power mode that periodically powers down parts of the Wi-Fi radio. While this saves energy, it can cause intermittent disconnections or missed packets, especially on certain routers or with specific network settings.


Why Wi-Fi Sleep Causes Problems #

The ESP32’s modem sleep is enabled automatically when using WiFi.begin() in most frameworks. This mode is designed to reduce power consumption by allowing the chip to “doze off” between beacon intervals.

However, the following issues may arise:

  • The ESP32 may miss beacons or keepalive packets if sleep timing mismatches the router’s configuration
  • MQTT or TCP connections may time out if the ESP32 sleeps during handshake or ping intervals
  • Some routers don’t tolerate the ESP32’s low-power behavior well and force a disconnection

⏱ Disconnects After Idle #

If your ESP32 stays connected while transferring data but drops when idle (e.g., during delay()), it's likely sleeping.

📉 MQTT Keepalive or Ping Failures #

MQTT clients may drop the connection due to missed pings:

[MQTT] Connection lost: reason=ping timeout

🔁 Connection Recovers with WiFi.setSleep(false) #

If you add this line:

WiFi.setSleep(false);

and the disconnections stop, the issue was sleep mode.

🔋 Connection Drops When Router Reboots #

Modem sleep can also interfere with reconnect timing if the router has longer beacon intervals or takes time to reboot.

How to Disable Wi-Fi Sleep Mode #

Add this after connecting to Wi-Fi:

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
WiFi.setSleep(false);

This keeps the Wi-Fi modem fully powered and responsive at all times.

You can verify sleep mode with:

Serial.println(WiFi.getSleep());

Other Mitigation Strategies #

🛠 Tune MQTT Keepalive and TCP Options #

Increase keepalive intervals or reduce ping frequency to give the ESP32 more time between activity:

mqttClient.setKeepAlive(60);  // Increase from default 15-30 seconds

Also consider enabling automatic reconnect in your MQTT library.

📡 Adjust Router Settings #

Some routers use 40 MHz Wi-Fi channels or long beacon intervals (e.g. 1024 ms) that the ESP32 may not handle well in sleep. In your router’s advanced settings:

  • Use 20 MHz only (improves ESP32 compatibility)

  • Set beacon interval to 100–300 ms

  • Avoid mesh or AP isolation modes

🔋 Ensure Stable Power Supply

Sleep-to-wake transitions involve rapid changes in current demand. If your 3.3V LDO or USB power is weak, these transitions can brown out the radio or CPU.

Use:

  • A low dropout, fast-response regulator (like AMS1117 or TLV75533)

  • A decoupling capacitor near the ESP32 (10–47 µF)

Conclusion #

Wi-Fi sleep mode on ESP32 is power-efficient but can cause intermittent connectivity problems — especially for applications that expect 24/7 uptime.

If you're seeing random disconnects or ping loss:

  • Disable sleep with WiFi.setSleep(false)

  • Monitor power stability

  • Tune router and MQTT settings

These adjustments make a huge difference in reliability, particularly for smart home or long-running IoT applications.

Still Stuck with an ESP32 Issue? Let's solve it together.

Our interactive troubleshooting wizard will guide you through common ESP32 problems and their solutions, step by step.

No registration required. Start solving issues right away!