【得捷电子Follow me第2期】任务4.1:日历&时钟
[复制链接]
【得捷电子Follow me第2期】任务4.1:日历&时钟
本文的代码 “参考” 了许多帖子的代码,望海涵
完整代码
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
|