LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Linux Battery Monitor

I want to make a labview project in linux, in which I will display the battery percentage in a progress bar, display the remaining time in hours, say if it is connected to the ac adapter, the battery temperature, etc., and I want to do this for linux systems. I will be making a distributable package afterwards. Please give me some sample code, so I can start my project.

Thanks.

0 Kudos
Message 1 of 10
(3,921 Views)

The most simple way is probably accessing the sysfs power module that is usually located at /sys/power/ and also /sys/class/power_supply/ in the filesystem and can be read through File IO functions as well as written to.

 

However this is Linux so everything that is considered standard practice will usually change depending on distribution, kernel version, and user installation preferences. This is just a warning if you intend to write something generic that should work on more than systems you control yourself. 

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 10
(3,893 Views)

Hello Nando88,

 

What distribution of Linux you are interested in using? If you are interested on use Ubunto you could try the upower command. This command gives you all the battery data. You can use the System Exec.vi to run the command and parse the Output String.

 

If you don't know the /org/... path you can use the command upower -e (--enumerate)

 

upower -i /org/freedesktop/UPower/devices/battery_BAT0 Example Output:

 

 native-path:          /sys/devices/LNXSYSTM:00/device:00/PNP0C0A:00/power_supply/BAT0
  vendor:               NOTEBOOK
  model:                BAT
  serial:               0001
  power supply:         yes
  updated:              Thu Feb  9 18:42:15 2012 (1 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               charging
    energy:              22.3998 Wh
    energy-empty:        0 Wh
    energy-full:         52.6473 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         31.6905 W
    voltage:             12.191 V
    time to full:        57.3 minutes
    percentage:          42.5469%
    capacity:            84.6964%
    technology:          lithium-ion
  History (charge):
    1328809335  42.547  charging
    1328809305  42.020  charging
    1328809275  41.472  charging
    1328809245  41.008  charging
  History (rate):
    1328809335  31.691  charging
    1328809305  32.323  charging
    1328809275  33.133  charging

 

You can also try the cat command

 

cat /proc/acpi/battery/BAT0/* Output:

 

design voltage: 11100 mV
design capacity warning: 341 mWh
design capacity low: 0 mWh
capacity granularity 1: 100 mWh
capacity granularity 2: 0 mWh
model number: Dell
serial number: 866
battery type: LION
OEM info: PANASONIC
present: yes
capacity state: ok
charging state: charged
present rate: 1 mW
remaining capacity: 3417
mWh present voltage: 12408 mV

 

design voltage: 11100 mV design capacity warning: 341 mWh design capacity low: 0 mWh capacity granularity 1: 100 mWh capacity granularity 2: 0 mWh model number: Dell serial number: 866 battery type: LION OEM info: PANASONIC present: yes capacity state: ok charging state: charged present rate: 1 mW remaining capacity: 3417 mWh present voltage: 12408 mV - See more at: https://docs.oseems.com/general/operatingsystem/linux/view-battery-information#sthash.JTYOUJPN.dpuf
design voltage: 11100 mV design capacity warning: 341 mWh design capacity low: 0 mWh capacity granularity 1: 100 mWh capacity granularity 2: 0 mWh model number: Dell serial number: 866 battery type: LION OEM info: PANASONIC present: yes capacity state: ok charging state: charged present rate: 1 mW remaining capacity: 3417 mWh present voltage: 12408 mV - See more at: https://docs.oseems.com/general/operatingsystem/linux/view-battery-information#sthash.JTYOUJPN.dpuf

 

 

design voltage: 11100 mV design capacity warning: 341 mWh design capacity low: 0 mWh capacity granularity 1: 100 mWh capacity granularity 2: 0 mWh model number: Dell serial number: 866 battery type: LION OEM info: PANASONIC present: yes capacity state: ok charging state: charged present rate: 1 mW remaining capacity: 3417 mWh present voltage: 12408 mV - See more at: https://docs.oseems.com/general/operatingsystem/linux/view-battery-information#sthash.JTYOUJPN.dpuf
design voltage: 11100 mV design capacity warning: 341 mWh design capacity low: 0 mWh capacity granularity 1: 100 mWh capacity granularity 2: 0 mWh model number: Dell serial number: 866 battery type: LION OEM info: PANASONIC present: yes capacity state: ok charging state: charged present rate: 1 mW remaining capacity: 3417 mWh present voltage: 12408 mV - See more at: https://docs.oseems.com/general/operatingsystem/linux/view-battery-information#sthash.JTYOUJPN.dpuf

Another way could be to use the acpi command, if acpi it's not installed you can install it using the command sudo apt-get install acpitool or sudo yum install.

 

acpi -V Output:

 

Battery 0: Full, 100%
Battery 0: design capacity 8400 mAh, last full capacity 8044 mAh = 95%
Adapter 0: on-line
Thermal 0: ok, 53.5 degrees C
Thermal 0: trip point 0 switches to mode critical at temperature 107.0 degrees C
Cooling 0: LCD 0 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
Cooling 5: Processor 0 of 10
Cooling 6: Processor 0 of 10
Cooling 7: Processor 0 of 10

acpi -t Output:

Thermal 0: ok, 39.5 degrees C

 

The following it's a Community Parse String Example that may help you:

 

Community: Parse String:

https://decibel.ni.com/content/docs/DOC-41702

 

Let me know if this helped you, best regards.

David P.
National Instruments
Applications Engineer
www.ni.com/soporte
0 Kudos
Message 3 of 10
(3,847 Views)

Please not that acpi cli and the /proc/acpi filesystem tree is considered obsolete since several years.

Rolf Kalbermatter
My Blog
0 Kudos
Message 4 of 10
(3,821 Views)

I used the command upwoer -e in the system exec vi, and I didn't get the information that you told me I would get.

Can someone please tell me how to fix this?

Thanks.

Screenshot from 2015-03-26 10:48:05.png

0 Kudos
Message 5 of 10
(3,794 Views)

I forgot to mention that I'm using Lubuntu, and ubuntu derivative.

0 Kudos
Message 6 of 10
(3,792 Views)

Well upower -e will give you a list of devices it knows about. In your case there only seems to be one device. Parse that output into lines and then you can use each line as parameter to upower -i <your device name>. there are many man pages about upower so it's a good idea to google for that and learn from there. This is not a LabVIEW problem but simply Linux knowledge.

Rolf Kalbermatter
My Blog
0 Kudos
Message 7 of 10
(3,777 Views)

As said,

 

The upower -e command outputs a list of devices, you must use that output as a parameter of the upower command to get the battery information.

David P.
National Instruments
Applications Engineer
www.ni.com/soporte
0 Kudos
Message 8 of 10
(3,765 Views)

I wish to know how to display the information that is received by the system exec vi. I tried all the commands, but none of them give me any information, just the first one, but its not the battery info, its like a path. Please tell me how to fix this.

Thanks

0 Kudos
Message 9 of 10
(3,746 Views)

Please read those ideas we gave you. And let us see what commands you did, including the parameters and what results you see.

Rolf Kalbermatter
My Blog
0 Kudos
Message 10 of 10
(3,726 Views)