Python code to Laptop Battery Life Saver - Battery Status Check

Coded a light-weight Python program (600 KB), 'Battery Life Saver' to extend the life span of laptop batteries with GUI (Graphical User Interfae)

 

Using a Laptop Battery continuously by overcharging above 90% or by below 15%, reduces its life span.

 

This light-weight .exe program, Battery Life Saver will alarm, on excess charging or discharging.

 

 

Check this page for downloading this program

Download Battery Life Saver Program

 

 

Battery percentage can be checked by ‘psutil’ and it can retrieve battery information.

psutil (process and system utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python.

 

Installation of psutil

Windows in Command Prompt as Administrator:

 

pip install psutil

 

For Linux distributions:

sudo apt-get install gcc python3-dev

sudo pip3 install psutil

 

Code

 

import psutil

 

# Getting duration in the format, hh:mm:ss

def convertTime(seconds):

    minutes, seconds = divmod(seconds, 60)

    hours, minutes = divmod(minutes, 60)

    return "%d:%02d:%02d" % (hours, minutes, seconds)

 

battery = psutil.sensors_battery()

 

print("Battery Percent: ", battery.percent)

print("Connected to charger : ", battery.power_plugged)

 

print("Battery remaining : ", convertTime(battery.secsleft))

 

 

Check this page for downloading this program

Download Battery Life Saver Program

 

 

Code for getting alarm

 

import psutil

battery = psutil.sensors_battery()

battery_percent =  float(input("Enter maximum battery percentage, between 15 to 90 (numbers only):   "))

x=-1

while x<1:

    if battery.power_plugged == False:

        if (battery.percent<15):

            import winsound   # for windows OS only

            winsound.Beep(2500, 1000)

    # change these numbers for frequency (2500 Hz) and duration (1000 ms)

            print("Low battery. Connect to charger.")

            print (" ")

            print (" ")

 

    if battery.power_plugged == True:

        if (battery.percent>90):

            import winsound  # for windows OS only

            winsound.Beep(2500, 1000)

            print("Battery is overcharged. Remove charger to enhance battery span.")

            print (" ")

            print (" ")

 

        if (battery.percent>battery_percent):

            import winsound   # for windows OS only

            winsound.Beep(2500, 1000)

            print("Battery is charged above", battery_percent, "%. Remove charger to enhance battery span.")

            print (" ")

            print (" ")

 

 

 

Author

Dr. M Kanagasabapathy

Asst. Professor, Department of Chemistry

Rajus’ College, Madurai Kamaraj University

Rajapalayam (Tamil Nadu) India 626117