from machine import I2C, Pin, RTC
from ssd1306 import SSD1306_I2C
import network
import time
import ntptime
# Wokwi固定WiFi
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = ""
# OLED初始化
i2c = I2C(0, sda=Pin(21), scl=Pin(22), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
week_map = ["MON","TUE","WED","THU","FRI","SAT","SUN"]
x = 128
rtc = RTC()
# 连接WiFi+对时
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print("连接Wokwi-GUEST...")
wlan.connect(WIFI_SSID, WIFI_PASS)
while not wlan.isconnected():
time.sleep_ms(500)
ntptime.settime()
connect_wifi()
# 主循环
while True:
oled.fill(0)
y,mon,day,wk,h,m,s,_ = rtc.datetime()
# 核心修复:UTC时间 +8小时 转为北京时间
h = h + 8
if h >= 24:
h = h - 24
# 滚动名字
oled.text("Name:Xu", x, 0)
x -= 1
if x < -80:
x = 128
oled.text(f"Date:{y}-{mon:02d}-{day:02d}", 0, 16)
oled.text(f"Week:{week_map[wk]}", 0, 32)
oled.text(f"Time:{h:02d}:{m:02d}:{s:02d}", 0, 48)
oled.show()
time.sleep_ms(100)