Skip to content

Conversation

@lexfrei
Copy link

@lexfrei lexfrei commented Jan 25, 2026

Summary

Implement automatic WiFi management based on power source. When enabled, WiFi is automatically disabled when running on battery and re-enabled when external power is connected.

  • Add wifi_on_external_power_only config option to enable feature
  • Add wifi_power_loss_timeout_secs config option for delay before disabling WiFi (default: 30s)
  • Implement proper WiFi hot toggle without device reboot

Use Case

Docked mode (at home, charging):

  • Node connected to USB power
  • WiFi enabled for web UI access from PC, MQTT, remote management

Portable mode (on the go):

  • Node running on battery
  • WiFi not needed — phone connects via BLE
  • WiFi wastes ~100-150mA for no benefit

Implementation

WiFi Service Cleanup (hot toggle support)

  • Add deinitWebServer() to stop HTTPS/HTTP servers
  • Add stop() method to UdpMulticastHandler
  • Add deinitWifiServices() to properly shutdown all WiFi-dependent services (mDNS, NTP, Syslog, WebServer, API Server)
  • Reset APStartupComplete flag to allow service reinitialization on reconnect

Power-Based WiFi Control

  • handleWifiPowerManagement() in Power.cpp monitors USB power state
  • Timer-based approach prevents WiFi toggle on brief power interruptions
  • WiFi automatically re-enables when external power is restored

Dependencies

Test Plan

  • Build for ESP32-S3 target
  • Verify WiFi connects normally with external power
  • Disconnect USB, wait 30s — verify WiFi disabled
  • Reconnect USB — verify WiFi re-enables
  • Test "accidental unplug" — disconnect and reconnect within 30s — WiFi should stay on
  • Monitor heap usage for memory leaks

Platforms

  • ESP32 (tested build: seeed-xiao-s3)
  • RP2040 (untested, should work)
  • Linux Native (untested, should work)

Closes: #9427

lexfrei and others added 6 commits January 25, 2026 05:23
Add wifi_on_external_power_only and wifi_power_loss_timeout_secs fields
to NetworkConfig structure. Updated init macros, field tags, and size.

Note: Full regeneration with nanopb required before merge.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Aleksei Sviridkin <f@lex.la>
Add deinitWebServer() to properly stop and clean up web servers
(HTTPS and HTTP) when WiFi is disabled at runtime.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Aleksei Sviridkin <f@lex.la>
Add stop() method to properly close UDP multicast listener
when WiFi is disabled at runtime.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Aleksei Sviridkin <f@lex.la>
Add deinitWifiServices() to properly stop all WiFi-dependent services
before disabling WiFi:
- Syslog client
- API server
- Web server
- mDNS
- NTP client

Reset APStartupComplete flag to allow services to reinitialize on reconnect.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Aleksei Sviridkin <f@lex.la>
Add handleWifiPowerManagement() to automatically manage WiFi based on
external power state:

- When external power is lost: start timer, disable WiFi after timeout
- When external power is restored: cancel timer, re-enable WiFi
- Timeout prevents accidental WiFi toggle on brief power interruptions

Configuration:
- wifi_on_external_power_only: enable feature (default: false)
- wifi_power_loss_timeout_secs: delay before WiFi disable (default: 30s)

Closes: meshtastic#9427

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Aleksei Sviridkin <f@lex.la>
Points to feat/wifi-power-management branch with new NetworkConfig fields.
Will be updated to upstream master after protobufs PR is merged.

See: meshtastic/protobufs#851

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Aleksei Sviridkin <f@lex.la>
@github-actions
Copy link
Contributor

@lexfrei, Welcome to Meshtastic!

Thanks for opening your first pull request. We really appreciate it.

We discuss work as a team in discord, please join us in the #firmware channel.
There's a big backlog of patches at the moment. If you have time,
please help us with some code review and testing of other PRs!

Welcome to the team 😄

lexfrei and others added 2 commits January 25, 2026 05:39
The close() method is only available in ESP32's AsyncUDP implementation.
Other platforms (nrf52, portduino/native) don't have this method, causing
build failures in native simulator tests.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Aleksei Sviridkin <f@lex.la>
Portduino has HAS_WIFI=1 but excludes mesh/wifi/ from build, causing
undefined reference errors for isWifiAvailable(), initWifi(), deinitWifi().

Added !defined(ARCH_PORTDUINO) guard to all WiFi power management code.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Aleksei Sviridkin <f@lex.la>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: Automatic WiFi disable when running on battery

1 participant