Graceful shutdown when battery is low

Started by ilario, July 31, 2024, 11:40:22 AM

Previous topic - Next topic

ilario

Dear all,
I am using a A64-OLinuXino-2Ge8G-IND rev.E connected to a BATTERY-LIPO6600mAh.

Reading this post, I found the file that indicates the battery voltage:

cat /sys/class/power_supply/axp20x-battery/voltage_now
4198000

The question is:
is there something ready-to-use for gracefully shutdown the computer when the voltage gets below a threshold?
And: which number would be a reasonable voltage threshold?

LubOlimex

10% battery remaining is good time to save, sync, and shut down.

It can be done in many ways, maybe try with a shell script. There are some examples online. You can automate with cronjob.

By the way make sure to also set the current charge for the size of your battery, check what is the recommended charge current and set the value in:

/sys/class/power_supply/axp20x-battery/constant_charge_current_max
Technical support and documentation manager at Olimex

ilario

Quote from: LubOlimex on July 31, 2024, 12:00:54 PM10% battery remaining is good time to save, sync, and shut down.

It can be done in many ways, maybe try with a shell script. There are some examples online. You can automate with cronjob.

Thanks!
I found some useful info on Arch Linux's wiki here:
https://wiki.archlinux.org/title/Laptop#Hibernate_on_low_battery_level
they suggest to do it with UDEV but seems that on OLinuXino the battery does not communicate via UDEV (I waited until full discharge, and there was no message visible in "udevadm monitor --property", except when I plugged or unplugged the power supply).
Also, they mention a "acpi" tool that seems not packaged for Debian.
So I wrote this:

#!/bin/sh
for battery in /sys/class/power_supply/*bat*
do
        status=`cat $battery/status`
        capacity=`cat $battery/capacity`

        if [ "$status" = Discharging -a "$capacity" -lt 10 ]; then
                logger "Critical battery threshold"
                systemctl poweroff
        fi
done

and added this to crontab for running every 10 minutes.

Quote from: LubOlimex on July 31, 2024, 12:00:54 PMBy the way make sure to also set the current charge for the size of your battery, check what is the recommended charge current and set the value in:

/sys/class/power_supply/axp20x-battery/constant_charge_current_max

The default value in that file is 1.2 A, the suggested current for the pack with the 3 batteries in series seems to be 3*1.1 A (1.1 A can be seen here) but I will just leave the default value, as I saw that this file
cat /sys/class/power_supply/axp813-ac/input_current_limit
1500000
indicates that the power supply will not provide more than 1.5 A anyway.

Thanks!