掘金 后端 ( ) • 2024-04-08 14:24

theme: juejin highlight: dark

在TB购买的液晶模块,运行按照文档无法安装,后自己查找资料完成安装

参考文档Adafruit PiOLED - 128x32 Mini OLED for Raspberry Pi


接线示意如下图:一般线材颜色VCC红色,GND黑色,SDA蓝色,SCL黄色 下载.png


# 1. 硬件连接完成开始配置树莓派

sudo raspi-config

开启树莓派配置工具,选择 Interface Options 选项

微信截图_20240408110657.png 然后选择I2C选项会进入确认页面,选择yes或是确认开启,开启I2C。 image.png 然后执行 sudo reboot 重启树莓派,然后执行sudo i2cdetect -y 1 查看oled是否被识别。 出现如下0x3c 说明已被识别。

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f                                                                                                    
00:                         -- -- -- -- -- -- -- --                                                                                                    
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --                                                                                                    
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --                                                                                                    
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- --                                                                                                    
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --                                                                                                    
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --                                                                                                    
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --                                                                                                    
70: -- -- -- -- -- -- -- --  

注:如果没有识别到请确认连线是否良好,线材是否有问题。 执行sudo i2cdetect -l 查看i2c列表,如下图,编号1的i2c已经加载i2c-1 i2c Synopsys DesignWare I2C adapter I2C adapter 执行sudo i2cdetect -y 1 查看oled被识别

image.png


2. 安装Python程序依赖,驱动oled

执行依赖安装命令 更新系统依赖

  • sudo apt-get update

  • sudo apt-get upgrade

  • sudo apt-get install vim

安装ssd1306驱动

sudo pip3 install adafruit-circuitpython-ssd1306

sudo apt-get install python3-pip

Python图像处理库

sudo apt-get install python3-pil

3. 执行数据显示程序,并配置到系统开机启动

程序代码如下:写入python代码 sudo vim oled.py

import time
import os

from board import SCL, SDA
import busio
import adafruit_ssd1306

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

import subprocess


i2c = busio.I2C(SCL, SDA)
# Raspberry Pi pin configuration:
RST = None     # on the PiOLED this pin isnt used

# 128x32 display with hardware I2C:
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)

# Initialize library.
disp.show()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0

# Load default font.
font = ImageFont.load_default()

def getCPULoadRate():
    f1 = os.popen("cat /proc/stat", 'r')
    stat1 = f1.readline()
    count = 10
    data_1 = []
    for i  in range (count):
        data_1.append(int(stat1.split(' ')[i+2]))
    total_1 = data_1[0]+data_1[1]+data_1[2]+data_1[3]+data_1[4]+data_1[5]+data_1[6]+data_1[7]+data_1[8]+data_1[9]
    idle_1 = data_1[3]

    time.sleep(1)

    f2 = os.popen("cat /proc/stat", 'r')
    stat2 = f2.readline()
    data_2 = []
    for i  in range (count):
        data_2.append(int(stat2.split(' ')[i+2]))
    total_2 = data_2[0]+data_2[1]+data_2[2]+data_2[3]+data_2[4]+data_2[5]+data_2[6]+data_2[7]+data_2[8]+data_2[9]
    idle_2 = data_2[3]

    total = int(total_2-total_1)
    idle = int(idle_2-idle_1)
    usage = int(total-idle)
  #  print("idle:"+str(idle)+"  total:"+str(total))
    usageRate = int(float(usage * 100  / total))
    return "CPU:"+str(usageRate)+"%"


while True:
    # Draw a black filled box to clear the image.
    draw.rectangle((0,0,width,height), outline=0, fill=0)
  
    CPU = getCPULoadRate()
    
    cmd = os.popen('vcgencmd measure_temp').readline()
    CPU_TEMP = cmd.replace("temp=","Temp:").replace("'C\n","C")

    cmd = "free -m | awk 'NR==2{printf "Ram: %s /%s MB ", $2-$3,$2}'"
    MemUsage = subprocess.check_output(cmd, shell = True )
    MemUsage = MemUsage.decode('UTF-8','strict')

    cmd = "df -h | awk '$NF=="/"{printf "Disk: %d /%d MB", ($2-$3)*1024,$2*1024}'"
    Disk = subprocess.check_output(cmd, shell = True )
    Disk = Disk.decode('UTF-8','strict')
    
    cmd = "hostname -I | cut -d' ' -f1"
    IP = subprocess.check_output(cmd, shell = True )
    IP = IP.decode('UTF-8','strict')
    # Write two lines of text.

    draw.text((x, top), str(CPU), font=font, fill=255)
    draw.text((x+66, top), str(CPU_TEMP), font=font, fill=255)
    draw.text((x, top+8), str(MemUsage),  font=font, fill=255)
    draw.text((x, top+16), str(Disk),  font=font, fill=255)
    draw.text((x, top+24), "IP:" + str(IP),  font=font, fill=255)

    # Display image.
    disp.image(image)
    disp.show()
    time.sleep(.1) 

编写启动脚本 sudo vim start.sh

#!/bin/sh
sleep 5s
python /home/pi/oled/oled.py & 

注:请确认启动脚本中oled.py是否与你创建的路径保持一致

配置开机启动

sudo vim /etc/rc.local

exit 0 上添加一行

/home/pi/oled/start.sh

执行重启 测试是否正常。

sudo reboot