Script to Shutdown When Battery Level Low

Cubietruck related support

Script to Shutdown When Battery Level Low

Postby rlsten » 20 Dec 2015, 17:26

Hi,

I am using the Cubietruck inside a Ewell case to serve as a portable front-end to listen to music through an attached USB DAC (a Chord Mojo) connected to headphones. I am using a 5300 mAh power from Aliexpress that has the correct 2.5 mm JST connector. I also have a 1TB Samsung SSD inside the case, and am running the latest version of Rune Audio for Cubietruck. Everything is working very well, with excellent, clean sound.

What I am trying to do now is create a script that will shutdown the Cubietruck safely (stopping the MPD service and unmounting the SATA SDD before shutting down) if the battery level reaches a certain level, say 10 percent. (I have already modified the LEDs to indicate when the battery is fully charged or is charging.)

I have cobbled a script from various sources to be located in a file called "/usr/bin/battery_status.sh". The script would be executed as a service "/etc/systemd/system/battery_status.service", containing the following code:

Code: Select all
[Unit]
Description=battery_status
[Service]
ExecStart=/usr/bin/battery_status.sh

[Install]
WantedBy=multi-user.target



Here is the "battery_status.sh" script:


Code: Select all
#!/bin/bash
while true
do
        battery_level=`cat /sys/class/power_supply/battery/voltage_now`
        if [ $battery_level -le 400000 ]; then
        echo "Stop MPD and unmount shares..."
        mpc stop
        systemctl stop nginx
        sleep 1
        systemctl stop mpd
        systemctl stop spopd
        sleep 1
        umount -aft nfs
        umount -aft cifs
   sleep 30
   systemctl poweroff
        fi
        sleep 300
done



Essentially, when the battery voltage = 10% of full capacity, the commands from the rune_shutdown.service located in "/var/www/command/rune_shutdown" are executed, and the system then powers off.

My question is, will this script work? Am I missing anything, or doing anything that will screw up the system? Or is there a better way to do this? :?:

Since I am a relative newbie to coding, I don't want to run a script that will end up with some kind of infinite loop or otherwise gum up the works. :shock:

Thanks very much,
Rod
rlsten
 
Posts: 118
Joined: 25 May 2015, 18:06

Re: Script to Shutdown When Battery Level Low

Postby hondagx35 » 20 Dec 2015, 18:27

Hi rod,

My question is, will this script work? Am I missing anything, or doing anything that will screw up the system? Or is there a better way to do this? :?:

Your script looks good and i do not think that it will screw up something.

- Did you consider to use:
cat /sys/class/power_supply/battery/capacity
instead of
cat /sys/class/power_supply/battery/voltage_now

- Did you adapt script.fex to your needs (battery)?

- Your script has to be executable
Code: Select all
chmod +x /usr/bin/battery_status.sh


Frank
User avatar
hondagx35
 
Posts: 3042
Joined: 11 Sep 2014, 22:06
Location: Germany

Re: Script to Shutdown When Battery Level Low

Postby rlsten » 20 Dec 2015, 19:22

Hi frank,

Thanks for the quick reply!

Did you consider to use:
cat /sys/class/power_supply/battery/capacity
instead of
cat /sys/class/power_supply/battery/voltage_now


I didn't know about the Capacity function, so I will definitely use that.

- Did you adapt script.fex to your needs (battery)?


The only change I made to script.bin was to change "pmu_battery_cap" from 2100 to 5300 to reflect the capacity of the battery I installed. I didn't know of anything else to change.

I have made the script executable.

After I have changed the script to substitute "cat /sys/class/power_supply/battery/capacity", I will enable the service with

Code: Select all
systemctl enable battery_status.service


and see how it goes!

Thanks again,
Rod
rlsten
 
Posts: 118
Joined: 25 May 2015, 18:06

Re: Script to Shutdown When Battery Level Low

Postby hondagx35 » 21 Dec 2015, 22:54

Hi Rod,

any news?

Frank
User avatar
hondagx35
 
Posts: 3042
Joined: 11 Sep 2014, 22:06
Location: Germany

Re: Script to Shutdown When Battery Level Low

Postby keithdw » 22 Jul 2016, 13:55

You mention editing the script.fex file to give the battery parameters. I have just installed a LiPo battery on my cubietruck and was wondering if you could explain to a newbie how to edit this file, how to find this file.

I've also found that when I try to shutdown runeaudio it just reboots. Have you experienced this, and do you know how to get around it?

Thanks in advance for any assistance,
keithdw
 
Posts: 5
Joined: 22 Jul 2016, 13:38

support RuneAudio Donate with PayPal


Return to Cubietruck

Who is online

Users browsing this forum: No registered users and 0 guests