Homemade electronic lunar calendar

Hydrology Master is online. Today is a water electronic lunar calendar card.

First of all, let’s talk about electronic accessories. One is the choice of small electronic screens. When encountering a lot of text, especially Chinese characters, don’t choose the traditional 128x64 oled. The above can be seen clearly). I chose ili9341 and thought it was convenient.

Secondly, the room temperature does not use api to grasp the local weather, but uses hardware temperature measurement. Here is the ds18b20. I found that this thing is quite advanced. The accuracy is still good, and the accuracy in the normal temperature range is already very high. It should be noted that its protocol is 1-wire (that is, w1-gpio must be initialized on the board).

As for the relevant information of the lunar calendar, it is captured by the api. Next, send a code journal.

#: 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

The approximate layout diagram is as follows. The main thing is not to forget to add the pull-up resistor to the positive electrode of ds18b20 and both ends of the data line.

Guess you like

Origin blog.csdn.net/u011410413/article/details/132221633