1.接线
显示器 — 香橙派
SDA — SDA
SCL — SCK
VCC — 5v或者3.3v(看自己买的屏幕所需要的电压)
GND — GND
下面是Orange Pi Zero 2的引脚图
2.测试屏幕
如果你是纯新手,建议按照官方手册的3.24《3.24. I2C 接口的 0.96 寸 OLED 模块使用方法》先点亮屏幕测试。
OrangePi_Zero2_H616_用户手册_v4.0https://www.aliyundrive.com/s/mrc3TKmcjcA注意,官方的oled屏幕是7个针脚的,但是大家一般买到的都是4个针脚的oled1306,所以只用接上文书上面说的四个针脚就行。
根据官方手册,走一遍流程,先点亮 oled_demo确保屏幕、连接没有问题。
3.安装依赖
Python 3:确保您的系统上安装了Python 3解释器
psutil模块:第三方库,用于获取系统状态信息。可以使用以下命令装:
pip install psutil
Adafruit CircuitPython。可以使用以下命令装:
pip install adafruit-circuitpython
Pillow 第三方图像处理库,用于创建和编辑图像。可以使用以下命令安装:
pip install Pillow
adafruit_ssd1306 模块:第三方库,用于控制 SSD1306 OLED 显示屏
其实有两个可以选择
https://github.com/adafruit/Adafruit_Python_SSD1306
https://github.com/adafruit/Adafruit_CircuitPython_SSD1306
选择安装就行
4.编辑python 保存运行
我的代码如下
import time
import shutil
import subprocess
import psutil
import board
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
from datetime import datetime
# 定义屏幕尺寸
WIDTH = 128
HEIGHT = 64
BORDER = 5
# 初始化屏幕
i2c = board.I2C()
oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c)
# 清空屏幕
oled.fill(0)
oled.show()
# 创建图像对象
image = Image.new("1", (WIDTH, HEIGHT))
draw = ImageDraw.Draw(image)
# 加载字体
font = ImageFont.load_default()
def get_disk_usage():
total, used, free = shutil.disk_usage("/")
disk_space = used / (2**30) # 转换为GB
disk_percent = (used / total) * 100 # 计算已用硬盘空间百分比
return disk_space, disk_percent
def get_memory_usage():
mem = psutil.virtual_memory()
memory_usage = mem.used / (2**30) # 转换为GB
memory_percent = (mem.used / mem.total) * 100 # 计算已用内存空间百分比
return memory_usage, memory_percent
def get_cpu_temperature():
temperature = subprocess.check_output(["cat", "/sys/class/thermal/thermal_zone0/temp"]).decode("utf-8")
cpu_temp = float(temperature) / 1000
return cpu_temp
def get_ip_address():
ip_address = subprocess.check_output(["hostname", "-I"]).decode("utf-8").split(" ")[0]
return ip_address
while True:
# 清空图像
draw.rectangle((0, 0, WIDTH, HEIGHT), outline=0, fill=0)
# 获取硬盘空间、内存剩余、CPU温度和IP地址
disk_space, disk_percent = get_disk_usage()
memory_usage, memory_percent = get_memory_usage()
cpu_temperature = get_cpu_temperature()
ip_address = get_ip_address()
# 获取当前日期和时间
now = datetime.now()
current_time = now.strftime("%Y-%m-%d")
time_time = now.strftime("%H:%M:%S")
# 在屏幕上显示硬盘空间、内存剩余、CPU温度和IP地址
draw.text((BORDER, 0), "Disk: {:.2f} GB ({:.2f}%)".format(disk_space, disk_percent), font=font, fill=255)
draw.text((BORDER, 11), "RAM: {:.2f} GB ({:.2f}%)".format(memory_usage, memory_percent), font=font, fill=255)
draw.text((BORDER, 21), "CPU Temp: {:.2f} °C".format(cpu_temperature), font=font, fill=255)
draw.text((BORDER, 32), "IP: {}".format(ip_address), font=font, fill=255)
draw.text((BORDER, 44), "Date: {}".format(current_time), font=font, fill=255)
draw.text((BORDER, 55), "Time: {}".format(time_time), font=font, fill=255)
# 更新屏幕显示
oled.image(image)
oled.show()
# 等待一段时间
time.sleep(1)
保存成CPUtemp.py
上传到orangepi zero2,我是上传到/home/orangepi文件夹内(如图所示)
输入命令运行程序
sudo python3 /home/orangepi/CPUtemp.py
完成!