软件篇
分享一下自己搞的树莓派电子相框;记录下都做了些什么操作
1. 系统镜像
树莓派安装有GUI的系统(清华源)
https://mirrors.tuna.tsinghua.edu.cn/raspberry-pi-os-images/
2. 替换软件源
2.1 备份 /etc/apt/sources.list
1
| cp /etc/apt/sources.list /etc/apt/sources.list.bak
|
2.2 修改里面的软件源为
1
| deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/
|
3. 安装vim,feh,zerotier
1 2 3 4 5 6 7
| sudo apt-get update sudo apt-get install -y vim feh
curl -s https://install.zerotier.com | sudo bash zerotier-cli join a0cbf4b62abd56ec
|
4. feh脚本
info打印出“从python脚本中获取到的时间、天气信息”
harmony字体是我从电脑上复制的ttf字体,一定要是ttf,不能是ttc(feh中不生效)
1 2 3 4 5 6 7 8 9 10 11 12
| #!/bin/bash
feh /home/pi/Pictures/ -rzFYZ -D 60 -R 120 --info "python3 /home/pi/Desktop/info.py" -C /usr/share/fonts/truetype/harmonyos_sans_sc -e HarmonyOS_Sans_SC_Regular/30
|
5. 群晖服务器定时脚本
这一步是用来远程同步树莓派上的相册。可以有很多其他方法。
1 2 3
| #!/bin/bash
rsync -azrtvP /var/services/homes/twh/Drive/pi_photos/ pi@[ip]:/home/pi/Pictures/
|
6. 树莓派上开机自启动feh脚本
1 2
| mkdir /home/pi/.config/autostart vim /home/pi/.config/autostart/feh.desktop
|
feh脚本的内容如下:
1 2 3 4
| [Desktop Entry] Type=Application Name=feh Exec=/home/pi/Desktop/start.sh
|
7. 设置树莓派屏幕常亮,禁止树莓派屏幕休眠
1 2 3 4 5
| sudo vim /etc/lightdm/lightdm.conf
xserver-command=X -s 0-dpms
sudo reboot
|
8. 定时控制背光亮度
1 2 3 4 5 6
| echo X > /sys/class/backlight/rpi_backlight/brightness
0 22 * * * /home/pi/Desktop/turn_down_brightness.sh 0 6 * * * /home/pi/Desktop/turn_up_brightness.sh
|
这里有坑,每次重启树莓派后 /sys/class/backlight/rpi_backlight/brightness 这个权限会背重置,导致我的crontab执行失败,我的解决办法是在root用户下开机启动的时候执行命令修改权限
1 2 3 4
| sudo su vim /etc/rc.local
chmod 666 /sys/class/backlight/rpi_backlight/brightness
|
调亮脚本
1 2
| #!/bin/bash echo 200 > /sys/class/backlight/rpi_backlight/brightness
|
调暗脚本
1 2
| #!/bin/bash echo 30 > /sys/class/backlight/rpi_backlight/brightness
|
9. 显示日期时间
利用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
|
import json import time
tempeature = '-' weather = '未知' response = {} wind_dir = '东风' wind_scale = '0'
with open('/home/pi/Desktop/city_weather.json', 'r', encoding='UTF-8') as f: response = json.loads(f.read())
if response["code"] == "200": tempeature = response["now"]["temp"] weather = response["now"]["text"] wind_dir = response["now"]["windDir"] wind_scale = response["now"]["windScale"]
date = time.strftime("%Y-%m-%d %H:%M", time.localtime())
print("{0}\n上海: {1}, {2}℃\n{3}{4}级".format(date, weather, tempeature, wind_dir, wind_scale))
|
10. 显示天气
利用python脚本实现,调用和风天气api
获取到天气数据后,存在一个json文件里
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
import requests import json import time
key = "xxxx" location_id = "101020100"
url = "https://devapi.qweather.com/v7/weather/now?key={0}&location={1}".format(key, location_id)
payload={} headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
if response.status_code == 200: ffw = open('/home/pi/Desktop/city_weather.json', 'w', errors='ignore') ffw.write(response.text) ffw.close() print('Updated!') else: print('Nothing Change.')
|
11. 为减少和风天气api调用次数,每小时更新一次数据
1 2
| 0 * * * * python3 /home/pi/Desktop/weather.py
|
12. 重启,KO
