入坑树莓派-04-OLED屏幕
1.点亮屏幕
OLED屏幕指的是这个:
这是块0.96寸的单色屏幕,分辨率是:128*64,芯片型号是SSD1306。
开启它依然需要启用I2C总线并安装smbus库,方法在上一篇文章中可以找到。
使用i2cdetect命令能看到总线地址,其中0x3c就是OLED屏幕的地址。
另外还要安装一个Adafruit_Python_SSD1306的库(之前的例程中已经运行过的就不用再运行了,我也不知道这里面哪些代码是真正有用的,没试,直接全部安装了):
apt-get update
apt-get upgrade
pip3 install --upgrade setuptools
apt-get install python3-pip
pip3 install RPI.GPIO
pip3 install adafruit-circuitpython-ssd1306
apt-get install python3-pip
apt-get install python3-pil
git clone https://github.com/adafruit/Adafruit_CircuitPython_SSD1306.git
cd Adafruit_CircuitPython_SSD1306-master
python setup.py install
运行下面的python代码,就可以点亮屏幕了:
import time
import busio
import adafruit_ssd1306
from board import SCL, SDA
from PIL import Image, ImageDraw, ImageFont
OLED_WIDTH = 128
OLED_HEIGHT = 64
i2c = busio.I2C(SCL, SDA)
oled = adafruit_ssd1306.SSD1306_I2C(OLED_WIDTH, OLED_HEIGHT, i2c)
while True:
oled.fill(0)
image = Image.new('1', (oled.width, oled.height))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('/root/board/fonts/Courier_New.ttf', 32)
draw.text((0, 0), 'Hello', font=font, fill=255)
draw.text((0, 32), 'World', font=font, fill=255)
oled.image(image)
oled.show()
time.sleep(1)
效果如下(由于相机和屏幕刷新频率问题,拍不全):
2.展示更多信息
接下来我们就可以用这块屏幕展示一些有用的信息了。
比如,ip地址信息(上面的是wifi地址,下面的是有线网卡地址):
日期时间信息:
系统统计信息(CPU负载、内存占用、磁盘占用、CPU温度)
环境温湿度信息(温度好像不准,虚高):
温湿度的传感器是这个(DHT11):
使用温湿度传感器需要安装额外的库:
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
2.
cd Adafruit_Python_DHT
python setup.py install
python3 setup.py install
CPU各核心实时频率:
代码如下(各个屏幕滚动展示):
import sys
import time
import socket
import fcntl
import struct
import board
import busio
import subprocess
import Adafruit_DHT
import adafruit_ssd1306
from board import SCL, SDA
from PIL import Image, ImageDraw, ImageFont
font_cn = ImageFont.truetype('/root/board/fonts/微软雅黑.ttf', 14)
font_en = ImageFont.truetype('/root/board/fonts/Courier_New.ttf', 14)
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', str.encode(ifname[:15]))
)[20:24])
def show_screen_ip():
oled.fill(0)
WIFIADDR = ""
WIREADDR = ""
try:
WIFIADDR += get_ip_address('wlan0')
except IOError:
WIFIADDR += "0.0.0.0"
try:
WIREADDR += get_ip_address('eth0')
except IOError:
WIREADDR += "0.0.0.0"
image = Image.new('1', (oled.width, oled.height))
draw = ImageDraw.Draw(image)
draw.text((0, 10), WIFIADDR, font=font_en, fill=255)
draw.text((0, 30), WIREADDR, font=font_en, fill=255)
oled.image(image)
oled.show()
def show_screen_time():
oled.fill(0)
image = Image.new('1', (oled.width, oled.height))
draw = ImageDraw.Draw(image)
draw.text((0, 0), time.strftime("%A"), font=font_en, fill=255)
draw.text((0, 20), time.strftime("%Y-%m-%d"), font=font_en, fill=255)
draw.text((0, 40), time.strftime("%X"), font=font_en, fill=255)
oled.image(image)
oled.show()
def show_screen_stats():
oled.fill(0)
image = Image.new('1', (oled.width, oled.height))
draw = ImageDraw.Draw(image)
cmd = "top -bn1 | grep load | awk '{printf \"%.2f\", $(NF-2)}'"
cpustr = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "free -m | awk 'NR==2{printf \"%s MB\", $3 }'"
memstr = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "df -h | awk '$NF==\"/\"{printf \"%d GB\", $3}'"
diskstr = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "expr `cat /sys/class/thermal/thermal_zone0/temp` / 1000"
tempstr = subprocess.check_output(cmd, shell=True).decode("utf-8")
draw.text((0, 0), "CPU: " + cpustr, font=font_en, fill=255)
draw.text((0, 14), "MEM: " + memstr, font=font_en, fill=255)
draw.text((0, 28), "DSK: " + diskstr, font=font_en, fill=255)
draw.text((0, 44), "TMP: " + tempstr, font=font_en, fill=255)
oled.image(image)
oled.show()
def show_screen_climate():
oled.fill(0)
image = Image.new('1', (oled.width, oled.height))
draw = ImageDraw.Draw(image)
humistr, tempstr = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 4)
draw.text((0, 0), '温度 : {0:0.1f}度'.format(tempstr), font=font_cn, fill=255)
draw.text((0, 20), '湿度 : {0:0.1f}%'.format(humistr), font=font_cn, fill=255)
oled.image(image)
oled.show()
def show_screen_cpufreq():
oled.fill(0)
image = Image.new('1', (oled.width, oled.height))
draw = ImageDraw.Draw(image)
cmd = "expr `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq` / 1000"
cpu0str = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "expr `cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq` / 1000"
cpu1str = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "expr `cat /sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq` / 1000"
cpu2str = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "expr `cat /sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq` / 1000"
cpu3str = subprocess.check_output(cmd, shell=True).decode("utf-8")
draw.text((0, 0), "CPU0 : " + cpu0str, font=font_en, fill=255)
draw.text((0, 15), "CPU1 : " + cpu1str, font=font_en, fill=255)
draw.text((0, 30), "CPU2 : " + cpu2str, font=font_en, fill=255)
draw.text((0, 45), "CPU3 : " + cpu3str, font=font_en, fill=255)
oled.image(image)
oled.show()
i2c = busio.I2C(SCL, SDA)
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
while True:
# ip地址
show_screen_ip()
time.sleep(2)
# 时间
show_screen_time()
time.sleep(2)
# 统计信息
show_screen_stats()
time.sleep(2)
# 环境信息
show_screen_climate()
time.sleep(2)
# CPU频率
show_screen_cpufreq()
time.sleep(2)