This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
forked from linshuqin329/UPS-Lite
-
Notifications
You must be signed in to change notification settings - Fork 2
/
UPS_Lite.py
60 lines (44 loc) · 2.04 KB
/
UPS_Lite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
import struct
import smbus
import sys
import time
class UPS():
def __init__(self):
# Set the bus port either 1 or 0
self.bus = smbus.SMBus(1)
# set low capacity alert for the battery
self.low_capacity = 20
def read_voltage(self):
# This function returns the voltage as float from the UPS-Lite via SMBus object
address = 0x36
read = self.bus.read_word_data(address, 2)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
voltage = swapped * 1.25 /1000/16
return voltage
def read_capacity(self):
# This function returns the ramaining capacitiy in int as precentage of the battery connect to the UPS-Lite
address = 0x36
read = self.bus.read_word_data(address, 4)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
capacity = swapped/256
return int(capacity)
def is_battery_full(self,capacity):
# This function returns True if the battery is full, else return False
if(capacity == 100):
return True
return False
def is_battery_low(self,capacity):
# This function returns True if the battery capacity is low, else return False
if(capacity <= self.low_capacity):
return True
return False
def main():
ups_lite = UPS()
voltage = ups_lite.read_voltage()
capacity = ups_lite.read_capacity()
is_low = ups_lite.is_battery_low(capacity)
is_full = ups_lite.is_battery_full(capacity)
print("[-] Voltage: %s" % voltage)
print("[-] Capacitiy: %s" % capacity)
main()