【得捷电子Follow me第2期】 任务4:分任务1:日历&时钟
[复制链接]
本帖最后由 knv 于 2023-10-10 00:06 编辑
【得捷电子Follow me第2期】 任务4:分任务1:日历&时钟——完成一个可通过互联网更新的万年历时钟,并显示当地的天气信息
0x0: 相关API
本次使用高德地图的根据IP访问获取地区信息
使用高德地图的API根据地区信息获取天气信息
使用http://worldtimeapi.org/api/ip 获取准确的时间信息
创建secrets.py
secrets = {
"ssid": "这是ssid",
"password": "这是密码",
"timezone": "Asia/Shanghai", # Check http://worldtimeapi.org/timezones
"token":"高德api token"
}
0x1:使用到的库
创建wifi链接信息文件 secrets.py
0x2:编写代码
import board
import wifi
import time
import ssl
import socketpool
import adafruit_requests
import displayio
import terminalio
import adafruit_imageload
from adafruit_display_text import label
import adafruit_bitmap_font.bitmap_font
import rtc
the_rtc = rtc.RTC()
基本库导入完毕后,配置WIFI链接
token = "高德API的token"
### connecte to a WiFi AP (AP name and password in secrets.py) ###
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
获得屏幕信息,设置屏幕方向
screen = board.DISPLAY
screen.rotation = 90
设置一个背景图片
bitmap, palette = adafruit_imageload.load("/imgs/purple.bmp",bitmap=displayio.Bitmap,palette=displayio.Palette)
# Create a TileGrid to hold the bitmap
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette,tile_width=160, tile_height=240,# pixels number
width=1, height=1) # tiles number
创造一些显示信息
FONT = adafruit_bitmap_font.bitmap_font.load_font("mfyh-16.bdf")
arealabel = label.Label(FONT, x=0, y=32, text="北京市", scale=1, color=(255,255,255))
wtlabel = label.Label(FONT, x=0, y=62, text="晴", scale=1, color=(255,255,255))
wdlabel = label.Label(FONT, x=0, y=92, text="温度:", scale=1, color=(255,255,255))
sdlabel = label.Label(FONT, x=0, y=122, text="湿度:", scale=1, color=(255,255,255))
yearlabel = label.Label(FONT, x=0, y=152, text="时间:", scale=1, color=(255,255,255))
monthlabel = label.Label(FONT, x=0, y=182, text="", scale=1, color=(255,255,255))
daylabel = label.Label(FONT, x=0, y=212, text="", scale=1, color=(255,255,255))
# Create a Group to hold the TileGrid
group = displayio.Group()
# Add the TileGrid to the Group
group.append(tile_grid)
group.append(arealabel)
group.append(wtlabel)
group.append(wdlabel)
group.append(sdlabel)
group.append(yearlabel)
group.append(monthlabel)
group.append(daylabel)
screen.show(group)
tile_grid[0] = 0
time.sleep(1.0)
打印WIFI信息,创造SOKCET连接池
print("IP addr:", wifi.radio.ipv4_address)
response = None
pool = socketpool.SocketPool(wifi.radio)
# 建立连接
session = adafruit_requests.Session(pool, ssl.create_default_context())
使用高德API获取ip的区域代码
while True:
response = session.get("https://restapi.amap.com/v3/ip?key="+token)
data = response.json()
#print("data:",data["adcode"])
adcode = data["adcode"]
time.sleep(1)
break
使用worldtimeapi 获得地区时间
while True:
response = session.get("http://worldtimeapi.org/api/ip")
json = response.json()
current_time = json["datetime"]
the_date, the_time = current_time.split("T")
year, month, mday = [int(x) for x in the_date.split("-")]
the_time = the_time.split(".")[0]
hours, minutes, seconds = [int(x) for x in the_time.split(":")]
# We can also fill in these extra nice things
year_day = json["day_of_year"]
week_day = json["day_of_week"]
# Daylight Saving Time (夏令时)?
is_dst = json["dst"]
now = time.struct_time(
(year, month, mday, hours, minutes, seconds+1, week_day, year_day, is_dst) )
the_rtc.datetime = now
time.sleep(1)
break
loopdata = 600
使用高德API定时刷新数据信息
while True:
#yearlabel.text = "时间:"+str(the_rtc.datetime.tm_year)
monthlabel.text = ""+str(the_rtc.datetime.tm_mon)+"-"+str(the_rtc.datetime.tm_mday)+""
daylabel.text = "" + str(the_rtc.datetime.tm_hour) + ":" + str(the_rtc.datetime.tm_min) + ":"+str(the_rtc.datetime.tm_sec)+""
try:
if loopdata==600:
response = session.get("https://restapi.amap.com/v3/weather/weatherInfo?key="+token+"&city="+adcode)
data = response.json()
#print("data:",data["adcode"])
wd = data["lives"][0]["temperature_float"]
sd = data["lives"][0]["humidity_float"]
wdlabel.text ="温度: "+ wd+"°"
sdlabel.text = "湿度: "+sd+"%"
arealabel.text = data["lives"][0]["province"]
wtlabel.text = data["lives"][0]["weather"]
loopdata=0
else:
loopdata = loopdata +1
time.sleep(1)
except:
pass
效果如下:
QQ视频20231009223448
视频链接:
https://training.eeworld.com.cn/video/37981
源码及库的下载链接如下:
https://download.eeworld.com.cn/detail/knv/629533
经过本次得捷电子Follow me第2期 的学习,使我学习了更现代化的单片机开发技巧。使用python 能更快速的开发程序,项目落地。
|