自制电子农历

水文大师上线。今天一水电子农历牌。

首先讲讲电子配件,一来是电子小屏幕的选择,遇到文字比较多的,尤其是汉字,不要选传统那款128x64 oled,绝对放不下(找到最牛的超小免费字体至少要在8pixel以上才能看清楚)。我选了ili9341,觉得趁手。

其次是室温不用api抓的当地天气,而是采用硬件测温,此处上ds18b20,我发现这玩意儿挺高级,管道测温,水下测温包个不锈钢,下去的也是这个模块,可见敏感度还是不错的,常温区间的精确度已经很高了,要注意它的协议是1-wire(即板子上要初始化w1-gpio)。

至于农历的相关信息,是api抓的。接下来发一发代码流水账。

#: cat ds18b20.py

from w1thermsensor import W1ThermSensor
sensor = W1ThermSensor()

def modify_text(int_temp):
    with open('/home/nongLi/info_txt/ds18b20.txt', "r+") as f:
        read_data = f.read()
        f.seek(0)
        f.truncate()   #清空文件
        f.write(int_temp)


temperature = str(sensor.get_temperature()) + '\'C'
modify_text(temperature)
#: cat vcgencmd.py

import os

def temperature_of_raspberry_pi():
    cpu_temp = os.popen("vcgencmd measure_temp").read().splitlines()
    return str(cpu_temp[0].replace("temp=", ""))

def modify_text(int_temp):
    with open('/home/nongLi/info_txt/vcgencmd.txt', "r+") as f:
        read_data = f.read()
        f.seek(0)
        f.truncate()   #清空文件
        f.write(int_temp)

modify_text(temperature_of_raspberry_pi())
#: cat api_info.py
#nongLi: https://eolink.o.apispace.com/453456/lives_geo/v001/calendar?days=2
#HK红日:https://data.gov.hk/sc-data/dataset/hk-effo-statistic-cal
#室温:硬件测温-ds18b20
#cpu温度: vcgencmd

import os
import requests

def modify_text(int_temp):
    with open('/home/nongLi/info_txt/nongLi.txt', "r+") as f:
        read_data = f.read()
        f.seek(0)
        f.truncate()   #清空文件
        f.write(int_temp)

nongLi_url = "https://eolink.o.apispace.com/453456/lives_geo/v001/calendar"
payload = {"days":"2"}


headers = {
    "X-APISpace-Token": "buy it",
    "Authorization-Type":"apikey"
}

response=requests.request("GET", nongLi_url, params=payload, headers=headers)

#print(response.text)

modify_text(response.text)
#: cat txt_integration.py
import os
import json
from datetime import datetime

##农历文本整理
with open('info_txt/nongLi.txt','r') as load_f:
    load_dict = json.load(load_f)
target_dict = load_dict['result'][1]
target_date = datetime.strptime(str(target_dict['date']),'%Y-%m-%d')
txt1 = str(target_dict['date']) + "; " + str(target_dict["moreDetail"]["constellation"]) + "; " + str(target_dict['lunar']) + ' ' + str(target_dict["festival"]) + ' '
txt2 = str(target_dict['ganzhiYear']) + ' ' + str(target_dict['ganzhiMonth']) + ' ' + str(target_dict['ganzhiDay']) + ' '
txt3 = '纳音: ' + str(target_dict["moreDetail"]["elementYear"]) + " " + str(target_dict["moreDetail"]["elementMonth"]) + " " + str(target_dict["moreDetail"]["elementDay"]) + '\n'
txt4 = '彭祖百忌: ' + str(target_dict['moreDetail']['pzTaboo']) + '\n'
txt5 = '宜: ' + str(target_dict['fitting']) + '\n'
txt6 = '忌: ' + str(target_dict['taboo']) + '\n'
txt7 = '冲: ' + str(target_dict["moreDetail"]["chong"]) + '; ' + '煞: ' + str(target_dict["moreDetail"]["sha"]) + '\n'
txt8 = str(target_dict["solarTerm"]) + str(target_dict["stDays"]) + '天,' + str(target_dict["nextSt"]) + str(target_dict["nextstDays"]) + "天后。" + '\n'

