import network
import ntptime
# 連接 Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Wokwi-GUEST", "")#(ssid,password)
# 等待連接
while not wlan.isconnected():
pass
print("連接無線網路成功")
#-----------------------------------
from machine import Pin, SoftI2C
import ssd1306
import ds1307
import time
# 初始化軟件 I2C
i2c_rtc = SoftI2C(scl=Pin(22), sda=Pin(21))
i2c_oled = SoftI2C(scl=Pin(18), sda=Pin(19))
# 初始化 OLED 顯示器
oled = ssd1306.SSD1306_I2C(128, 64, i2c_oled)
# 初始化實時時鐘
ds = ds1307.DS1307(i2c_rtc)
#---------------------------------
# 設置網絡時間
ntptime.settime()
# 設定時區為 +8 (台北時間)
tm = time.localtime(time.time() + 3600 * 8)
#ds.datetime ((year,month,day,weekday,hour,minute,second))
ds.datetime ((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5]))
english_weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
while True:
# 獲取 DS1307 時間
print(ds.datetime())#(2024, 3, 29, 5, 10, 29, 45, 0)
twTime=ds.datetime()
# 顯示時間
oled.fill(0)
yymmdd=str(twTime[0])+'/'+str(twTime[1])+'/'+str(twTime[2])
hhmmss=str(twTime[4])+'/'+str(twTime[5])+'/'+str(twTime[6])
weekday=english_weekdays[twTime[3]]
x0=31
oled.text(yymmdd, x0, 0)
oled.text(weekday, x0+10, 12)
oled.text(hhmmss, x0+5, 24)
oled.show()
time.sleep(1)