Sunday, April 10, 2011

How Many Watt Consumed in Ubuntu Laptop?

Yay... we will estimate how many watt consumed in Ubuntu laptop using data from ACPI interface. We have measured voltage and current, getting exact value of them, trying to make a simple shell script, and installing calculator package in console.
By putting all of these stuffs altogether, we will estimate how many power used by our lovely Ubuntu box.

# Make a shell script to cat voltage and ampere usage
#!/bin/bash
cat /proc/acpi/battery/C1AC/state | grep "present voltage" |
awk '{print $3}'
cat /proc/acpi/battery/C1AC/state | grep "present rate" |
awk '{print $3}'
# Make two variables to store voltage and ampere value
volt=`cat /proc/acpi/battery/C1AC/state | grep "present voltage" |
awk '{print $3}'`
ampere=`cat /proc/acpi/battery/C1AC/state | grep "present rate" |
awk '{print $3}'`
# Divide variable volt and ampere by 1000 because it still in mV or mA unit
volt=`echo "scale=3;$volt/1000"|bc`
ampere=`echo "scale=3;$ampere/1000"|bc`
Parameter scale is used to set precision. If we set them to 3, then we will get three decimal places precision.

# Measure the power by simply multiply voltage and current consumed
power=`echo "scale=3;$v*$a"|bc`
# And the full shell script is illustrated below:
#!/bin/bash
volt=`cat /proc/acpi/battery/C1AC/state | grep "present voltage" |
awk '{print $3}'`
ampere=`cat /proc/acpi/battery/C1AC/state | grep "present rate" |
awk '{print $3}'`
volt=`echo "scale=3;$volt/1000"|bc`
ampere=`echo "scale=3;$ampere/1000"|bc`
echo "Present voltage: " $volt "Volt"
echo "Present current: " $ampere "Ampere"
power=`echo "scale=3;$volt*$ampere"|bc`
echo "Present power :" $power "Watt"
# And it will give us a message like this:
Present voltage:  16.285 Volt
Present current: 1.507 Ampere
Present power : 24.541 Watt

This shell script has been tested in Ubuntu Lucid Lynx 10.04 and it works well. Enjoy!

No comments: