How to track battery life - instructions


cosurgi

http://janek.kozicki.pl/
Joined
Jul 30, 2008
Messages
1,098
Location
Gdansk, Poland
Website
janek.kozicki.pl
Hi, sorry if this is wrong section, but I think that most of battery-related problems will appear here. So this is why I decided to post this here.


I was worried that my battery has problems and so I had setup a simple script for tracking battery life. I have records from more than a week already, and finally (in the bus) I have time to post about how I'm doing this. So that everyone concerned can track battery life, and perhaps later we can compare our results.


That's really simple, first you have to install cron:



Code:
opkg install cron
will do that for you. Then you need to make a script battery.sh like this:





Code:
#!/bin/sh

cd /media/someSDcard/somepath_where_you_have_write_access

echo `date +%s` `cat /sys/class/power_supply/bq27500-0/capacity` `cat /sys/class/power_supply/bq27500-0/time_to_empty_avg` >> battery.dat

then you need to make it executable with:

Code:
chmod a+x battery.sh

Also make sure that you have write access in place where you record the battery data.



Finally you need to add a crontab entry. Make a textfile that contains:





Code:
# min hr dom moy dow command

*/2   *  *   *   *   /the/path/to/your/script/battery.sh

and with this command

Code:
crontab /path/to/textfile
make it to be your crontab. Remember that you need empty line at the end of crontab file or it will not work. I am invoking this script every two minutes (the */2 is for that), you might want to chose different interval. You can check for sure if your crontab is really configured by "l"isting it with command

Code:
crontab -l



OK, so now you have recording up and running, you can check if your battery.dat file is getting filled with numbers.



At the end I have made a simple gnuplot script for plotting this data, but there is still lots of room for improvement here:





Code:
set xdata time

set format x "%H:%M"

set ytic 1

set grid

set ylabel "hours"

set y2label "battery %"

#set xlabel "hours"

set yr [0:25]

set y2r [0:100]

set y2tics

set pointsize 1.5

set key left bottom

plot '/media/SanDisk32GB/battery.dat' u ($1)$3/3600.0) title "expected hours" w lp, '/media/SanDisk32GB/battery.dat' u ($1)$2/4.0) title "battery %/4 (max 25)" w lp

pause -1




(just change your paths to battery.dat file)



to plot this on pandora you will need to install gnuplot, fortunately [/CODE]
Code:
opkg install gnuplot

will work. Maybe also gnuplot-nox (I forgot). This plot will squash whole battery history, so after a week it stopped being readable, but that's easy to fix so perhaps later I will post about that too - but anyway - first we need to have some recorded data, then we can worry about plotting it. But for now, with this script you can plot by calling:

[CODE]gnuplot that_script_above.gnuplot


So if you are interested in battery life - go setup your cron and start recording it!


EDIT: now setup cron with call "crontab texfile" - should make things simpler for everyone.


EDIT: added invocation to gnuplot, for plotting.
 
Last edited by a moderator:
hi need halp gettign cron to run


can't seem to edit a file when i do sudo crontab -e; says that vi is missing but i can edit in vi using crontab -e but when i do this it saves this only in /tmp and disappears when i exit vi?


cheers
 
@sony - you are welcome :)

hi need halp gettign cron to run


can't seem to edit a file when i do sudo crontab -e; says that vi is missing but i can edit in vi using crontab -e but when i do this it saves this only in /tmp and disappears when i exit vi?


cheers
yes, when editing crontab, you are working on a temporary file, which is later automatically used as crontab. What happens when you do



Code:
crontab -l

It should print your current crontab. Do you see your lines there?
 
Last edited by a moderator:
@cosurgi...


Forgive me for my ignorance but is CRON already installed in the Pandora's nand???. In other words, all I have to do is run those commands in the terminal for it to run?


I would like to keep an accurate track of my battery life.
could you remove that big quote? It adds a lot of scrolling height :)

cron is not installed on pandora by default, you have to install it with



Code:
opkg install cron
then you jost follow those commands, and it should work.
 
Last edited by a moderator:
we have few issues about setting VI as environment editor, it' easier to simply create a new text file with mousepad and crontab it -



Code:
crontab /media/sdcard/textfile
- add sudo if needed,

good hint! Should fix any vi problems here :)

tried alerino suggestions and when running crontab -l it outputs the lines in file but no battery.dat file being created?
 
tried alerino suggestions and when running crontab -l it outputs the lines in file but no battery.dat file being created?
Check if your script battery.sh is correct - run it by hand and see if battery.dat appears. If it works then at most in 2 minutes cron will make it. You added an empty line at the end of cron file, right?
 
tried alerino suggestions and when running crontab -l it outputs the lines in file but no battery.dat file being created?
Check if your script battery.sh is correct - run it by hand and see if battery.dat appears. If it works then at most in 2 minutes cron will make it. You added an empty line at the end of cron file, right?

battery.sh does not run, gives this error:


./battery.sh: line 3: can't create battery.dat: Permission denied.


battery.sh is running in my home folder.
 
battery.sh does not run, gives this error:
./battery.sh: line 3: can't create battery.dat: Permission denied.


battery.sh is running in my home folder.
did you change

Code:
cd /media/someSDcard/somepath_where_you_have_write_access
to something meaningful? You can give write access by:



Code:
chmod a+rwX /media/someSDcard/somepath_where_you_have_write_access
 
battery.sh does not run, gives this error:
./battery.sh: line 3: can't create battery.dat: Permission denied.


battery.sh is running in my home folder.
did you change

Code:
cd /media/someSDcard/somepath_where_you_have_write_access
to something meaningful? You can give write access by:



Code:
chmod a+rwX /media/someSDcard/somepath_where_you_have_write_access

hi


made error in using /home instead of ~/


script works and cron seems to be working as well


thanks!
 
@hakmanplayer: did it work for you?
Yes exactly as you typed and sorry i did not thank you sooner.


CRON is perfect for this!!


If i can help let me know.


Easier than i thought actually. :) :blink:
 
Last edited:
hi again


when trying to run gnuplot myscript.sh it says expected to find X11 driver, no such file or directory?
 
i also have battery % and hrs. left until a charge on my task bar.
 
hi again


when trying to run gnuplot myscript.sh it says expected to find X11 driver, no such file or directory?

does this help?



Code:
opkg install gnuplot-nox


I'm not sure what gnuplot packages I had installed. If that doesn't work, I'll try to find this out. Basically it's the best to install them all, read what is written on the terminal when you try this command, and maybe some other packages are mentioned.


EDIT: if myscript.sh is a gnuplot script, then better call it myscript.gnuplot :) The .sh extension is used for shell scripts (like /bin/bash), just by convention. Of course nothing prevents you from using any extension you want, it's linux, you are free to do anything you want :)
 
Last edited by a moderator:
hi


opkg install gnuplot-X11 seems to work but the plot window does not stay on permanently after invoking gnuplot myscript?
 
hi


opkg install gnuplot-X11 seems to work but the plot window does not stay on permanently after invoking gnuplot myscript?

do you have the last line "pause -1" ? It means to wait until escape is pressed, then it disappears. But instead of pressing esc, it's better to click on this window, so that it has focus, and then press "e" or "a" to refresh - do this after several minutes, and you will see that it 'grows'. Also - the best is to maximize this plot window :) Also, if you right click somewhere, you can draw a rectangle to zoom in. Then press "p" to go to previous zoom.
 
ah dang yes did not have that pause line!


thanks


so what does the constant difference between the expected red line and the actual green line really represent?


cheers
 
Back
Top