• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!

[SHELL SCRIPT] Battery logging (discharge, capacity, etc)

Search This thread

xak944

Senior Member
Jun 9, 2010
379
211
North Carolina
After seeing apps like Android Battery Monitor, I wanted something I could run in a shell (adb) that would give me similar statistics, namely the battery discharge rate. I couldn't find any such tools or scripts, so I wrote one. Hopefully it will be useful to someone else, too.

The file paths are hard coded, and will likely only work on the Samsung Epic 4G Touch, however it could probably be easily adapted to other devices as well. It also probably requires busybox, since it uses some basic shell programs.

The script will output statistics every second, like this:
Code:
[DATE]|[TIME]|[DISCHARGE]|[CHARGE%]|[CHARGEMV]|[BATTTEMP]
2011/09/27|13:03:18|+0mA|92%|4101mV|31°C
2011/09/27|13:03:19|+0mA|92%|4101mV|31°C
2011/09/27|13:03:20|+0mA|92%|4101mV|31°C
2011/09/27|13:03:21|-83mA|92%|4018mV|32°C
2011/09/27|13:03:22|+0mA|92%|4018mV|32°C
2011/09/27|13:03:23|+0mA|92%|4018mV|32°C
2011/09/27|13:03:24|+0mA|92%|4018mV|32°C
2011/09/27|13:03:25|+0mA|92%|4018mV|32°C
2011/09/27|13:03:26|+0mA|92%|4018mV|32°C
2011/09/27|13:03:27|+0mA|92%|4018mV|32°C
2011/09/27|13:03:28|+0mA|92%|4018mV|32°C
2011/09/27|13:03:29|+0mA|92%|4018mV|32°C
2011/09/27|13:03:30|+45mA|92%|4063mV|32°C

Here's the script:
Code:
INTERVAL=1

CAPACITYVOLTAGE=0
while true; do
  PREVVOLTAGE=$CAPACITYVOLTAGE
  DATETIME=$(date +'%Y/%m/%d|%H:%M:%S')
  CAPACITYVOLTAGE="$(( $(cat /sys/devices/platform/sec-battery/power_supply/battery/voltage_now) / 1000 ))"
  CAPACITYPERCENT="$(cat /sys/devices/platform/sec-battery/power_supply/battery/capacity)"
  DISCHARGE="+$(( $CAPACITYVOLTAGE - $PREVVOLTAGE ))"
  TEMP="$(( $(cat /sys/devices/platform/sec-battery/power_supply/battery/batt_temp) / 10 ))"


  echo "${DATETIME}|$(echo ${DISCHARGE}|sed -e 's/^+-/-/')mA|${CAPACITYPERCENT}%|${CAPACITYVOLTAGE}mV|${TEMP}°C"

  sleep ${INTERVAL}
done
 
Last edited:

jerdog

Admin Emeritus - Purveyor of word nuggets
May 17, 2007
8,169
7,154
Gallifrey
While a nice piece of work - this is not development. It is an App. Moving to the appropriate section.
 

nabbed

Senior Member
Aug 15, 2010
2,368
342
After seeing apps like Android Battery Monitor, I wanted something I could run in a shell (adb) that would give me similar statistics, namely the battery discharge rate. I couldn't find any such tools or scripts, so I wrote one. Hopefully it will be useful to someone else, too.

The file paths are hard coded, and will likely only work on the Samsung Epic 4G Touch, however it could probably be easily adapted to other devices as well. It also probably requires busybox, since it uses some basic shell programs.

The script will output statistics every second, like this:
Code:
[DATE]|[TIME]|[DISCHARGE]|[CHARGE%]|[CHARGEMV]|[BATTTEMP]
2011/09/27|13:03:18|+0mA|92%|4101mV|31°C
2011/09/27|13:03:19|+0mA|92%|4101mV|31°C
2011/09/27|13:03:20|+0mA|92%|4101mV|31°C
2011/09/27|13:03:21|-83mA|92%|4018mV|32°C
2011/09/27|13:03:22|+0mA|92%|4018mV|32°C
2011/09/27|13:03:23|+0mA|92%|4018mV|32°C
2011/09/27|13:03:24|+0mA|92%|4018mV|32°C
2011/09/27|13:03:25|+0mA|92%|4018mV|32°C
2011/09/27|13:03:26|+0mA|92%|4018mV|32°C
2011/09/27|13:03:27|+0mA|92%|4018mV|32°C
2011/09/27|13:03:28|+0mA|92%|4018mV|32°C
2011/09/27|13:03:29|+0mA|92%|4018mV|32°C
2011/09/27|13:03:30|+45mA|92%|4063mV|32°C

