【Digi-Key Follow Me Issue 2】Task 4-1: Calendar & Clock & Weather
[Copy link]
This post was last edited by Reincarnated in Another World Becomes a Coder on 2023-8-16 17:06
Experimental steps:
1. Hardware Preparation
2. Connect to Wi-Fi
3. Access the network interface
4. Parsing interface data
5. Display data information
1. Hardware
Device: Use Adafruit ESP32-S3 TFT Feather to connect to the computer
Adafruit ESP32-S3 TFT Feather is a unique open source hardware produced by Adafruit, a well-known company in the open source hardware industry. The development board uses Espressif's ESP32-S3 chip, supports WiFi and Bluetooth, and comes with a high-definition TFT color display.
2. Connect to wifi
The code is as follows: Add the following code in code.py to complete the wifi connection
##连接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. Access the network interface
The code is as follows: Add the following code in code.py to complete the access interface
## 访问网络 --------------------------------------------------------------------------------------
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. Parsing interface data
The code is as follows: Add the following code in code.py to complete the parsing of interface data
## 读取解析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. Display data information
The code is as follows: Add the following code in code.py to complete the display of data information
## 显示天气信息 ---------------------------------------------------
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显示天气信息 ---------------------------------------------------
|