【得捷电子Follow me第2期】任务4-1:日历&时钟&天气
[复制链接]
本帖最后由 转生异世界成为码农 于 2023-8-16 17:06 编辑
实验步骤:
1. 硬件准备
2. 连接wifi
3. 访问网络接口
4. 解析接口数据
5. 显示数据信息
1. 硬件设备
器件:使用 Adafruit ESP32-S3 TFT Feather 连接电脑
Adafruit ESP32-S3 TFT Feather是由开源硬件行业知名公司Adafruit出品的一款富有特色的开源硬件,开发板使用乐鑫ESP32-S3芯片,支持WiFi和蓝牙能,自带高清TFT彩色显示屏
2. 连接到wifi
代码如下: 在code.py 添加如下代码完成连接wifi
##连接wifi --------------------------------------------------------------------------------------
# SPDX-FileCopyrightText: 2020 Brent Rubell for Adafruit Industries
# SPDX-License-Identifier: MIT
import os
import wifi
print("ESP32-S3 Station Test")
print(f"My MAC address: {[hex(i) for i in wifi.radio.mac_address]}")
print("Available WiFi networks:")
for network in wifi.radio.start_scanning_networks():
print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
network.rssi, network.channel))
wifi.radio.stop_scanning_networks()
print(f"Connecting to {os.getenv('WIFI_SSID')}")
wifi.radio.connect('OnePlus 11', '88888888')
print(f"Connected to {os.getenv('WIFI_SSID')}")
print(f"My IP address: {wifi.radio.ipv4_address}")
## END连接wifi --------------------------------------------------------------------------------------
2. 访问网络接口
代码如下: 在code.py 添加如下代码完成 访问接口
## 访问网络 --------------------------------------------------------------------------------------
import ssl
import socketpool
import adafruit_requests
## 数据来源: https://www.sojson.com/api/weather.html
## api http://t.weather.sojson.com/api/weather/city/101280601
JSON_TIME_URL = "http://t.weather.sojson.com/api/weather/city/101280601"
pool = socketpool.SocketPool(wifi.radio)
# 请求网络获取JSON
requests = adafruit_requests.Session(pool, ssl.create_default_context())
## END访问网络 --------------------------------------------------------------------------------------
3. 解析接口数据
代码如下: 在code.py 添加如下代码完成 解析接口数据
## 读取解析Json ---------------------------------------------------------------------------------------
print(f"Fetching and parsing json from {JSON_TIME_URL}")
response = requests.get(JSON_TIME_URL)
print("-" * 40)
## print(f"Time: {response.json()['sysTime2']}")
print("-" * 40)
print(response.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']+' pm2.5:'+str(city_weather['pm25']) +' pm10:'+str(city_weather['pm10'])
dis_str += "\n 注意!! "+forecast[0]['notice']
## END读取解析Json ---------------------------------------------------------------------------------------
5. 显示数据信息
代码如下: 在code.py 添加如下代码完成 显示数据信息
## 显示天气信息 ---------------------------------------------------
import board
import displayio
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font
display = board.DISPLAY
board.DISPLAY.brightness = 0.9
board.DISPLAY.rotation = 0
## 导入汉字 字体文件
font = bitmap_font.load_font("wenquanyi_10pt.pcf")
## 字体颜色
color = 0x9499CA
text_group = displayio.Group()
text_area = label.Label(font, text=dis_str, color=color)
text_area.x = 0
text_area.y = 10
text_area.line_spacing = 0.8
text_area.scale = 1
text_group.append(text_area)
display.show(text_group)
while True:
pass
## END显示天气信息 ---------------------------------------------------
|