这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » 继续把玩ESP32-S3ReverseTFTFeatherbyAdafruit(

共2条 1/1 1 跳转至页

继续把玩ESP32-S3ReverseTFTFeatherbyAdafruit(五)

专家
2026-10-10 12:25:39     打赏

接上次的帖子,

https://forum.eepw.com.cn/thread/401842/1

时间同步完成了,现在赶下时髦,用ai完成一个天气预报的时钟,效果如下:

图片.png


import os
import time
import rtc
import ssl
import socketpool
import wifi
import adafruit_ntp
import adafruit_requests

# 屏幕显示库
import board
import terminalio
import displayio
from adafruit_display_text import label

# ---------- 1. 连接 WiFi ----------
print("Connecting WiFi...")
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"),
                   os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print("Connected, IP:", wifi.radio.ipv4_address)

# ---------- 2. 网络会话 ----------
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

# ---------- 3. NTP 同步时间(东八区) ----------
ntp = adafruit_ntp.NTP(pool, tz_offset=8, server="pool.ntp.org")
rtc.RTC().datetime = ntp.datetime
print("Time synced!")

# ---------- 4. 初始化屏幕(240x135) ----------
display = board.DISPLAY
group = displayio.Group()

SCALE = 1   # 原始字号(不放大)

WHITE = 0xFFFFFF
YELLOW = 0xFFFF00
CYAN = 0x00FFFF
ORANGE = 0xFFA500
GRAY = 0xAAAAAA

# 标题:固定位置
title_label = label.Label(terminalio.FONT, text="QINGDAO WEATHER", color=YELLOW, scale=SCALE)
title_label.x = 8
title_label.y = 5
group.append(title_label)

# 天气主行(描述 + 温度):固定位置
weather_label = label.Label(terminalio.FONT, text="Loading...", color=CYAN, scale=SCALE)
weather_label.x = 8
weather_label.y = 20
group.append(weather_label)

# 详细信息(体感/湿度/风速):固定位置
detail_label = label.Label(terminalio.FONT, text="", color=ORANGE, scale=SCALE)
detail_label.x = 8
detail_label.y = 35
group.append(detail_label)

# 时间:固定位置
time_label = label.Label(terminalio.FONT, text="", color=WHITE, scale=SCALE)
time_label.x = 8
time_label.y = 65
group.append(time_label)

# 更新状态行:固定位置
status_label = label.Label(terminalio.FONT, text="", color=GRAY, scale=SCALE)
status_label.x = 8
status_label.y = 95
group.append(status_label)

display.root_group = group

# ---------- 5. 查询青岛天气 ----------
LAT, LON = 36.0671, 120.3826
weather_url = (
    "https://api.open-meteo.com/v1/forecast?"
    f"latitude={LAT}&longitude={LON}"
    "&current=temperature_2m,relative_humidity_2m,"
    "apparent_temperature,weather_code,wind_speed_10m"
    "&timezone=Asia%2FShanghai"
)

def weather_desc(code):
    return {
        0: "Clear", 1: "Mostly Clear", 2: "Partly Cloudy", 3: "Overcast",
        45: "Fog", 48: "Rime Fog", 51: "Drizzle", 53: "Light Drizzle",
        55: "Drizzle", 61: "Light Rain", 63: "Rain", 65: "Heavy Rain",
        80: "Light Showers", 81: "Showers", 82: "Heavy Showers",
        71: "Light Snow", 73: "Snow", 75: "Heavy Snow", 86: "Snow Showers",
        95: "Thunderstorm", 96: "Thunderstorm Hail", 99: "Severe Hail",
    }.get(code, f"Code {code}")

def update_weather():
    try:
        resp = requests.get(weather_url)
        data = resp.json()
        resp.close()
        cur = data["current"]
        temp = cur["temperature_2m"]
        feels = cur["apparent_temperature"]
        humidity = cur["relative_humidity_2m"]
        wind = cur["wind_speed_10m"]
        desc = weather_desc(cur["weather_code"])

        # 天气主行:描述 + 温度
        weather_label.text = f"{desc}  {temp:.1f}C"
        # 详细信息:体感、湿度、风速
        detail_label.text = (f"Feels like: {feels:.1f}C   Humidity: {humidity}%\n"
                             f"Wind speed: {wind} km/h")
        status_label.text = f"Updated {now_str()}"
        print("Weather updated")
    except Exception as e:
        weather_label.text = "FAIL"
        status_label.text = str(e)
        print("Weather error:", e)

def now_str():
    t = time.localtime()
    return f"{t.tm_hour:02d}:{t.tm_min:02d}:{t.tm_sec:02d}"

update_weather()

# ---------- 6. 主循环 ----------
while True:
    now = time.localtime()
    # 时间:完整日期 + 时刻
    time_label.text = (f"{now.tm_year}-{now.tm_mon:02d}-{now.tm_mday:02d} "
                       f"{now.tm_hour:02d}:{now.tm_min:02d}:{now.tm_sec:02d}")

    # 每 30 分钟刷新天气
    if now.tm_min % 30 == 0 and now.tm_sec == 0:
        update_weather()

    time.sleep(1)


将下面文件夹直接复制到电脑虚拟的盘符中即可:

circuitpy.zip

图片.png

话说使用ai完成这种小玩意,真是速度。

图片.png





关键词: 继续     把玩     ESP32-S3ReverseTFTF    

专家
2026-10-11 09:01:08     打赏
2楼

谢谢楼主分享


共2条 1/1 1 跳转至页

回复

匿名不能发帖!请先 [ 登陆 注册 ]