树莓派4B加微雪2.13寸墨水屏,监测pm2.5信息


获取pm2.5信息的教程参照我的博客https://my.oschina.net/wangandi/blog/901648
微雪2.13寸墨水屏的资料参照http://www.waveshare.net/wiki/2.13inch_e-Paper_HAT 缺什么软件包也请参照这里

打开spi
vim /boot/config.txt
添加
dtparam=spi=on

pip镜像源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

1
2
3
4
5
6
pacman -S gcc python-numpy make wiringpi freetype2 python-pillow
pip install spidev RPi.GPIO requests
pip install --upgrade setuptools
pip install wiringpi
pip install --no-cache-dir pillow
pip install --upgrade pip

如果rpi.gpio安装出错,下载https://sourceforge.net/code-snapshots/hg/r/ra/raspberry-gpio-python/code/raspberry-gpio-python-code-08048dd1894a6b09a104557b6eaa6bb68b6baac5.zip
解压后运行python setup.py install

载样例包https://github.com/waveshare/e-Paper
下面的python文件要放在e-paper/RaspberryPi&JetsonNano/python/examples/里面
代中的url参照第一个网址的getData.php,格式如下{“pm”:”(23,32,35,23,32,35,3669,1042,136,13,1,1,10.0,19.9,64.4)”,”date”:”2020-02-24 13:40:01”},我代码中另一个树莓派的地址,你也可以在同一个树莓派中使用
python代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import os
import datetime
import requests
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
if os.path.exists(libdir):
sys.path.append(libdir)
import logging
from waveshare_epd import epd2in13_V2
import time
from PIL import Image,ImageDraw,ImageFont
import traceback
logging.basicConfig(level=logging.DEBUG)
try:
logging.info("epd2in13_V2 Demo")
epd = epd2in13_V2.EPD()
logging.info("init and Clear")
epd.init(epd.FULL_UPDATE)
#epd.Clear(0xFF)
# Drawing on the image
font15 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 15)
font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
logging.info("1.Drawing on the image...")
image = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
draw = ImageDraw.Draw(image)
#GetTime
#time = (datetime.datetime.now() + datetime.timedelta(hours=8)).strftime('%Y.%m.%d %H:%M:%S')
#PrintWord
#draw.text((20, 40), '呵呵', font = font15, fill = 0)
#PrintTime
#draw.text((20, 80), time, font = font15, fill = 0)
#Your PM2.5 data url
#Content for example:
#{"pm":"(25,37,46,25,37,46,3519,982,166,30,5,3,10.0,19.8,64.8)","date":"2020-02-24 13:30:03"}
url = "http://192.168.1.196/pm25/getData.php"
#Format pm2.5 data
alldata = requests.get(url)
pmdata = alldata.json()["pm"]
pmdata = pmdata[1:-1]
pmdata = pmdata.split(",")
#Get time
time = alldata.json()["date"]
#Get pm2.5,temperature,humidity
pm25 = pmdata[4]
temperature = pmdata[13]
humidity = pmdata[14]
#Draw all data
draw.text((20, 10), '空气质量:' + pm25 + 'μg/m³', font = font15, fill = 0)
draw.text((20, 30), '温度:' + temperature + '°C', font = font15, fill = 0)
draw.text((20, 50), '湿度:' + humidity + '%', font = font15, fill = 0)
draw.text((20, 70), time, font = font15, fill = 0)
draw.text((20, 90), 'By Andy', font = font15, fill = 0)
#ScreenRotate180
image = image.transpose(Image.ROTATE_180)
epd.display(epd.getbuffer(image))
logging.info("Goto Sleep...")
epd.sleep()
except IOError as e:
logging.info(e)
except KeyboardInterrupt:
logging.info("ctrl + c:")
epd2in13_V2.epdconfig.module_exit()
exit()