要想实现日历时钟的获取可以通过第三方天气平台获取 通过申请api。获取到相关json数据后,就从json数据里解析温度和天气信息与时间信息 此处学习下来我还不太明白如何把获取到的json解析成数据这个测试代码部分参考坛友的。
首先导入包
import board
import digitalio
import time
import wifi
import os
import ssl
import socketpool
import adafruit_requests
import displayio
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
# 屏幕基础配置
display = board.DISPLAY
# 使用字体
font_file = "fonts/SJxinweibeijian-Regular-21.bdf"
font = bitmap_font.load_font(font_file)
# WiFi连接
wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
# JSON获取
JSON_URL = "http://t.weather.itboy.net/api/weather/city/"+os.getenv("city")
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
response = requests.get(JSON_URL)
# JSON分析
weather = response.json()
cityInfo = weather['cityInfo']
city_weather = weather['data']
forecast = city_weather['forecast']
dis_str = ""+cityInfo['parent']+' '+cityInfo['city'] +' '+weather['time'][:11] + forecast[0]['week']
dis_str += '\n 空气质量:'+city_weather['quality'] +" " +forecast[0]['type']
dis_str += "\n 最"+forecast[0]['high']+' 最'+forecast[0]['low']
dis_str += "\n 湿度: "+city_weather['shidu']
# 屏幕显示
text_group = displayio.Group(scale=1,x=0,y=0,)
text_area = label.Label(font, text=dis_str, color=0xFFFFFF, x=10, y=25)
text_group.append(text_area) # Subgroup for text scaling
display.show(text_group)
while True:
pass
运行结果如下
该代码存在bug 并不能准确的定位我所在的城市 等时间充裕一定解决该问题!
本帖最后由 pildio 于 2023-10-24 19:36 编辑