Hi all,
As you all know I have been at home a lot the past few months and I have been spending quite a few sleepless, mosquito infested nights due to power cuts. So I was thinking about how frequent power cuts happen and a way to monitor it. If we can have enough data we can predict based on past experience if at any given point of time how likely it is that we may have a power failure. There is no point of reading newspapers for the power cut schedule cause they don't follow it anyway! I guess its BESCOM version of Calvin & Hobbes :)
At first I was wondering how to sense if the power is on/off if I had to collect data first. We could build a small circuit to sense it and write a driver blah, blah, blah...
But since I was on my laptop I could monitor if it is charging or not. Seems simple and does not require any special hardware. In future maybe if I setup a dedicated system to monitor it then we shall see how it goes.
So based on that I thought of doing the following:
1. Use the acpi interface to check the status of the laptop adapter among other useful information.
2. Monitor it periodically and log it in to a file for which I run a script every 5 minutes.
3. Read the file, use some probability mumbo jumbo to predict if there is a chance of a power cut.
Of course this is not the ideal way to do it as it would not be realistic for someone to keep their laptops connected to the power outlet always. But just as a small project I just wanted to try it anyway just for fun.
So I'll post my progress so far if any of you guys are interested help me out cause I am having trouble with math and coding (Never understood the former, forgot the later).
Shell script for the monitoring:
#!/bin/bash
file="/tmp/power.csv"
touch $file
acpi -a | grep off > /dev/null
if [ $? -eq 0 ]
then
echo `date +"%a; %d/%m/%y; %R;"` "0">> $file
else
echo `date +"%a; %d/%m/%y; %R;"` "1">> $file
fi
###### EOF ######
Sample power.csv file:
Mon; 05/08/13; 16:50; 1
Mon; 05/08/13; 16:55; 1
Mon; 05/08/13; 17:00; 1
Mon; 05/08/13; 17:05; 0
Mon; 05/08/13; 17:10; 1
Mon; 05/08/13; 19:10; 0
Mon; 05/08/13; 19:15; 0
Mon; 05/08/13; 19:20; 1
Mon; 05/08/13; 19:25; 0
Mon; 05/08/13; 19:30; 0
Mon; 05/08/13; 19:35; 0
For running the script you will need to check if your laptop has already been install with the acpi package and check its output. On mine I get the following output. Not sure if everyone gets the same data and format.
So far I can monitor the data and even plot a simple graph in excel once I import the *.csv file. So much for the easy part. Now comes the hard part of reading the file and predicting the future... :D
Oh I almost forgot... Need to set up crontab with the following data to automatically run the script every 5 minutes.
Add the following line...
...and verify using the following command.
[Anush@Lenny]$ crontab -l
*/5 * * * * /home/Anush/Desktop/power.sh
Check if crond is running:
As you all know I have been at home a lot the past few months and I have been spending quite a few sleepless, mosquito infested nights due to power cuts. So I was thinking about how frequent power cuts happen and a way to monitor it. If we can have enough data we can predict based on past experience if at any given point of time how likely it is that we may have a power failure. There is no point of reading newspapers for the power cut schedule cause they don't follow it anyway! I guess its BESCOM version of Calvin & Hobbes :)
At first I was wondering how to sense if the power is on/off if I had to collect data first. We could build a small circuit to sense it and write a driver blah, blah, blah...
But since I was on my laptop I could monitor if it is charging or not. Seems simple and does not require any special hardware. In future maybe if I setup a dedicated system to monitor it then we shall see how it goes.
So based on that I thought of doing the following:
1. Use the acpi interface to check the status of the laptop adapter among other useful information.
2. Monitor it periodically and log it in to a file for which I run a script every 5 minutes.
3. Read the file, use some probability mumbo jumbo to predict if there is a chance of a power cut.
Of course this is not the ideal way to do it as it would not be realistic for someone to keep their laptops connected to the power outlet always. But just as a small project I just wanted to try it anyway just for fun.
So I'll post my progress so far if any of you guys are interested help me out cause I am having trouble with math and coding (Never understood the former, forgot the later).
Shell script for the monitoring:
#!/bin/bash
file="/tmp/power.csv"
touch $file
acpi -a | grep off > /dev/null
if [ $? -eq 0 ]
then
echo `date +"%a; %d/%m/%y; %R;"` "0">> $file
else
echo `date +"%a; %d/%m/%y; %R;"` "1">> $file
fi
###### EOF ######
Sample power.csv file:
Mon; 05/08/13; 16:50; 1
Mon; 05/08/13; 16:55; 1
Mon; 05/08/13; 17:00; 1
Mon; 05/08/13; 17:05; 0
Mon; 05/08/13; 17:10; 1
Mon; 05/08/13; 19:10; 0
Mon; 05/08/13; 19:15; 0
Mon; 05/08/13; 19:20; 1
Mon; 05/08/13; 19:25; 0
Mon; 05/08/13; 19:30; 0
Mon; 05/08/13; 19:35; 0
For running the script you will need to check if your laptop has already been install with the acpi package and check its output. On mine I get the following output. Not sure if everyone gets the same data and format.
[Anush@Lenny]$ acpi -V
Battery 0: Discharging, 76%, 03:27:29 remaining
Battery 0: design capacity 4752 mAh, last full capacity 4511 mAh = 94%
Adapter 0: off-line
Thermal 0: ok, 44.0 degrees C
Thermal 0: trip point 0 switches to mode critical at temperature 98.0 degrees C
Cooling 0: LCD 7 of 15
Cooling 1: Processor 0 of 10
Cooling 2: Processor 0 of 10
Cooling 3: Processor 0 of 10
Cooling 4: Processor 0 of 10
[Anush@Lenny]$
Oh I almost forgot... Need to set up crontab with the following data to automatically run the script every 5 minutes.
[Anush@Lenny]$ crontab -e
*/5 * * * * /home/Anush/Desktop/power.sh
*/5 * * * * /home/Anush/Desktop/power.sh
Check if crond is running:
[Anush@Lenny powmon]$ service crond status
Redirecting to /bin/systemctl status crond.service
crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled)
Active: active (running) since Mon, 05 Aug 2013 12:18:43 +0530; 7h ago
Main PID: 28143 (crond)
CGroup: name=systemd:/system/crond.service
└ 28143 /usr/sbin/crond -n
If crond is not running try the following
[Anush@Lenny]$ chkconfig crond
[Anush@Lenny]$ sudo chkconfig crond on
So as I said before still need to figure out how to read the file and calculate the probability stuff.
Thinking if I should use a C program or try a bit more of scripting. There maybe better ways of doing what I have done so far so let me know what you guys think, all your suggestions are welcome...!!!
Also I would like to know if we need to setup an online repository on github or google code if we need to share code and ideas in the future. Cause I am not sure how easy it is for you guys to read or post code here but if you guys are ok with it we shall continue as it is. Not that I expect a lot of traffic on this site. I was surprised that SSH even remembered to post here... ;)
It was good to see his post after such a long idle time.
Damn it just as I am about to finish this post there is no power. I am not kidding. I intend to get to the bottom of this. If and when this is code gets completed I hope you guys can help me out by sharing data from your areas and figure out if its just my bad luck that they cut the power here so frequently or its the same all over Bangalore.
Dear AK,
ReplyDeleteSo our Rajanikath.Jr has decided to take on the BESCOM as his latest nemesis. I am up for this project. I will test the above setup with my laptop by this weekend and will let you know the results. And as far as the Mathematics stuff you were talking, its beyond my capability. Probably we need to join some crash courses starting from very basic postulates, theorems, algebra and arithmetic.
Regarding the code sharing and other discussions, its better we follow git hub and some mailing lists which are helpful in tracking our inventions if at all we do something.