Skip to content

Reduce WiFi connect current draw on bus powered devices - #1272

Merged
gskjold merged 2 commits into
mainfrom
wifi-connect-power
Oct 15, 2026
Merged

gskjold merged 2 commits into
mainfrom
wifi-connect-power

Conversation

@gskjold

@gskjold gskjold commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Why

A HAN-powered Pow-U in a technical room with weak WiFi was reboot looping. On boot it sat in the supply-voltage wait (long yellow LED) because the capacitor could not charge fully, then browned out while WiFi was connecting. Two things in the WiFi client handler made connecting draw more current than necessary:

  1. The configured TX power was only applied after the device had connected (ARDUINO_EVENT_WIFI_STA_CONNECTED). Scans and connection attempts always ran at the full ~20 dBm. On ESP8266 the setting was never applied at all, because the only setOutputPower call sat inside the ESP32-only event handler.
  2. Every STA_DISCONNECTED event turned WiFi off entirely (WiFi.mode(WIFI_OFF)). The next connect then restarted the radio and scanned all channels.

What changed (src/WiFiClientConnectionHandler.*)

  • New applyTxPower() helper, called right after WiFi.mode(WIFI_STA) and before connecting. It is still reapplied once connected on ESP32, and it now works on ESP8266 too.
  • The disconnect event only logs; it no longer tears WiFi down.
  • isConnected() remembers the current channel and AP (BSSID). On the next connect attempt after a lost connection, it retries exactly that AP on its channel with the radio still on: WIFI_FAST_SCAN with bssid_set on ESP32, WiFi.begin(ssid, psk, channel, bssid) on ESP8266. That AP is the one the last full all-channel scan (WIFI_CONNECT_AP_BY_SIGNAL) picked as the strongest. Pinning the BSSID matters on mesh networks: a fast scan on the channel alone ends at the first matching node, and mesh nodes often share a channel.
  • It only falls back to the full WiFi restart if that retry has not connected within CONNECTION_TIMEOUT (30 s). An explicit disconnect() clears the remembered channel.

Trade-offs

  • With a wrong password or an access point that changed channel, it now takes up to ~40 s (instead of ~5 s) to fall back to a full restart with a full scan.
  • After a retry, the device stays pinned to that AP until the next full restart. If the AP disappears, the retry fails and the 30 s fallback scans all channels and picks the strongest again. The firmware does no roaming of its own; it only sets btm_enabled. I have not verified whether a pinned BSSID blocks mesh steering via 802.11v on this SDK.
  • ESP8266 flash usage is now 97.9%.

Testing

  • pio run for esp32s2, esp32 and esp8266: all build
  • Pow-U on HAN power: a lost connection logs Reconnecting to WiFi network: … (channel N, AP xx:xx:…) instead of closing resources, and reconnects to the same AP
  • Mesh with nodes on the same channel: after a lost connection it rejoins the same node; with that node powered off, it falls back to a full scan after ~30 s
  • Pow-U with low TX power set: the voltage graph shows smaller dips during connect
  • Wrong password / AP off: falls back to a full restart after ~30 s and connects once the AP is back

🤖 Generated with Claude Code

Apply the configured TX power right after the radio starts, instead of
after association. Scans and association used to run at full power
(~20 dBm) regardless of the setting, and on ESP8266 the setting was
never applied at all since the only call sat in the ESP32-only event
handler.

When the connection is lost, no longer power the radio down and rescan
all channels. The disconnect event only logs now; the next connect
attempt retries on the last known channel (fast scan) with the radio
still on, and only falls back to the full WiFi restart if that retry
has not connected within CONNECTION_TIMEOUT.

Both reduce the current spikes that brown out HAN powered boards
(Pow-U etc.) with a weak signal.

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

ArnieO commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Interesting! Could this discovery be linked to the issues Stig Landrø saw?

A fast scan on the remembered channel ends at the first AP matching the
SSID. Mesh nodes often share a channel, so the retry could join a weaker
node than the one the full scan had picked. Remember the BSSID as well
and retry exactly that AP; if it is gone, the existing timeout falls
back to a full WiFi restart and all-channel scan, which picks the
strongest AP again.

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

Copy link
Copy Markdown

🔧 PR Build Artifacts

Version: 59eb740

All environments built successfully. Download the zip files:

Artifacts expire after 7 days. View workflow run

@gskjold
gskjold merged commit a66a925 into main Oct 15, 2026
8 checks passed
@gskjold
gskjold deleted the wifi-connect-power branch October 15, 2026 06:28
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.

2 participants