##cpu温度整理
with open('/home/nongLi/info_txt/vcgencmd.txt','r') as temp_f:
    rpi_temp = temp_f.read()

##室温整理
with open('/home/nongLi/info_txt/ds18b20.txt','r') as f_temp:
    current_temp = f_temp.read()
txt1 = txt1 + current_temp + '\n'
txt2 = txt2 + rpi_temp + '\n'
txt9 = txt1 + txt2 + txt3 + txt4 + txt5 + txt6 + txt7 + txt8

##香港红日整理
def yyyymmdd_convertor(str_date):
    return datetime.strptime(str_date,'%Y%m%d')

with open("/home/nongLi/info_txt/hk_holidays.json",'r', encoding='utf-8-sig') as decode_f:
    holidays_dict = json.load(decode_f)
hk_list = holidays_dict['vcalendar'][0]['vevent']
hk_summary = ""
hk_flag = 0
for i in hk_list:

    first_date = yyyymmdd_convertor(i['dtstart'][0])
    last_date = yyyymmdd_convertor(i['dtend'][0])
    if target_date == first_date or target_date == last_date:
        #summary = i["summary"]
        hk_flag = 1
        break
    if target_date > first_date and target_date < last_date:
        #summary = i["summary"]
        hk_flag = 1
        break

if hk_flag == 1:
    final_text = txt9 + "港假"
elif hk_flag == 0:
    final_text = txt9
else:
    final_text = "Error Error Error!"

def modify_text(int_temp):
    with open('info_txt/display.txt', "r+") as f:
        read_data = f.read()
        f.seek(0)
        f.truncate()   #清空文件
        f.write(int_temp)

modify_text(final_text)
#: cat display_ili9341.py
import time
import busio
import digitalio
from board import SCK, MOSI, MISO, CE0, D24, D23

from adafruit_rgb_display import color565
import adafruit_rgb_display.ili9341 as ili9341
from PIL import Image,ImageDraw,ImageFont

# Configuration for CS and DC pins:
CS_PIN = CE0
DC_PIN = D24
RESET_PIN = D23

# Setup SPI bus using hardware SPI:
spi = busio.SPI(clock=SCK, MOSI=MOSI)

# Create the ILI9341 display:
disp = ili9341.ILI9341(spi,rotation=270,cs=digitalio.DigitalInOut(CS_PIN),
                          dc=digitalio.DigitalInOut(DC_PIN),rst=digitalio.DigitalInOut(RESET_PIN))

disp.fill(0)

width=disp.width
height=disp.height

# 文字显示
with open('/home/nongLi/info_txt/display.txt','r') as f:
    buffer_txt = f.read()
image1=Image.new('RGB',(height,width))
draw1=ImageDraw.Draw(image1)
font1=ImageFont.truetype('/home/nongLi/fonts/SimSun-special.ttf',12) #SimSun-Special save my day!
draw1.text((0,0),buffer_txt,font=font1,fill=color565(234,255,0))

try:
    disp.image(image1)
except Exception as e:
    print(e)
    disp.fill(0)
#: sudo crontab -e

0 1 * * * /usr/bin/python /home/nongLi/api_info.py  #凌晨1点取一次。
*/10 * * * * /usr/bin/python /home/nongLi/ds18b20_temp.py #每十分钟
*/10 * * * * /usr/bin/python /home/nongLi/vcgencmd.py
*/11 * * * * /usr/bin/python /home/nontLi/txt_integration.py
*/12 * * * * /usr/bin/python /home/nongLi/display_ili9341.py

大概得布局图如下,主要别忘了那个上拉电阻加到ds18b20的正极和数据线两端。

自制电子农历_第1张图片

你可能感兴趣的:(Python,ili9341,树莓派4B)