Here's the script:
Code:
INTERVAL=1

CAPACITYVOLTAGE=0
while true; do
  PREVVOLTAGE=$CAPACITYVOLTAGE
  DATETIME=$(date +'%Y/%m/%d|%H:%M:%S')
  CAPACITYVOLTAGE="$(( $(cat /sys/devices/platform/sec-battery/power_supply/battery/voltage_now) / 1000 ))"
  CAPACITYPERCENT="$(cat /sys/devices/platform/sec-battery/power_supply/battery/capacity)"
  DISCHARGE="+$(( $CAPACITYVOLTAGE - $PREVVOLTAGE ))"
  TEMP="$(( $(cat /sys/devices/platform/sec-battery/power_supply/battery/batt_temp) / 10 ))"


  echo "${DATETIME}|$(echo ${DISCHARGE}|sed -e 's/^+-/-/')mA|${CAPACITYPERCENT}%|${CAPACITYVOLTAGE}mV|${TEMP}°C"

  sleep ${INTERVAL}
done

1) How does one install the script?
2) How does one uninstall it?
3) How much does the script affect battery performance?
4) How stable is the script?
5) What makes this script better than other similar apps in the market?
 

NeoA

Member
Feb 21, 2011
30
6
Bangalore
>> 1) How does one install the script?
You don't. Just move it to an appropriate location on your phone via adb push command, change permissions to make executable & run it. It does not involve any formal installation/removal.

>> 3) How much does the script affect battery performance?
Neglible. even if you launched from a terminal emulator on-board your phone & not while it was connected via the USB to your computer

>> 4) How stable is the script?
Fairly since its quite simple. He is only reading those ascii files available on any typical linux based system & printing to console after some post-processing
 
  • Like
Reactions: xak944

Futurian

Member
Jan 19, 2007
24
0
Been interested in looking at what I can get my rooted device to do via Terminal/SSH, this is a great start :D Had to edit it a bit for my desire Z, however working a treat.. The script can lag a little if I use it via SSH with the screen off, however to be expected I suppose. A little Caffeine and it completely works as expected.

Code:
INTERVAL=1

CAPACITYVOLTAGE=0
while true; do
  PREVVOLTAGE=$CAPACITYVOLTAGE
  DATETIME=$(date +'%Y/%m/%d|%H:%M:%S')
  CAPACITYVOLTAGE="$(cat /sys/devices/platform/rs30100001:00000000/power_supply/battery/batt_vol)"
  CAPACITYPERCENT="$(cat /sys/devices/platform/rs30100001:00000000/power_supply/battery/capacity)"
  DISCHARGE="+$(( $CAPACITYVOLTAGE - $PREVVOLTAGE ))"
  TEMP="$(( $(cat /sys/devices/platform/rs30100001:00000000/power_supply/battery/batt_temp) / 10 ))"

  echo "${DATETIME}|$(echo ${DISCHARGE}|sed -e 's/^+-/-/')mA|${CAPACITYPERCENT}%|${CAPACITYVOLTAGE}mV|${TEMP}°C"

  sleep ${INTERVAL}
done

I'm off to try and create something similar for the GPS.. No idea how feasible as yet, but appreciate the inspiration.
 

here.david

Senior Member
Jan 6, 2009
423
29
Sonoma, CA
I had to edit it a bit for my Vibrant (Miui 9.23)

make file (I called mine "adb_batt.sh" placed it on my sdcard
I used Gscrip lite to run it (or terminal)
"sh ./mnt/sdcard/adb_batt.sh"
==================================

INTERVAL=1

CAPACITYVOLTAGE=0
while true; do
PREVVOLTAGE=$CAPACITYVOLTAGE
DATETIME=$(date +'%Y/%m/%d|%H:%M:%S')
CAPACITYVOLTAGE="$(( $(cat sys/devices/platform/i2c-gpio.6/i2c-6/6-0066/max8998-charger/power_supply/battery/voltage_now) / 1000 ))"
CAPACITYPERCENT="$(cat sys/devices/platform/i2c-gpio.6/i2c-6/6-0066/max8998-charger/power_supply/battery/capacity
)"
DISCHARGE="+$(( $CAPACITYVOLTAGE - $PREVVOLTAGE ))"
TEMP="$(( $(cat sys/devices/platform/i2c-gpio.6/i2c-6/6-0066/max8998-charger/power_supply/battery/batt_temp_check) / 10 ))"


