Get Battery Status from Console with PHPCLI

Hahahaha, aku jadi inget ucapan… duh, ucapan siapa ya, lupa aku.

Problem / Limitasi akan membuat seseorang menjadi kreatif.

Contohnya ini (narsis boleh dong, blog sendiri koq ;)) ), karena main game mode fullscreen, gak bisa liat status baterai di Tray menu. Tapi masih bisa liat console (again, dengan sihir dari yakuake). tapi kalo lagi main males dong di ganggu dengan liat barus status yang panjang, mending to the point aja. Maka, jadilah script berikut ini. dia akan membaca /proc/acpi/battery/BAT1/info dan /proc/acpi/battery/BAT1/state untuk menghasilkan nilai berapa persen sisa baterai laptop sekarang.

Silahkan diambil dan dimodif sesuai kebutuhan.

#!/usr/bin/php
<?php
//read_bat_status.php
    $debug = false;
    $bat_infox = file('/proc/acpi/battery/BAT1/info');
    if($debug) print_r($bat_infox);
    foreach($bat_infox as $idx => $bat_info){
        $bat_infox[$idx] = explode(':', $bat_info);
    }
    if($debug) print_r($bat_infox);
    $lfCapacity = 0;
    foreach($bat_infox as $idx => $bat_info){
        if($bat_infox[$idx][0] == 'last full capacity'){
            $lfCapacity = trim($bat_infox[$idx][1]);
        }
    }
    if($debug) echo '$lfCapacity : '.$lfCapacity."\n";
    $bat_statex = file('/proc/acpi/battery/BAT1/state');
    if($debug) print_r($bat_statex);
    foreach($bat_statex as $idx => $bat_state){
        $bat_statex[$idx] = explode(':', $bat_state);
    }
    if($debug) print_r($bat_statex);
    $rmCapacity = 0;
    foreach($bat_statex as $idx => $bat_state){
        if($bat_statex[$idx][0] == 'remaining capacity'){
            $rmCapacity = trim($bat_statex[$idx][1]);
        }
    }
    if($debug) echo '$rmCapacity : '.$rmCapacity."\n";
    $currPercent = ($rmCapacity * 100) / $lfCapacity;
    echo 'Battery Power Remaining : '.$currPercent." %\n";
?>

Cara penggunaannya, tinggal php read_bat_status.php. Tapi sebelumnya install dulu ya paket php-cli dengan sudo apt-get install php5-cli.

Share

Check Battery State in Ubuntu

If you use Laptop and Ubuntu, you can check whether your current battery status is in good condition of need a replacement. Just type this command in your shell:

cat /proc/acpi/battery/BAT1/info

Depending on your hadware config, you’ll see result like this :

present:                 yes
design capacity:         6600 mAh
last full capacity:      6600 mAh
battery technology:      rechargeable
design voltage:          14800 mV
design capacity warning: 660 mAh
design capacity low:     198 mAh
capacity granularity 1:  264 mAh
capacity granularity 2:  3780 mAh
model number:            Primary
serial number:
battery type:            Lion
OEM info:                Hewlett-Packard

If last full capacity is lower than design capacity than your battery is slowly going to it’s end.

Share