Skip to content

Rest the radio between failed WiFi connection attempts on ESP32 - #1308

Merged
gskjold merged 1 commit into
mainfrom
fix/wifi-pace-failed-connects
Nov 5, 2026
Merged

gskjold merged 1 commit into
mainfrom
fix/wifi-pace-failed-connects

Conversation

@gskjold

@gskjold gskjold commented Nov 5, 2026

Copy link
Copy Markdown
Member

Why

Follow-up to #1272, for bus powered devices (Pow-U etc.) with a weak signal or an unavailable AP.

setAutoReconnect(true) lets the ESP32 core retry immediately after every failed connection attempt. #1272 also stopped the disconnect event from turning WiFi off. Together, while the AP can't be reached, the radio retries back to back and never rests. That draws the most current exactly when the device's supply is weakest.

What changed (src/WiFiClientConnectionHandler.*)

  • Auto-reconnect off on ESP32. Retries are paced only by connect() and eventHandler(). ESP8266 keeps setAutoReconnect(true).
  • New linkUp flag. It is set on STA_CONNECTED and cleared in disconnect(). The disconnect event uses it to tell the two cases apart:
    • An established connection was lost: only clears the flag. connect() then does the fast same-AP retry from Reduce WiFi connect current draw on bus powered devices #1272 with the radio still on.
    • A connection attempt failed: disconnect(RECONNECT_TIMEOUT) turns WiFi off for 5 s. The next connect() restarts the radio and scans all channels.

Effect on the #1272 fallback

The fast same-AP retry now gets a single attempt. If it fails, the device falls back to a full scan after ~5 s instead of waiting out CONNECTION_TIMEOUT (30 s). With a wrong password or an AP that changed channel, the fallback comes sooner than in #1272.

disconnect() is called from the WiFi event handler again, as it was before #1272.

Measurements

Pow-U (ESP32-S2) at 5 V from a Nordic PPK2, staying connected to the AP. Recorded for 2 min starting ~150 s after boot, built on top of #1306:

Build Average Median (1 ms) p99 (1 ms)
#1306 103.9 mA 100.1 mA 167 mA
#1306 + this PR 103.7 mA 100.2 mA 166 mA

While connected, power is unchanged, as expected. The failed-connect path this PR targets has not been measured yet.

Testing

  • pio run for esp32s2, esp32 and esp8266: all build (esp8266 flash at 98.4%)
  • Pow-U connects normally after boot and OTA, with no change in steady-state power
  • AP unavailable (SSID off or MAC blocked) for a few minutes: lower average current than without this PR, and reconnects once the AP is back
  • Established connection lost: logs Reconnecting to WiFi network: … (channel N, AP …) and rejoins the same AP
  • Wrong password: retries every ~5 s with the radio off between attempts

🤖 Generated with Claude Code

With auto-reconnect enabled, the ESP32 core retries back to back after
every failed connection attempt, keeping the radio busy. A bus powered
device with a weak signal or an unavailable AP can not sustain that.

Disable the core's auto-reconnect on ESP32 and pace retries ourselves.
The disconnect event now tells a lost established connection apart from
a failed attempt: a lost connection still leaves the radio on for the
fast same-AP retry in connect(), while a failed attempt turns WiFi off
for RECONNECT_TIMEOUT before connect() tries again with a full scan.
ESP8266 keeps auto-reconnect.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

🔧 PR Build Artifacts

Version: a2b0377

All environments built successfully. Download the zip files:

Artifacts expire after 7 days. View workflow run

@gskjold
gskjold merged commit cc5aec3 into main Nov 5, 2026
14 of 16 checks passed
@gskjold
gskjold deleted the fix/wifi-pace-failed-connects branch November 5, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant