Skip to content

Instantly share code, notes, and snippets.

@theskanthunt42
Last active September 7, 2022 16:51
Show Gist options
  • Save theskanthunt42/c6f4d02259464c5de885e3ae03705b94 to your computer and use it in GitHub Desktop.
Save theskanthunt42/c6f4d02259464c5de885e3ae03705b94 to your computer and use it in GitHub Desktop.
Raspberry Pi w/UPS module(https://github.com/linshuqin329/UPS-18650) general infomations monitor. $h!t code warning
#This piece of code will only run if:
#You have a Raspberry Pi 3 or later with a UPS board using 18650 batteries(https://github.com/linshuqin329/UPS-18650/)
#side notes: in which is my dev env
import os
import struct
import smbus
import sys
import time
off_plan = False
plug_in_detect = False
def OSInfo():
carrier = dict()
carrier['up_since'] = os.popen("uptime -s").read().split("\n")[0]
carrier['uptime'] = os.popen("uptime -p").read().split("up ")[1].split("\n")[0]
carrier['hostname_v4'] = os.popen("hostname -I").read().split(" ")[0]
return carrier
def Detect_Plugin():
print("To be complete")
return False
def VoltageReadings(bus):
address = 0x36
reading = bus.read_word_data(address, 2)
voltage = struct.unpack("<H", struct.pack(">H", reading))[0]
dummy = voltage*1.25/1000/16
return dummy
def CapacityReading(bus):
address = 0x36
reading = bus.read_word_data(address, 4)
capacity = struct.unpack("<H", struct.pack(">H", reading))[0]
dummy = capacity/256
return dummy
def LogSaver(logs):
with open("/home/the42/ups_log.txt", "w") as f:
f.write(f"\n{logs}")
def VideoCoreReadings():
carrier = dict()
carrier['vc_temp'] = os.popen("vcgencmd measure_temp").read().split("=")[1].split("\n")[0]
carrier['arm_clock'] = int(os.popen("vcgencmd measure_clock arm").read().split("=")[1].split("\n")[0]) / 1000000
carrier['core_clock'] = int(os.popen("vcgencmd measure_clock core").read().split("=")[1].split("\n")[0]) / 1000000
carrier['h264_clock'] = int(os.popen("vcgencmd measure_clock h264").read().split("=")[1].split("\n")[0]) / 1000000
carrier['core_volts'] = os.popen("vcgencmd measure_volts core").read().split("=")[1].split("\n")[0]
carrier['gpu_ram'] = os.popen("vcgencmd get_mem gpu").read().split("=")[1].split("\n")[0]
carrier['ring_osc_info'] = os.popen("vcgencmd read_ring_osc").read().split("=")[1]
return carrier
def LineUpdates(volt, capt, c_time, vc_temp, arm_clk, core_clk, h264_clk, core_v, vram, osc, plug, off_plan, up_since, total_uptime, ipv4_addr):
summary = f"""
---------------------General Infomations-----------------------
Current Time: {c_time}
Power on Since: {up_since}
Power on For: {total_uptime}
IPv4 Address(if had): {ipv4_addr}
RAM Allocated by GPU: {vram}
---------------------Battery Infomations-----------------------
Current Battery Voltage Reading: {volt}
Current Capacity Reading(Provided by MAX17040G): {capt}
Plugged in? : {plug}
Planned to shutdown?: {off_plan}
-----------------------SoC Infomations-------------------------
Current SoC Temperature: {vc_temp}
CPU Clock Speed: {arm_clk} MHz
VideoCore Clock Speed: {core_clk} MHz
H.264 Decoder Clock Speed(?): {h264_clk} MHz
SoC Package Voltage: {core_v}
Ring Oscillator Readings: {osc}
"""
return summary
bus = smbus.SMBus(1)
#Event looping
while True:
if plug_in_detect:
plug = Detect_Plugin
else:
plug = "N/A"
voltage = VoltageReadings(bus)
capacity = CapacityReading(bus)
current_time = os.popen("date").read().split("\n")[0]
VCR = VideoCoreReadings()
OSI = OSInfo()
#print(VCR)
#print(VideoCoreReadings.vc_temp)
#VideoCoreReadings = vc_temp, arm_clock, core_clock, h264_clock, core_volts, gpu_ram, ring_osc_info
if int(capacity) <= 28:
off_plan = True
Finish_Line = LineUpdates(voltage, capacity, current_time, VCR['vc_temp'], VCR['arm_clock'], VCR['core_clock'], VCR['h264_clock'], VCR['core_volts'], VCR['gpu_ram'], VCR['ring_osc_info'], plug, off_plan, OSI['up_since'], OSI['uptime'], OSI['hostname_v4'])
os.system("clear")
print(Finish_Line)
if off_plan:
LogSaver(Finish_Line)
os.system("shutdown")
else:
pass
time.sleep(1)
@theskanthunt42
Copy link
Author

Some functions aint there yet(I'm tired

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment