ESP32 Arduino Core v2 vs v3: WiFi Reconnect
The first Wi-Fi try after a reset often fails. Here is why, what changed between Arduino core 2.0.17 and core 3.x, and the one reconnect that works on both.
Why the First Connect After a Reset Fails
Press reset, or pull power, while the board is still joined to the router. The ESP32 never sends a goodbye. Many routers keep the old session for that MAC address, so the first connect attempt or two times out. A later try usually gets through.
Espressif tracked this in esp-idf issue 12315. Reset the board without a clean disconnect and the first try or two fails. Disconnect first, then reset, and the first try succeeds. It depends on the router. The same reset is fine on some access points and repeatable on others. When the router still holds the previous session, the next join can fail in more than one way: no answer during authentication, no DHCP, and so on.
The Arduino core gives you one free retry on that first disconnect. That retry is the fix in arduino-esp32 pull request 6359, written because a restart onto the same access point often connected only on the second try. Let that free retry run. Start your own retry from the main loop, not from inside the Wi-Fi event handler.
If you are about to reset or enter deep sleep on purpose, say goodbye first:
WiFi.disconnect();
delay(100);WiFi.reconnect() Is Not the Same Call on Both Cores
This was checked in the core source: Arduino core 2.0.17 against core 3.3.12.
On 2.0.17, WiFi.reconnect() always disconnects, then connects again.
On 3.x, WiFi.reconnect() disconnects only if the station is already connected. Call it in the middle of a failed attempt and it does nothing. The driver reports sta is connecting and the retry never restarts.
A retry that calls WiFi.reconnect() restarts the join on 2.0.17. On 3.x, in the middle of a failed attempt, that same call does not. The portable restart is WiFi.disconnect(false) followed by WiFi.begin().
Which Disconnect Reasons the Core Retries
Auto-reconnect is on unless you turn it off. After the one free retry, the core keeps going only for reason codes on its list.
| Code | Name | On the Auto-Retry List? |
|---|---|---|
| 2 | AUTH_EXPIRE | Yes. |
| 4 | ASSOC_EXPIRE | Yes. This is what core 3.x reports for the failed connect above. Leave auto-reconnect on and 3.x keeps firing retries. That is the retry storm. |
| 39 | TIMEOUT | No. This is what core 2.0.17 reports for that same failure. After the free retry, 2.x stops. Auto-reconnect will not try again. |
| 203 | ASSOC_FAIL | On 2.0.17 this is the dead end people quote: "ASSOC_FAIL never reconnects." That is true only when auto-reconnect is off. With auto-reconnect on, ASSOC_FAIL is on the retry list. |
Turn auto-reconnect off on both cores, and do the retries yourself. Then the sketch does not depend on which reason code that core decided to care about.
What the OhioIoT SDK Does
The SDK uses one reconnect for both cores. Minimalist, Scaler, and Champion all share it, and controller.loop() is the call you write. You do not branch on the core version.
- It turns auto-reconnect off with
WiFi.setAutoReconnect(false), so 3.x cannot storm and 2.x cannot stop on a reason the list ignores. - It calls
WiFi.persistent(false), so the core does not rewrite the network into flash on every boot. - It lets the core's one free retry run, and it ignores
ASSOC_LEAVE. That reason means the sketch asked to disconnect. - When it tries again, it does not call
WiFi.reconnect(). It callsWiFi.disconnect(false), thenWiFi.begin()with your network name and password. - The wait before the first retry you start is about 2 seconds. Each retry after that waits twice as long — 4 seconds, 8, 16 — and the wait stops growing at 60 seconds.
WiFi.setAutoReconnect(false);
WiFi.persistent(false);
// From the main loop, when it is time to try again.
// Not from inside the Wi-Fi event handler.
WiFi.disconnect(false);
WiFi.begin(ssid, password);disconnect(false) drops the current attempt and leaves the radio on. begin starts a fresh join. That pair is valid on 2.0.17 and on 3.x.
Stay on Core 2.0.17 for Now
OhioIoT recommends Arduino core 2.0.17 until we say otherwise.
- The official PlatformIO 6.x line still ships it. Platform 6.13.0, the newest 6.x release, is still Arduino core 2.0.17. That is the core the SDK is tested on. The newest official platform is 8.0.0, and that one ships core 3.3.12, so an unpinned
platform = espressif32can land on 3.x. Pin 6.x. - Core 3.x Wi-Fi does not behave the same way. The reason codes and
WiFi.reconnect()differ, as above, and the low-level Wi-Fi driver is closed source. When a retry goes wrong you cannot open that driver and patch it. - Core 4 is already in release candidate. Espressif's Arduino core roadmap has 4.0 on ESP-IDF 6.1. Jumping to 3.x now means moving again soon.
Pin It in platformio.ini
In your platformio.ini, set the platform to the official 6.x package:
platform = platformio/espressif32 @ ^6platformio/espressif32 is the official PlatformIO package.@ ^6 means version 6.x only, which is core 2.0.17. That pin stays off platform 8.0.0 (core 3.3.12) and off the 55.x community platform.
platform = espressif32 follows whatever is newest. As of platform 8.0.0, newest is Arduino core 3.3.12.How to Tell Which Core You Are On
Build the project and read the header PlatformIO prints before the compile. The line that starts with PLATFORM is the one that matters.
PLATFORM: Espressif 32 (6.13.0)— platform 6.x. Official PlatformIO. Arduino core 2.0.17. This is the one you want. The packages list under that header also names the core in parentheses, for example(2.0.17).PLATFORM: Espressif 32 (55.03.311)— platform 55.x (the numbers after 55 vary). That is the pioarduino community platform, and it ships Arduino core 3.x.PLATFORM: Espressif 32 (8.0.0)— official PlatformIO 8.x. This one ships Arduino core 3.3.12. Pin@ ^6to leave it.
A 6 at the start of that version is core 2.0.17. A 55 is the community 3.x platform. An 8 is the new official 3.x platform.
In the Arduino IDE, open Tools → Board → Boards Manager and find esp32 by Espressif Systems. Install 2.0.17.
Video and the Espressif Threads
- ESP32 WiFi - Reconnect The Right Way. The video is worth watching. Two lines in it apply to core 2.x only:
WiFi.reconnect()means "disconnect, then connect" on 2.x, and "ASSOC_FAIL never reconnects" is true only with auto-reconnect off. Pin the core, or usedisconnect(false)andbegin, as above. - esp-idf issue 12315 — the first connection attempts fail when the router still holds the old session.
- arduino-esp32 pull request 6359 — the core's one free retry after a restart onto the same access point.
