查看引脚定义

依赖安装

1pip install psutil luma.core luma.oled

代码

 1import subprocess
 2import time
 3import socket
 4import psutil
 5from luma.core.interface.serial import i2c, spi
 6from luma.core.render import canvas
 7from luma.oled.device import ssd1306, ssd1325, ssd1331, sh1106
 8 
 9serial = i2c(port=2, address=0x3C)
10device = ssd1306(serial)
11 
12def get_cpu_temperature():
13    with open('/sys/class/thermal/thermal_zone0/temp', 'r') as file:
14        temperature = int(file.read()) / 1000.0
15    return temperature
16 
17cmd = "hostname -I | cut -d\' \' -f1"
18IP = subprocess.check_output(cmd, shell=True).decode().strip()
19 
20    # 将字节转换为 GB
21def bytes_to_gb(bytes):
22    return bytes / (1024 ** 3)
23 
24# 将字节转换为合适的单位
25def convert_size(size):
26    # 定义单位列表
27    units = ['B', 'KB', 'MB', 'GB', 'TB']
28 
29    # 迭代转换单位直到小于1024
30    for unit in units:
31        if size < 1024:
32            return f"{size:.2f} {unit}"
33        size /= 1024
34 
35    return f"{size:.2f} {units[-1]}"
36 
37# 获取初始的输入输出流量
38net_io_start = psutil.net_io_counters()
39 
40while True:
41    with canvas(device) as draw:
42        cmd = "hostname -I | cut -d\' \' -f1"
43        current_IP = subprocess.check_output(cmd, shell=True).decode().strip()
44        timestamp_start = time.time()
45 
46        # 等待一秒钟
47        time.sleep(1)
48        
49        cpu_usage = psutil.cpu_percent(interval=1)
50        cpu_temperature = get_cpu_temperature()
51 
52        # 获取内存占用情况
53        memory = psutil.virtual_memory()
54 
55        # 获取总内存大小(以 GB 为单位)
56        total_memory = bytes_to_gb(memory.total)
57 
58        # 获取可用内存大小(以 GB 为单位)
59        available_memory = bytes_to_gb(memory.available)
60 
61        # 获取已使用内存大小(以 GB 为单位)
62        used_memory = bytes_to_gb(memory.used)
63 
64        # 获取内存使用率
65        memory_usage = memory.percent
66 
67        disk = psutil.disk_usage('/')
68        total_disk = bytes_to_gb(disk.total)
69        free_disk = bytes_to_gb(disk.free)
70 
71        # 获取当前的输入输出流量和时间戳
72        net_io_current = psutil.net_io_counters()
73        timestamp_current = time.time()
74 
75        # 计算时间间隔
76        time_interval = timestamp_current - timestamp_start
77 
78        # 计算上行和下行网速
79        upload_speed = (net_io_current.bytes_sent - net_io_start.bytes_sent) / time_interval 
80        download_speed = (net_io_current.bytes_recv - net_io_start.bytes_recv) / time_interval
81        upload_speed = upload_speed / 2
82 
83        # 切换单位
84        upload_speed = convert_size(upload_speed) 
85        download_speed = convert_size(download_speed)
86 
87 
88        # 更新初始的输入输出流量
89        net_io_start = net_io_current
90 
91        draw.rectangle(device.bounding_box, outline="white", fill="black") # 让屏幕周围显示一个框
92        draw.text((2,1),f"IP:{current_IP}",fill="white")
93        draw.text((2,11),f"CPU_use:{cpu_usage}%",fill="white")
94        draw.text((2,21),f"CPU_tem:{cpu_temperature:.2f}°C",fill="white")
95        draw.text((2,31),f"MEM:{memory_usage:.2f}%",fill="white")
96        draw.text((2,41), f"DISK:{free_disk:.2f}/{total_disk:.2f}GB", fill="white")
97        draw.text((2,51), f"NET:{upload_speed}/{download_speed}", fill="white")

运行