树莓派 SSD1306 OLED 显示树莓派温度及天气

硬件准备

  1. 树莓派

  2. SSD1306驱动OLED屏幕

  3. 母对母杜邦线

  4. 面包板、公对公(可选)

开始操作

把 oled屏幕引脚 通过 杜邦线 插到 树莓派 对应的引脚,VCC引脚插入3.3v引脚,GND代表接地,SDA要插入SDA.1,SCL插入SCL.1

未试验可否热插拔

ssh连接树莓派(或其他方式)

打开相应接口:

sudo raspi-config

Interfacing Options > I2C 打开I2C

Interfacing Options > SPI 打开SPI

安装相关驱动

sudo apt-get update

sudo apt-get install build-essential python-dev python-pip

sudo pip3 install RPi.GPIO

sudo apt-get install python-imaging python-smbus

sudo pip3 install demjson

从Github上拷贝Adafruit_Python_SSD1306

sudo apt-get install git

git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git 也可以下载其他语言

cd Adafruit_Python_SSD1306

sudo python3 setup.py install

编写程序

import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
import requests
import demjson
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import threading
import subprocess
RST = None
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0
disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
disp.begin()
disp.clear()
disp.display()
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
draw = ImageDraw.Draw(image)
print(str(width) +"," + str(height))
draw.rectangle((0,0,width,height), outline=0, fill=0)
padding = -3
top = padding
bottom = height-padding
x = 0
font = ImageFont.truetype('/home/pi/font/微软vista雅黑.ttf', 14)
#fontTemp = ImageFont.truetype('/home/pi/font/kraash.woff.ttf', 8)

def getWeather():
    while True:
        req = requests.get("http://wthrcdn.etouch.cn/weather_mini?city=%E5%8C%97%E4%BA%AC")
        weatherJson = demjson.decode(req.text)
        weather = weatherJson['data']['forecast'][0]
        global high
        high = weather['high'].replace('高温','').strip()
        global low
        low = weather['low'].replace('低温','').strip()
        global type_
        type_ = weather['type']
        global fengli
        fengli = weather['fengli'].replace('<![CDATA[','').replace(']','').replace('>','')
        time.sleep(60*60)
thead = threading.Thread(target = getWeather)
thead.start()
time.sleep(3)
while True:
    draw.rectangle((0,0,width,height), outline=0, fill=0)
    with open ("/sys/class/thermal/thermal_zone0/temp", 'r') as f_temp:
        temp = round(int(f_temp.read()) /1000,2)
    global high
    global low
    global type_
    global fengli
    draw.text((x, top),low + "~" + high + " " + type_,  font=font, fill=255)
    draw.text((x, top+14),"风力:" + fengli,  font=font, fill=255)
    draw.text((x+68, top+16), str(temp) +"  " ,  font=font, fill=255)
    draw.rectangle((0,13,128,15), outline=0, fill=255)
    draw.rectangle((66,13,68,32), outline=0, fill=255)
    disp.image(image)
    disp.display()
    time.sleep(1)

后台启动

nohup python3 pi_status.py &

效果

学习知识

第一次正经写python程序所以收货颇丰,代码规范不了解,见谅

  1. python多线程

  2. python变量作用域

  3. python字符串操作

  4. python Json操作

  5. python 文件读取相关操作

  6. oled屏幕相关操作

2 评论
    worthy Chrome Windows NT 10.0
    2019-11-18 13:50:02 回复

    羡慕树莓派 羡慕oled 羡慕python 羡慕学习知识

      myth Chrome Mobile iPhone
      2019-11-19 10:08:00

      @worthy:羡慕研究生