我的树莓派一般用来学习python,因为一直放在桌面上感觉非常枯燥,决定加一个屏幕用来显示天气情况。
闲话不多说,进入正题,先上一个成品图片
最底下是一个硬盘,中间是树莓派,有一个18B20传感器测量室温。
首先硬件的情况:树莓派使用的是4B1G版本(购买比较早,抢购的,130元抢购的,现在价格很高了,可以树莓派其他版本代替);硬盘为250g机械硬盘(因为4B支持usb启动,所以我一直都没用内存卡);树莓派外壳,已被积木包围,建议只购买散热片和风扇,直接使用积木作为外壳;积木直接大家去拼多多或者淘宝搜索微粒积木就可以,我这是买的8毫米500粒混色,不到五块钱,喜欢纯色的可以去买纯色。
屏幕采用的是淘晶驰串口屏2.2寸无触摸,咸鱼30元买的,需要下载软件开发,如有有需要这个UI的请留言。
程序话不多说,直接代码走起,其中天气部分为爬虫爬取(如有需要天气接口的请留言哦)
下面放代码:
GNU nano 3.2 1.txt 已更改
w#!/usr/bin/python3
# coding:utf-8
#采用爬虫模式
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
import serial
import time
import re
from urllib import request
# 打开串口
ser = serial.Serial("/dev/ttyAMA0", 9600)
ser.flushInput()
try:
url = 'https://tianqi.moji.com/today/china/shandong/weifang'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36','Referer':'https://tianqi.moji.com/forecast7/china/shandong',
'Host': 'tianqi.moji.com','sec-ch-ua': ' "\\Not;A\"Brand";v="99", "Google Chrome";v="85", "Chromium";v="85" ','sec-fetch-site':'cross-site'}
req = request.Request(url,headers=headers)
resp = request.urlopen(req)
#print(resp.read().decode('utf-8'))
s = resp.read().decode('utf-8')
ret = re.findall(r'<div\sclass="detail_time clearfix">.*?年(.*?) .*?\n(.*?)\n</div>.*?<div class="detail_wea clearfix">.*?<h2>今天(.*?)</h2>.*?<em>(.*?)</em>.*?<b>(.*?)</b>.*?<span>湿度 (.*?)%</span>.*?<em>(.*?) (.*?)级</em>.*?<h2>今天(.*?)</h2>.*?<em>(.*?)</em>.*?<b>(.*?)°</b>.*?<p><strong>日出 (.*?)</strong>.*?<h2>今天(.*?)</h2>.*?<em>(.*?)</em>.*?<b>(.*?)°</b>.*?<p><strong class="sun_down">日落 (.*?)</strong></p>.*?<b>(.*?)°</b>.*?<strong>(.*?)°</strong>.*?<span class="week">.*?<span class="week">周(.*?)</span>.*?<div class="$ b=['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23']
d = dict(zip(b,ret[0]))
real='page0.t1.txt="' d['4'] '℃ ' d['6'] '%"'
ser.write(real.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
data='page0.t4.txt="' d['5'] '"'
ser.write(data.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
data1='page0.t5.txt="' d['7'] '"'
ser.write(data1.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
data2='page0.t6.txt="' d['8'] '级"'
ser.write(data2.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
except:
data=''
ser.write(data.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
data='page0.t0.txt="' time.strftime('%m月%d日') '"'
ser.write(data.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
if time.strftime('%u')=="0":
week='page0.g0.txt="今天是周末耶!"'
if time.strftime('%u')=="1":
week='page0.g0.txt="今天星期一,美好的一周又开始了!"'
if time.strftime('%u')=="2":
week='page0.g0.txt="今天星期二,仍然需要努力工作!"'
if time.strftime('%u')=="3":
week='page0.g0.txt="今天星期三,本周马上过半了!"'
if time.strftime('%u')=="4":
week='page0.g0.txt="今天星期四,还有两天就周末了!"'
if time.strftime('%u')=="5":
week='page0.g0.txt="今天星期五,还有一天就休息了!"'
if time.strftime('%u')=="6":
week='page0.g0.txt="今天星期六,明天就可以休息了!"'
ser.write(week.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
def aps_test():#每秒刷新
ti=time.strftime('%H:%M:%S')
begin='page0.t3.txt="' ti '"'
ser.write(begin.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
tim=time.strftime('%H:%M')
#print(tim)
if tim=="18:00":
begin='dims=0'
ser.write(begin.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
if tim=="08:00":
begin='dims=100'
ser.write(begin.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
def aps_test1():#每小时刷新
'''
try:
url = 'https://tianqi.moji.com/today/china/shandong/weifang'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36','Referer':'https://tianqi.moji.com/forecast7/china/shandong',
'Host': 'tianqi.moji.com','sec-ch-ua': ' "\\Not;A\"Brand";v="99", "Google Chrome";v="85", "Chromium";v="85" ','sec-fetch-site':'cross-site'}
req = request.Request(url,headers=headers)
resp = request.urlopen(req)
#print(resp.read().decode('utf-8'))
s = resp.read().decode('utf-8')
ret = re.findall(r'<div\sclass="detail_time clearfix">.*?年(.*?) .*?\n(.*?)\n</div>.*?<div class="detail_wea clearfix">.*?<h2>今天(.*?)</h2>.*?<em>(.*?)</em>.*?<b>(.*?)</b>.*?<span>湿度 (.*?)%</span>.*?<em>(.*?) (.*?)级</em>.*?<h2>今天(.*?)</h2>.*?<em>(.*?)</em>.*?<b>(.*?)°</b>.*?<p><strong>日出 (.*?)</strong>.*?<h2>今天(.*?)</h2>.*?<em>(.*?)</em>.*?<b>(.*?)°</b>.*?<p><strong class="sun_down">日落 (.*?)</strong></p>.*?<b>(.*?)°</b>.*?<strong>(.*?)°</strong>.*?<span class="week">.*?<span class="week">周(.*?)</span>.*?<div cla$ b=['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23']
d = dict(zip(b,ret[0]))
real='page0.t1.txt="' d['4'] '℃' d['6'] '%"'
ser.write(real.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
data='page0.t4.txt="' d['5'] '"'
ser.write(data.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
data1='page0.t5.txt="' d['7'] '"'
ser.write(data1.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
data2='page0.t6.txt="' d['8'] '级"'
ser.write(data2.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
except:
real=''
ser.write(real.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
'''
data='page0.t0.txt="' time.strftime('%m月%d日') '"'
ser.write(data.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
if time.strftime('%u')=="0":
week='page0.g0.txt="今天是周末耶!"'
if time.strftime('%u')=="1":
week='page0.g0.txt="今天星期一,美好的一周又开始了!"'
if time.strftime('%u')=="2":
week='page0.g0.txt="今天星期二,仍然需要努力工作!"'
if time.strftime('%u')=="3":
week='page0.g0.txt="今天星期三,本周马上过半了!"'
if time.strftime('%u')=="4":
week='page0.g0.txt="今天星期四,还有两天就周末了!"'
if time.strftime('%u')=="5":
week='page0.g0.txt="今天星期五,还有一天就休息了!"'
if time.strftime('%u')=="6":
week='page0.g0.txt="今天星期六,明天就可以休息了!"'
ser.write(week.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
def aps_test2():#每2秒刷新一次
#打开温度传感器文件
tfile = open("/sys/bus/w1/devices/28-3c01f09664a0/w1_slave")
#读取文件所有内容
text = tfile.read()
#关闭文件
tfile.close()
#用换行符分割字符串成数组,并取第二行
secondline = text.split("\n")[1]
#用空格分割字符串成数组,并取最后一个,即t=23000
temperaturedata = secondline.split(" ")[9]
#取t=后面的数值,并转换为浮点型
temperature = float(temperaturedata[2:])
# 转换单位为摄氏度
temperature = (temperature / 1000)
#print(temperature)
ti=time.strftime('%H:%M:%S')
begin='page0.t2.txt="' str(temperature) '"'
ser.write(begin.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
begin='p1.pic=1'
ser.write(begin.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
time.sleep(1)
begin='p1.pic=2'
ser.write(begin.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
def aps_test3():#每天刷新一次,未使用
url = 'https://tianqi.moji.com/today/china/shandong/weifang'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36','Referer':'https://tianqi.moji.com/forecast7/china/shandong',
'Host': 'tianqi.moji.com','sec-ch-ua': ' "\\Not;A\"Brand";v="99", "Google Chrome";v="85", "Chromium";v="85" ','sec-fetch-site':'cross-site'}
req = request.Request(url,headers=headers)
resp = request.urlopen(req)
#print(resp.read().decode('utf-8'))
s = resp.read().decode('utf-8')
ret = re.findall(r'<div\sclass="detail_time clearfix">(.*?) .*?\n(.*?)\n</div>.*?<div class="detail_wea clearfix">.*?<h2>今天(.*?)</h2>.*?<em>(.*?)</em>.*?<b>(.*?)</b>.*?<span>湿度 (.*?)%</span>.*?<em>(.*?)级</em>.*?<h2>今天(.*?)</h2>.*?<em>(.*?)</em>.*?<b>(.*?)°</b>.*?<p><strong>日出 (.*?)</strong>.*?<h2>今天(.*?)</h2>.*?<em>(.*?)</em>.*?<b>(.*?)°</b>.*?<p><strong class="sun_down">日落 (.*?)</strong></p>.*?<b>(.*?)°</b>.*?<strong>(.*?)°</strong>.*?<span class="week">.*?<span class="week">周(.*?)</span>.*?<div class="tree clearf$ b=['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22']
d = dict(zip(b,ret[0]))
week='page0.g0.txt="天王盖地虎,今天星期' d['18'] '"'
ser.write(week.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
data='page0.t2.txt="' d['1'] '"'
ser.write(data.encode("GB2312"))
ser.write(bytes.fromhex('ff ff ff'))
scheduler = BlockingScheduler()
scheduler.add_job(func=aps_test, trigger='cron', second='*/1')#定时任务,每秒执行
scheduler.add_job(func=aps_test1, trigger='cron', minute=0)#定时任务,每小时的第零秒执行
scheduler.add_job(func=aps_test2, trigger='cron', second='*/5')#定时任务,每5秒执行
#scheduler.add_job(func=aps_test3, trigger='cron', hour=0)#定时任务,每小时的第零秒执行
scheduler.start()
,