The Hidden ESP Restart Problem: Why Your ESP Sometimes Cannot Reconnect to WiFi

I recently ran into a surprisingly annoying issue while working on an ESP-based project.
Everything worked perfectly during normal operation:

  • WiFi connected successfully
  • MQTT worked
  • OTA updates worked

But after calling ESP.restart(), the ESP would sometimes take forever to reconnect to WiFi, or fail completely until another reboot.

At first, I thought the router was unstable. Turns out, the real issue was much simpler.

The Actual Problem

Before restarting the ESP, I wasn’t disconnecting from WiFi properly.
I was doing this:

ESP.restart();

But the router still thought the previous WiFi session was alive for a short period of time.

When the ESP rebooted immediately and tried to reconnect, the old session hadn’t fully timed out yet.

This issue became more obvious after I found out that after another restart, the ESP can connect normally again

The Fix

Before restarting, explicitly disconnect WiFi first.

WiFi.disconnect(true);
delay(1000);
ESP.restart();

After adding a proper disconnect and a short delay, reconnection became much more reliable.

Why This Happens

Routers maintain connection states for connected devices.
If the ESP suddenly disappears without a proper disconnect, the router may still keep the session alive temporarily or DHCP leases may not refresh immediately

Since ESP devices reboot extremely quickly, they can reconnect before the router has fully cleaned up the previous session.

Final Recommendation

Whenever restarting an ESP, do this:-

  1. Disconnect WiFi properly
  2. Stop MQTT connections if applicable
  3. Add a small delay
  4. Then restart
mqtt.disconnect();
WiFi.disconnect(true);
delay(1000);
ESP.restart();

That one small change saved me hours of debugging random reconnection issues.

Leave a Comment