web-monitor/TablePI.py
2025-06-25 21:20:24 -04:00

32 lines
970 B
Python

from networktables import NetworkTables
import time
import psutil
NetworkTables.initialize(server='10.26.48.2=') # Resiving device IP
table = NetworkTables.getTable("PiStatus")
while True:
cpu = psutil.cpu_percent()
temps = psutil.sensors_temperatures()
mem = psutil.virtual_memory()
cputemp = "Unavailable"
for label in ["coretemp", "cpu-thermal", "k10temp", "cpu_thermal"]:
if label in temps and temps[label]:
cputemp = f"{temps[label][0].current:.1f}"
break
else:
# to force it to work when on PI
try:
with open("/sys/class/thermal/thermal_zone0/temp") as f:
cputemp = f"{int(f.read()) / 1000:.1f}"
except:
pass
table.putNumber("CPU Usage", cpu)
table.putNumber("Temperature", float(cputemp) if cputemp != "Unavailable" else -1)
table.putNumber("Memory", mem.used // (1024**2))
print("Sent data to laptop")
time.sleep(1)