Skip to content

feat: better power management#78

Draft
drupol wants to merge 2 commits into
attermann:masterfrom
drupol:push-slrzslmtutqw
Draft

feat: better power management#78
drupol wants to merge 2 commits into
attermann:masterfrom
drupol:push-slrzslmtutqw

Conversation

@drupol

@drupol drupol commented Jul 9, 2026

Copy link
Copy Markdown

This PR introduces power management and sleep states to extend battery runtime. By tracking device activity and gracefully yielding CPU resources when idle, nodes can drastically reduce their power footprint while maintaining mesh routing responsiveness.

The architecture and logic for these improvements draw heavy inspiration from the power management implementations found in the Meshtastic firmware, props to their developers.

  1. What has been done:
  • Introduced a global volatile unsigned long last_activity_time in Config.h.
  • Added hooks to refresh the activity timer on critical network and user events:
    • LoRa reception: Inside receive_callback()
    • Serial/Host interaction: Inside buffer_serial()
    • Physical input: Inside button_event()
  1. "Light Sleep" via RTOS Idle Yielding
  • Added logic at the end of the main loop() in RNode_Firmware.ino.
  • If power_management_enabled (it is true by defualt) is true and the node has been completely idle beyond idle_timeout_ms (defaults to 15 seconds), the loop executes a short delay(50).
  • On FreeRTOS-backed platforms like the ESP32 and nRF52 (e.g., RAK4631), this simple delay() yields execution back to the RTOS idle task. The idle task naturally executes WFI/WFE (Wait For Interrupt / Wait For Event) instructions, gracefully putting the CPU into a low-power "System ON" state while the LoRa radio remains in RX mode. The system wakes up instantly the moment an interrupt fires.
  1. Critical Battery Protection (Deep Sleep)
  • Updated Power.h to monitor battery_voltage.
  • If a battery is installed, the device is not on external power, and the voltage drops below the critical BAT_V_MIN threshold (typically 3.15V), the node intercepts the loop to automatically call sleep_now().
  • This triggers a true hardware Deep Sleep (e.g., SYSTEMOFF on nRF52), shutting down the transceiver and protecting LiPo batteries from irreversible deep-discharge damage.
  1. Configuration Options
  • Added power_management_enabled = true and idle_timeout_ms = 15000 to Config.h to allow users to opt-out or adjust the idle tolerances based on their use cases.
  1. Testing

Ongoing...

Let me know what you think about this.

@drupol drupol marked this pull request as ready for review July 9, 2026 21:23
@drupol drupol marked this pull request as draft July 9, 2026 21:23
@drupol

drupol commented Jul 9, 2026

Copy link
Copy Markdown
Author

I'll continue the testing tomorrow, time to go to bed. Feel free to take over if you want.

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