树莓派
我拥有的树莓派型号是 3b。
树莓派默认用户名 pi
,默认密码 raspberry
。
微雪墨水屏控制代码仓库(GitHub)。
微雪2.7inch e-Paper HAT(2.7英寸电子墨水屏)文档(Wiki)。
以下操作需提前配置网络。
运行程序
运行的测试程序: examples/epd_2in7_test.py
。
1git clone https://github.com/waveshareteam/e-Paper.git
2vim e-Paper/RaspberryPi_JetsonNano/python/app.py
1### e-Paper/RaspberryPi_JetsonNano/python/app.py
2
3#!/usr/bin/python
4# -*- coding:utf-8 -*-
5
6import sys
7import os
8picdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'pic')
9libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
10if os.path.exists(libdir):
11 sys.path.append(libdir)
12
13import logging
14from waveshare_epd import epd2in7
15import time
16from PIL import Image,ImageDraw,ImageFont
17import traceback
18
19# custom
20from datetime import datetime
21import requests
22import RPi.GPIO
23
24button1 = 5
25button2 = 6
26button3 = 13
27button4 = 19
28
29RPi.GPIO.setmode(RPi.GPIO.BCM)
30RPi.GPIO.setup(button1, RPi.GPIO.IN, pull_up_down=RPi.GPIO.PUD_UP)
31RPi.GPIO.setup(button2, RPi.GPIO.IN, pull_up_down=RPi.GPIO.PUD_UP)
32RPi.GPIO.setup(button3, RPi.GPIO.IN, pull_up_down=RPi.GPIO.PUD_UP)
33RPi.GPIO.setup(button4, RPi.GPIO.IN, pull_up_down=RPi.GPIO.PUD_UP)
34
35epd = epd2in7.EPD()
36
37epd.init()
38epd.Clear(0xFF)
39
40font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
41font16 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 16)
42font36 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 36)
43font84 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 84)
44
45# Location
46Lct = ['hangzhou','beijing','tianjin','shanghai']
47lct = Lct[0]
48c = 0
49
50def refreshWeather():
51 Himage = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
52 draw = ImageDraw.Draw(Himage)
53
54 key = "YOUR_KEY"
55 url = "https://api.seniverse.com/v3/weather/now.json?key=" + key + "&location=" + lct + "&language=zh-Hans&unit=c"
56
57 weaData = requests.get(url)
58 cityName = weaData.json()['results'][0]['location']['name'] # city
59 cityWea = weaData.json()['results'][0]['now']['text'] # weather
60 cityTemp = weaData.json()['results'][0]['now']['temperature'] + '°C' # temp
61
62 draw.text((10, 10), cityName, font = font36, fill = 0)
63 draw.text((90, 10), cityWea, font = font36, fill = 0)
64 draw.text((10, 50), cityTemp, font = font84, fill = 0)
65
66 # show the refresh date
67 dayTime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
68 draw.text((10, 150), u'更新时间:', font = font16, fill = 0)
69 draw.text((100, 150), dayTime, font = font16, fill = 0)
70
71 epd.display(epd.getbuffer(Himage))
72
73try:
74 refreshWeather()
75 while True:
76 if RPi.GPIO.input(button1) == 0:
77 time.sleep(0.1)
78 if RPi.GPIO.input(button1) == 0:
79 c = c - 1
80 if c < 0:
81 c = 3
82 lct = Lct[c]
83 refreshWeather()
84 if RPi.GPIO.input(button2) == 0:
85 time.sleep(0.1)
86 if RPi.GPIO.input(button2) == 0:
87 c = c + 1
88 if c >= 4:
89 c = 0
90 lct = Lct[c]
91 refreshWeather()
92 if RPi.GPIO.input(button3) == 0:
93 epd.Clear(0xFF)
94 if RPi.GPIO.input(button4) == 0:
95 time.sleep(0.1)
96 if RPi.GPIO.input(button4) == 0:
97 epd2in7.epdconfig.module_exit()
98 exit()
99
100except IOError as e:
101 logging.info(e)
102
103except KeyboardInterrupt:
104 logging.info("ctrl + c:")
105 epd2in7.epdconfig.module_exit()
106 exit()
感谢 @guanqr 的《树莓派利用水墨屏显示实时天气状况》。
结合 systemd 实现打开树莓派自动运行
1sudo vim /etc/systemd/system/e-paper.service
1# systemd unit file for the e-Paper
2[Unit]
3# Human readable name of the unit
4Description=e-Paper
5
6[Service]
7# Command to execute when the service is started
8ExecStart=/usr/bin/python /home/pi/e-Paper/RaspberryPi_JetsonNano/python/app.py
9# Disable Python's buffering of STDOUT and STDERR, so that output from the
10# service shows up immediately in systemd's logs
11Environment=PYTHONUNBUFFERED=1
12# Automatically restart the service if it crashes
13Restart=on-failure
14# Our service will notify systemd once it is up and running
15Type=notify
16User=pi
17
18[Install]
19# Tell systemd to automatically start this service when the system boots
20# (assuming the service is enabled)
21WantedBy=default.target