Showing posts with label Ubuntu Power Consumption. Show all posts
Showing posts with label Ubuntu Power Consumption. Show all posts

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!

[Linux Ubuntu] Getting Exact Value of Voltage and Current

We are now one step closer to measuring power process. In the previous explanation, we have got voltage and current value but it still has some obstacles. I mean, there are string "present voltage:" and "present rate" in voltage and current value, respectively. How can we remove them? We employ grep to get certain line and we can use awk get certain column of one line output.
cat /proc/acpi/battery/C1AC/state | grep "present voltage" |
awk '{print $3}'

cat /proc/acpi/battery/C1AC/state | grep "present rate" |
awk '{print $3}'
$3 in awk means it only prints the third column. The value of first column is string "present" and the second one is "voltage" or "rate" in current value. This command will print the exact value only without obstacles. Enjoy!

How Many Volt Consumed by Ubuntu Laptop?

Now we will observe how many watt consumed by Ubuntu laptop. Similar with how to check current (in ampere) consumed, we will only replace the grep command.

# Goto /proc/acpi/battery directory and go to directory name based on your battery type (i.e. C1AC)
cd /proc/acpi/battery/C1AC
# Cat the content of state file and grep only the voltage
cat state | grep "present voltage"
# We will get output like this:
present voltage:         16410 mV

The voltage consumed is shown in mV. Enjoy!

How Many Ampere Consumed by Ubuntu Laptop?

We can monitor how many ampere consumed by our Ubuntu laptop. Unfortunately, we still can't do them in Ubuntu desktop. ACPI interface will be used to employ this technique.

# Goto /proc/acpi/battery directory
cd /proc/acpi/battery
# There is only one directory and the name is different in accordance with laptop specifications. For example, my folder name is C1AC.
cd C1AC
# You will see three files: alarm, info, and state. Try to show the content of "state" file
cat state
# You will get a technical state about battery like shown here:
present:                 yes
capacity state: ok
charging state: charging
present rate: 1505 mA
remaining capacity: 840 mAh
present voltage: 16410 mV

# We only want to get ampere used, so just grep them
cat state | grep "present rate"
# The output is ampere present rate shown in mA
present rate:            1505 mA

We will use this technique to get power (in Watt) consumed by Ubuntu laptop. Enjoy!