得捷电子 Follow me 第2期任务提交—— 分任务:日历&时钟
[复制链接]
要想获取天气的话先要去高德申请一个api操作如下:
- 先注册登录高德(网站 高德开放平台 | 高德地图API (amap.com))
-
-
- 成功后就有个这个
- 添加key
6、然后我们需要记录一下这个
待会要用到
下面就是代码部分
#----------天气获取-----------#
#Wifi 相关头文件
import os
import wifi
#网络请求相关文件
import adafruit_requests
import socketpool
import ssl
#定义api请求地址
Weather_URL = 'https://restapi.amap.com/v3/weather/weatherInfo?city=500106&key=这里是你的key'
poem_URL = "https://v1.jinrishici.com/all.json" #这个地方是我加的古诗
# 2. 创建套接字池
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
response = requests.get(Weather_URL)
weather = response.json()
# 关闭连接
response.close()
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
response_poem = requests.get(poem_URL)
poem = response_poem.json()
print(poem)
#关闭连接
response_poem.close()
poem_item = f"{poem['origin']} "
poem_author = f" {poem['author']} "
poem_content = f" {poem['content']}"
#----------这里是获取返回的json的数据格式
weather = f"{weather['lives'][0]['weather']} {weather['lives'][0]['temperature']}℃ {weather['lives'][0]['humidity']}%"
#---------显示字体---------------#
# 加载字体并定义字体颜色为黑色
font = bitmap_font.load_font("/font/wenquanyi_13px.pcf")
color = 0xFFD700 # 黄色
color1 = 0x000000 # 黑色
color2 = 0xFF69B4 # 粉色
#年月日
text_year =str(time.tm_year)+"年"+str(time.tm_mon)+"月"+str(time.tm_mday)+"日"
data = label.Label(font, text=text_year, color=color2)
data.x = 30
data.y = 10
group.append(data)
display.show(group)
#星期
text_week = str(week(time.tm_wday))
data = label.Label(font, text=text_week, color=color2)
data.x = 50
data.y = 30
group.append(data)
display.show(group)
#时间
text_time = f"{time.tm_hour}:{min(time.tm_min)}:{sec(time.tm_sec)}"
data = label.Label(font, text=text_time, color=color2)
data.x = 43
data.y = 50
group.append(data)
display.show(group)
#天气温度
text_weather = weather
data = label.Label(font, text=text_weather, color=color2)
data.x = 30
data.y = 70
group.append(data)
display.show(group)
#古诗
text_poem = str(poem_item)
data = label.Label(font,text=text_poem,color = color2)
print(len(text_poem))
if len(text_poem) > 2 and len(text_poem) < 10:
data.x = 43
if len(text_poem) > 10:
data.x = 10
if len(text_poem) <= 2:
data.x = 60
data.y = 85
group.append(data)
display.show(group)
text_poem = str(poem_author)
data = label.Label(font,text=text_poem,color = color2)
data.x = 50
data.y = 100
group.append(data)
display.show(group)
text_poem = str(poem_content)
data = label.Label(font,text=text_poem,color = color2)
data.x = 10
data.y = 115
group.append(data)
display.show(group)
二、是时间的获取
------------获取实时时间-------------#
#使用adafruit_ntp.NTP函数初始化ntp服务
import adafruit_ntp,socketpool,ssl
pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=8, server="ntp.aliyun.com")
time = ntp.datetime
三、运行的结果
|