echo "${DATETIME}|$(echo ${DISCHARGE}|sed -e 's/^+-/-/')mA|${CAPACITYPERCENT}%|${CAPACITYVOLTAGE}mV|${TEMP}°C"

sleep ${INTERVAL}
done
 

FBis251

Senior Member
Mar 21, 2011
3,419
3,716
www.fernandobarillas.com
yes, but having a tweakable script version.

back on topic, trying to run the script but it complains
Code:
# sh logb
: not found
logb: 16: Syntax error: "done" unexpected (expecting "do")

Sounds like you might've copied and pasted the script on a windows text editor, which uses DOS line endings. You need to convert them to unix line endings. If you use notepad++ to edit/save the file:
Edit > EOL Conversion > Unix Format
 

frifox

Senior Member
Oct 12, 2007
502
56
Portland, OR
Sounds like you might've copied and pasted the script on a windows text editor, which uses DOS line endings. You need to convert them to unix line endings. If you use notepad++ to edit/save the file:
Edit > EOL Conversion > Unix Format
sh1t, i thought i made sure it pasted in unix format... oh well, redid it with notepad++ and that fixed it :)
 

Top Liked Posts

  • There are no posts matching your filters.
  • 10
    After seeing apps like Android Battery Monitor, I wanted something I could run in a shell (adb) that would give me similar statistics, namely the battery discharge rate. I couldn't find any such tools or scripts, so I wrote one. Hopefully it will be useful to someone else, too.

    The file paths are hard coded, and will likely only work on the Samsung Epic 4G Touch, however it could probably be easily adapted to other devices as well. It also probably requires busybox, since it uses some basic shell programs.

    The script will output statistics every second, like this:
    Code:
    [DATE]|[TIME]|[DISCHARGE]|[CHARGE%]|[CHARGEMV]|[BATTTEMP]
    2011/09/27|13:03:18|+0mA|92%|4101mV|31°C
    2011/09/27|13:03:19|+0mA|92%|4101mV|31°C
    2011/09/27|13:03:20|+0mA|92%|4101mV|31°C
    2011/09/27|13:03:21|-83mA|92%|4018mV|32°C
    2011/09/27|13:03:22|+0mA|92%|4018mV|32°C
    2011/09/27|13:03:23|+0mA|92%|4018mV|32°C
    2011/09/27|13:03:24|+0mA|92%|4018mV|32°C
    2011/09/27|13:03:25|+0mA|92%|4018mV|32°C
    2011/09/27|13:03:26|+0mA|92%|4018mV|32°C
    2011/09/27|13:03:27|+0mA|92%|4018mV|32°C
    2011/09/27|13:03:28|+0mA|92%|4018mV|32°C
    2011/09/27|13:03:29|+0mA|92%|4018mV|32°C
    2011/09/27|13:03:30|+45mA|92%|4063mV|32°C

    Here's the script:
    Code:
    INTERVAL=1
    
    CAPACITYVOLTAGE=0
    while true; do
      PREVVOLTAGE=$CAPACITYVOLTAGE
      DATETIME=$(date +'%Y/%m/%d|%H:%M:%S')
      CAPACITYVOLTAGE="$(( $(cat /sys/devices/platform/sec-battery/power_supply/battery/voltage_now) / 1000 ))"
      CAPACITYPERCENT="$(cat /sys/devices/platform/sec-battery/power_supply/battery/capacity)"
      DISCHARGE="+$(( $CAPACITYVOLTAGE - $PREVVOLTAGE ))"
      TEMP="$(( $(cat /sys/devices/platform/sec-battery/power_supply/battery/batt_temp) / 10 ))"
    
    
      echo "${DATETIME}|$(echo ${DISCHARGE}|sed -e 's/^+-/-/')mA|${CAPACITYPERCENT}%|${CAPACITYVOLTAGE}mV|${TEMP}°C"
    
      sleep ${INTERVAL}
    done
    1
    >> 1) How does one install the script?
    You don't. Just move it to an appropriate location on your phone via adb push command, change permissions to make executable & run it. It does not involve any formal installation/removal.

    >> 3) How much does the script affect battery performance?
    Neglible. even if you launched from a terminal emulator on-board your phone & not while it was connected via the USB to your computer

    >> 4) How stable is the script?
    Fairly since its quite simple. He is only reading those ascii files available on any typical linux based system & printing to console after some post-processing