Skip to content

Query the WiFi driver once per connection, not on every isConnected() - #1306

Merged
gskjold merged 1 commit into
mainfrom
fix/wifi-modem-sleep-isconnected
Nov 5, 2026
Merged

gskjold merged 1 commit into
mainfrom
fix/wifi-modem-sleep-isconnected

Conversation

@gskjold

@gskjold gskjold commented Nov 5, 2026

Copy link
Copy Markdown
Member

Why

v2.6.0-rc2 makes a Pow-U draw about 38% more power than v2.5.7: 145 mA instead of 105 mA at 5 V (724 mW instead of 527 mW). main behaves the same as rc2. On HAN powered devices this eats into the margin that #1272 was meant to improve.

The cause is in #1272 (a66a925). isConnected() calls WiFi.BSSID() and WiFi.channel() on every call to remember the AP for the fast reconnect. isConnected() runs on every loop(), about 40 times per second. Each of those driver queries wakes the radio from modem sleep for ~12 ms, so with WiFi power saving enabled the radio was awake 54% of the time instead of ~4%. I have not checked which of the two calls is responsible; the fix avoids both.

What changed (src/WiFiClientConnectionHandler.cpp)

isConnected() records the BSSID and channel only when none is remembered (lastChannel == 0). lastChannel is already reset by disconnect() and by the fast retry in connect(), so the AP is recorded again after every reconnect.

Measurements

Same Pow-U (ESP32-S2) and config, powered at 5 V by a Nordic PPK2 (source mode, 100 kS/s). WiFi power saving was at max modem sleep, no meter connected. Recording started ~150 s after boot.

Firmware Average Median (1 ms) p99 (1 ms) Radio awake
v2.5.7 105.3 mA 100.9 mA 177 mA 4%
v2.6.0-rc2 144.9 mA 152.5 mA 191 mA 54%
main (d25d6c1) 143.2 mA 145.6 mA 186 mA 53%
main + this PR 103.9 mA 100.1 mA 167 mA 3%

The v2.5.7 and rc2 runs are 10 min each. The main runs are 2 min, which is enough: 2-minute windows of the long runs vary by less than 1 mA.

On rc2 and main the 1 ms trace shows a ~12 ms step to ~175 mA on every loop pass, about every 24 ms. With this PR the trace matches v2.5.7.

Trade-offs

If the ESP32 core auto-reconnects to a different AP without loop() ever seeing the link down, the remembered BSSID stays stale until the next disconnect. Before this PR it was refreshed on every call.

Testing

  • pio run for esp32s2, esp32 and esp8266: all build (esp8266 flash at 98.4%)
  • Pow-U: average draw back to the v2.5.7 level (table above), connects normally after boot and OTA
  • A lost connection still logs Reconnecting to WiFi network: … (channel N, AP xx:xx:…) with the correct AP, and reconnects
  • ESP8266 board: power draw compared with v2.5.7 (not measured)

🤖 Generated with Claude Code

a66a925 made isConnected() call WiFi.BSSID() and WiFi.channel() on
every call, to remember the AP for a fast reconnect. isConnected() runs
on every loop(), and those driver queries woke the radio from modem
sleep for ~12 ms each time. With WiFi power saving enabled the radio
was awake about half the time instead of ~4%, raising the average draw
of a Pow-U from ~105 mA to ~145 mA at 5 V.

Record the AP only when none is remembered. lastChannel is already
reset by disconnect() and by the fast retry in connect(), so it is
recorded again after every 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: eaf8535

All environments built successfully. Download the zip files:

Artifacts expire after 7 days. View workflow run

@gskjold
gskjold merged commit 6836c02 into main Nov 5, 2026
8 checks passed
@gskjold
gskjold deleted the fix/wifi-modem-sleep-isconnected branch November 5, 2026 13:30
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