pc init
This commit is contained in:
23
pc.py
Normal file
23
pc.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from pc_utils import get_cpu_usage, get_memory_usage, get_cpu_temp
|
||||
#import serial
|
||||
from time import sleep
|
||||
|
||||
# connect to microbit
|
||||
#ser = serial.Serial('/dev/ttyACM0', 115200, timeout=1)
|
||||
#ser.flush()
|
||||
print("Connected to microbit.")
|
||||
|
||||
while True:
|
||||
sleep(1)
|
||||
cpu_usage = get_cpu_usage()
|
||||
memory_usage = get_memory_usage()
|
||||
cpu_temp = get_cpu_temp()
|
||||
print(f"CPU Usage: {cpu_usage}%")
|
||||
print(f"Memory Usage: {memory_usage}%")
|
||||
print(f"CPU Temperature: {cpu_temp}°C")
|
||||
print('--------')
|
||||
|
||||
data = [cpu_usage, memory_usage, cpu_temp]
|
||||
data_str = ','.join(map(str, data))
|
||||
#send_serial_data(data_str, ser)
|
||||
|
||||
26
pc_utils.py
Normal file
26
pc_utils.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import psutil
|
||||
|
||||
def get_cpu_usage():
|
||||
return round(psutil.cpu_percent(interval=1))
|
||||
def get_memory_usage():
|
||||
memory = psutil.virtual_memory()
|
||||
return round(memory.percent)
|
||||
def get_cpu_temp():
|
||||
try:
|
||||
# linux / mac
|
||||
temp = psutil.sensors_temperatures()['coretemp'][0].current
|
||||
except KeyError:
|
||||
# windows
|
||||
temp = psutil.sensors_temperatures()['cpu_thermal'][0].current
|
||||
return round(temp)
|
||||
|
||||
def send_serial_data(data, ser):
|
||||
if ser.is_open:
|
||||
ser.write(data.encode('utf-8'))
|
||||
print(f"Sent data: {data}")
|
||||
else:
|
||||
print("Serial port is not open.")
|
||||
ser.open()
|
||||
ser.write(data.encode('utf-8'))
|
||||
print(f"Sent data: {data}")
|
||||
ser.close()
|
||||
Reference in New Issue
Block a user