### ex0-1 ### # 下載 Thonny https://thonny.org ### ex0-2 ### # 下載旗標官方的 FM622A 範例程式檔 https://www.flag.com.tw/download.asp?FM622A # 下載官方的 CH341SER 驅動程式 http://www.wch.cn/downloads/CH341SER_EXE.html ### ex0-3 ### >>> "abc".upper() # 使用字串物件 "abc" 的 upper() 方法,將字串轉成大寫 >>> "abc".find('b') # find() 方法尋找 'b' 的位置 (從 0 開始) >>> "abc".replace('b', 'z') # replace() 方法將所有的 'b' 取代成 'z' ### ex0-4 ### >>> 111 + 111 # 整數物件相加 >>> "111" + "111" # 字串物件串聯 ### ex0-5 ### >>> 111 + "111" # 不同型別的資料相加發生錯誤 >>> 111 + int("111") # int() 將字串轉成整數 >>> str(111) + "111" # str() 將整數轉成字串 >>> type(111) # type() 可檢查物件的型別 ### ex0-6 ### >>> n1 = 123456789 # 將整數物件 123456789 指派給變數 n1 >>> n2 = 987654321 # 將整數物件 987654321 指派給變數 n2 >>> n1 + n2 # 實際上是 123456789 + 987654321 ### ex0-7 ### >>> print("abc") # 顯示字串物件 >>> print("abc".upper()) # 顯示字串物件.方法的執行結果 >>> print(111 + 111) # 顯示整數物件運算的結果 ### ex0-8 ### >>> import time # 匯入時間相關的 time 模組 >>> time.sleep(3) # 執行 time 模組的 sleep() 函式,暫停 3 秒 >>> help(time.sleep) # 查詢模組的函式 >>> from time import sleep # 從 time 模組裡匯入 sleep() 函式 >>> sleep(5) # 執行 sleep() 函式,暫停 5 秒 >>> help(sleep) # 查詢匯入的函式 ### ex0-9 ### # 暫停 3 秒後,印出 Hello World! from time import sleep sleep(3) print("Hello World!") ### ex1-1 ### # 用互動模式測試 >>> from machine import Pin # machine 模組匯入 Pin 物件 (類別) >>> led = Pin(2, Pin.OUT) # 建立 2 號腳位的 Pin 物件,設定為輸出腳位,並命名為 led >>> led.value(1) # 高電位,內建 LED 熄滅 >>> led.value(0) # 低電位,內建 LED 點亮 ### ex2-1 ### # 只執行一次的程式 (Lab 01.py) from machine import Pin # machine 模組匯入 Pin 物件 import time # 匯入時間相關的 time 模組 led = Pin(2, Pin.OUT) # 建立 2 號腳位的 Pin 物件,設定為輸出腳位,並命名為 led led.value(0) # 低電位,內建 LED 點亮 time.sleep(0.5) # 暫停 0.5 秒 led.value(1) # 高電位,內建 LED 熄滅 # 不斷重複閃爍 LED 燈 (Lab 02.py) from machine import Pin import time led = Pin(2, Pin.OUT) while True: # while True 為無窮迴圈 led.value(0) time.sleep(0.5) led.value(1) time.sleep(0.5) ### ex3-1 (Lab 03.py) ### from machine import Pin, I2C # 從 machine 模組匯入 Pin 與 I2C 物件 from ssd1306 import SSD1306_I2C # 從 ssd1306 模組匯入 SSD1306_I2C (0.96 吋黃藍雙色 128x64 點陣) 物件 i2c = I2C(scl = Pin(5), sda = Pin(4)) # 建立 I2C 的物件實體 oled = SSD1306_I2C(128, 64, i2c) # 建立 SSD1306_I2C 的物件實體。第一個參數: X 軸大小,第二個參數: Y 軸大小,第三個參數: 傳輸方式 oled.text("I love PYTHON!", 0, 0) # text() 方法建立字串。第一個參數: 欲顯示的字串,第二個參數: X 軸起點,第三個參數: Y 軸起點 oled.show() # show() 顯示文字在 OLED,類似電腦的 print() ### ex4-1 (Lab 04.py) ### from machine import Pin, I2C from ssd1306 import SSD1306_I2C import utime # 匯入可設定單位更小的時間模組 i2c = I2C(scl = Pin(5), sda = Pin(4)) oled = SSD1306_I2C(128, 64, i2c) while True: system_time = utime.ticks_ms() # 取得系統開機後,經過的毫秒數 oled.fill(0) # 顯示前先清除畫面 oled.text("System time: ", 0, 0) # 建立字串 oled.text(str(system_time), 0, 16) # 建立變數 system_time 的值,並轉成字串 oled.show() # 讓 OLED 顯示資料 utime.sleep_ms(100) # 暫停 0.1 秒 ### ex5-1 ### from machine import Pin, I2C from ssd1306 import SSD1306_I2C import framebuf # 匯入 framebuf 模組 oled = SSD1306_I2C(128, 64, I2C(scl = Pin(5), sda = Pin(4))) pic_list = [0x44, 0xee, 0xfe, 0xfe, 0xfe, 0x7c, 0x38, 0x10] # 用 list 容器儲存多個物件,橫向 16 進位 的值 pic_buffer = bytearray(pic_list) # 將 pic_list 轉成 bytearray pic = framebuf.FrameBuffer(pic_buffer, 8, 8, # 使用 FrameBuffer() 建立自建圖形。第一個參數: bytearrary,第二個參數: 圖形的 X 軸大小,第三個參數: 圖形的 Y 軸大小,第四個參數: 表示單色、橫向, 最高位元代表最左邊的點 framebuf.MONO_HLSB) oled.blit(pic, 0, 0) # blit() 建立 bytearray 的影像 oled.show() ### ex5-2 (Lab 05.py) ### from machine import Pin, I2C from ssd1306 import SSD1306_I2C import framebuf oled = SSD1306_I2C(128, 64, I2C(scl = Pin(5), sda = Pin(4))) pic_list = [0x1e, 0x3f, 0x7e, 0xfc, 0x7e, 0x3f, 0x1e, 0x00] # 用 list 容器儲存多個物件,縱向 16 進位 的值 pic_buffer = bytearray(pic_list) pic = framebuf.FrameBuffer(pic_buffer, 8, 8, framebuf.MONO_VLSB) # 第四個參數: 表示單色、縱向, 最高位元代表最下邊的點 oled.text("I", 8, 8) oled.blit(pic, 20, 8) oled.text("PYTHON", 32, 8) oled.rect(4, 4, 80, 16, 1) # rect() 畫方框。第一個參數: X 軸起始點,第二個參數: Y 軸起始點,第三個參數: 長方形的 X 軸大小,第四個參數: 長方的 Y 軸大小,第五個參數: 設定顏色 (沒作用) oled.show() ### ex6-1 (Lab 06.py) ### from machine import Pin, I2C from ssd1306 import SSD1306_I2C from hcsr04 import HCSR04 # 匯入超音波模組 import utime oled = SSD1306_I2C(128, 64, I2C(scl = Pin(5), sda = Pin(4))) sonar = HCSR04(trigger_pin = 14, echo_pin = 12) # 建立 HCSR04 物件。第一個參數: trigger_pin: 觸發腳,第個參數: 發出超音波,echo_pin: 接收腳,讀取收到的超音波 while True: distance = sonar.distance_cm() # distance_cm() 計算距離 (公分) oled.fill(0) oled.text("Distance:", 0, 0) oled.text(str(distance) + " cm", 0, 16) oled.show() print("偵測距離: " + str(distance) + " 公分") # print() 在螢幕上顯示字串,print() 螢幕專屬的內建函式 utime.sleep_ms(25) ### ex7-1 ### # 發出一個長聲 from machine import Pin, PWM buzzer = PWM(Pin(15, Pin.OUT), freq = 880, duty = 512) # freq: 設定音符頻率,duty: 設定音量大小 # 小蜜蜂歌曲 from machine import Pin, PWM import time beeper = PWM(Pin(15, Pin.OUT)) # |5 3 3 -|4 2 2 -|1 2 3 4 5 5 5|,0 表示休止符 notes = [ 392, 330, 330, 0, 349, 294, 294, 0, 262, 294, 330, 349, 392, 392, 392, 0] for note in notes: # 一一取出音符 if note == 0: # 休止符不發音 beeper.duty(0) else: beeper.duty(512) # 設定為一半音量 beeper.freq(note) # 依照音符設定頻率 time.sleep(0.2) # 讓聲音持續 0.2 秒 beeper.duty(0) # 停止發聲 time.sleep(0.1) # 持續無聲 0.1 秒 ### ex7-2 (Lab 07.py) ### from machine import Pin, I2C, PWM from ssd1306 import SSD1306_I2C from hcsr04 import HCSR04 import utime alarm_distance = 10 # 觸發警報距離 oled = SSD1306_I2C(128, 64, I2C(scl = Pin(5), sda = Pin(4))) sonar = HCSR04(trigger_pin = 14, echo_pin = 12) buzzer = PWM(Pin(15, Pin.OUT), freq = 880, duty = 0) while True: distance = sonar.distance_cm() oled.fill(0) oled.text("Distance:", 0, 0) oled.text(str(distance) + " cm", 0, 16) if 2 <= distance <= alarm_distance: # 設定警報距離大於等於 2,小於等於 10 公分 buzzer.duty(512) oled.text("!!! ALARM !!!", 0, 40) print("!!! 觸發警報 !!!") else: buzzer.duty(0) print("無警報") oled.show() utime.sleep_ms(25) ### ex8-1 (Lab 08.py) ### from machine import Pin, I2C, PWM from ssd1306 import SSD1306_I2C from hcsr04 import HCSR04 import utime oled = SSD1306_I2C(128, 64, I2C(scl = Pin(5), sda = Pin(4))) sonar = HCSR04(trigger_pin = 14, echo_pin = 12) buzzer = PWM(Pin(15, Pin.OUT), freq = 784, duty = 0) sound_delay = 500 buzzer_time = utime.ticks_ms() while True: distance = sonar.distance_cm() oled.fill(0) oled.text("Distance:", 0, 0) oled.text(str(distance) + " cm", 0, 16) oled.show() print("偵測距離: " + str(distance) + " 公分") if 2 <= distance <= 50: buzzer.duty(512) utime.sleep_ms(10) buzzer.duty(0) sound_delay = int(distance) * 10 - 10 # 距離越長發聲的間隔也越長,這個數學式一個經驗值 else: sound_delay = 500 utime.sleep_ms(sound_delay) ### ex9-1 (Lab 09.py) ### from machine import Pin, I2C, PWM from ssd1306 import SSD1306_I2C from hcsr04 import HCSR04 import utime note_min = 220 # 最小頻率 (Hz) note_max = 880 # 最大頻率 (Hz) dist_min = 40 # 測距最小距離 (公厘): 4 公分 dist_max = dist_min + (note_max - note_min) # 測距最大距離 (公厘): 70 公分 oled = SSD1306_I2C(128, 64, I2C(scl = Pin(5), sda = Pin(4))) sonar = HCSR04(trigger_pin = 14, echo_pin = 12) buzzer = PWM(Pin(15, Pin.OUT), freq = 0, duty = 0) while True: distance = sonar.distance_mm() # 測量超音波距離 (公厘) if dist_min <= distance <= dist_max: # 若在 4 ~ 70 公分內,則發生不同的聲音 key = note_min + (distance - dist_min) # 自訂數學式 buzzer.freq(key) buzzer.duty(512) else: key = 0 buzzer.duty(0) oled.fill(0) oled.text("Dist: " + str(distance) + " mm", 0, 0) oled.text("Note: " + str(key) + " Hz", 0, 16) oled.show() print("偵測距離: " + str(distance) + " 公厘, 播放頻率: " + str(key) + " 赫茲") utime.sleep_ms(10) ### ex10-1 (Lab 10.py) ### from machine import Pin, I2C from ssd1306 import SSD1306_I2C import bh1750fvi, utime i2c = I2C(scl = Pin(5), sda = Pin(4)) oled = SSD1306_I2C(128, 64, i2c) while True: light_level = bh1750fvi.sample(i2c, mode = 0x23) # sample() 取得亮度。第一個參數: i2c 物件,第二個參數: mode 選擇 BH1750 的三種讀取模式 oled.fill(0) oled.text("Light level:", 0, 0) oled.text(str(light_level) + " lux", 0, 16) oled.show() print("偵測亮度: " + str(light_level) + " lux") ### ex11-1 (Lab 11.py) ### from machine import Pin, PWM, I2C from ssd1306 import SSD1306_I2C import bh1750fvi, utime i2c = I2C(scl = Pin(5), sda = Pin(4)) oled = SSD1306_I2C(128, 64, i2c) buzzer = PWM(Pin(15, Pin.OUT), freq = 110, duty = 0) while True: light_level = bh1750fvi.sample(i2c, mode = 0x23) print("偵測亮度: " + str(light_level) + " lux") oled.fill(0) oled.text("Light level:", 0, 0) oled.text(str(light_level) + " lux", 0, 16) if 20 < light_level < 300: # 自訂亮度 20 ~ 300 為,亮度不足 oled.text("!! Warning !!", 0, 32) oled.text("TOO DARK", 0, 48) print("警告 !! 亮度不足 !!") buzzer.freq(110) buzzer.duty(512) else: # 其他,則代表亮度充足,不提醒 buzzer.duty(0) oled.show() ### ex12-1 (Lab 12.py) ### from machine import Pin, I2C, PWM import bh1750fvi, utime score = 0 # 初始化分數 buzzer = PWM(Pin(15, Pin.OUT), freq = 784, duty = 0) while score < 10: light_level = bh1750fvi.sample(I2C(scl = Pin(5), sda = Pin(4)), mode = 0x23) print("偵測亮度: " + str(light_level) + " lux, 得分: " + str(score)) if light_level > 10000: # 表示雷射槍正確擊中 print("==== 命中! ====") score += 1 buzzer.freq(784) buzzer.duty(512) utime.sleep_ms(100) buzzer.freq(988) utime.sleep_ms(300) buzzer.duty(0) utime.sleep_ms(10) print("=== 遊戲結束! ===") buzzer.freq(784) buzzer.duty(512) utime.sleep_ms(100) buzzer.freq(659) utime.sleep_ms(100) buzzer.freq(523) utime.sleep_ms(300) buzzer.duty(0) ### ex13-1 (Lab 13.py) ### from machine import Pin, I2C, PWM import bh1750fvi, utime def getLightLevel(): data = bh1750fvi.sample(I2C(scl = Pin(5), sda = Pin(4)), mode = 0x23) return data buzzer = PWM(Pin(15, Pin.OUT), freq = 768, duty = 0) count = 0 print("系統校準中, 請讓雷射持續照射在亮度感測器5秒鐘...") while count < 5: light_level = getLightLevel() if light_level > 10000: count += 1 print("已校準 " + str(count) + " 秒...") else: count = 0 utime.sleep_ms(1000) while True: light_level = getLightLevel() if light_level < 10000: print("!! 警報觸發 !!") buzzer.duty(512) else: buzzer.duty(0) print("-- 待命中 --") ### ex17-1 ### from machine import Pin import utime sw520d = Pin(13, Pin.IN, Pin.PULL_UP) while True: print(sw520d.value()) utime.sleep_ms(100) ### ex17-2 (Lab 17.py) ### from machine import Pin, I2C, PWM from ssd1306 import SSD1306_I2C import utime sw520d = Pin(13, Pin.IN, Pin.PULL_UP) oled = SSD1306_I2C(128, 64, I2C(scl = Pin(5), sda = Pin(4))) buzzer = PWM(Pin(15, Pin.OUT), freq = 880, duty = 0) while True: oled.fill(0) oled.text("Shake status:", 0, 0) if sw520d.value() == 1: buzzer.duty(512) oled.text("!!! TIPPED !!!", 0, 16) print("!!! 感測器傾倒 !!!") else: buzzer.duty(0) oled.text("- Standby -", 0, 16) print("感測器狀態正常") oled.show() utime.sleep_ms(100) ### ex18-1 (Lab 18.py) ### from machine import Pin, I2C, PWM from ssd1306 import SSD1306_I2C import utime sw520d = Pin(13, Pin.IN, Pin.PULL_UP) oled = SSD1306_I2C(128, 64, I2C(scl = Pin(5), sda = Pin(4))) buzzer = PWM(Pin(15, Pin.OUT), freq = 392, duty = 0) oled.text("Step counter", 0, 8) oled.show() print("計步器已啟動") count = 0 while True: if sw520d.value() == 1: start_time = utime.ticks_ms() while sw520d.value() == 1: pass if utime.ticks_ms() - start_time > 50: count += 1 print("步數: " + str(count)) buzzer.duty(512) oled.fill(0) oled.text("Count:", 0, 0) oled.text(str(count) + " step(s)", 0, 16) oled.show() buzzer.duty(0) ### ex20-1 ### # IFTTT 官網 https://ifttt.com ### ex20-2 ### # 先測試網路 import network # 新增 network 模組 sta_if = network.WLAN(network.STA_IF) # 設定為 STA_IF 模式 sta_if.active(True) # 啟用 wifi 介面 sta_if.connect('你的WiFi名稱', '你的WiFi密碼') # 設定 wifi 帳號與密碼 print(sta_if.ifconfig()[0]) # 取出並印出 IP 位置 ### ex20-3 ### import network, urequests sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect('你的WiFi名稱', '你的WiFi密碼') while not sta_if.isconnected(): # 若連不上,什麼都不做 (pass),繼續等待 pass print(sta_if.ifconfig()[0]) url = "http://micropython.org/ks/test.html" # 要請求的網址 response = urequests.get(url) # get() 請求網址並讀取回應 if response.status_code == 200: # 請求成功,目標網頁回應 HTTP 碼 200 print("網頁請求成功:") print(response.text) # 印出回應 else: print("網頁請求失敗") ### ex20-4 (Lab 20.py) ### import network, urequests, utime, bh1750fvi from machine import Pin, I2C ssid = "你的WiFi名稱" pw = "你的WiFi密碼" key = "你的金鑰" url = "https://maker.ifttt.com/trigger/light_level/with/key/" + key print("連接 WiFi...") wifi = network.WLAN(network.STA_IF) wifi.active(True) wifi.connect(ssid, pw) while not wifi.isconnected(): pass print("已連上") print("亮度記錄器已啟動") while True: light_level = bh1750fvi.sample(I2C(scl=Pin(5), sda=Pin(4)), mode=0x23) response = urequests.get(url + "?value1=" + str(light_level)) if response.status_code == 200: print("IFTTT 呼叫成功: 傳送亮度 " + str(light_level) + " lux") else: print("IFTTT 呼叫失敗") utime.sleep(5) ### ex21-1 (Lab 21.py) ### import network, urequests, utime from machine import Pin, I2C from hcsr04 import HCSR04 sonar = HCSR04(trigger_pin = 14, echo_pin = 12) ssid = "你的WiFi名稱" pw = "你的WiFi密碼" key = "你的金鑰" url = "https://maker.ifttt.com/trigger/sonar_detected/with/key/" + key print("連接 WiFi: " + ssid + "...") wifi = network.WLAN(network.STA_IF) wifi.active(True) wifi.connect(ssid, pw) while not wifi.isconnected(): pass print("已連上") print("防盜器已啟動") while True: distance = sonar.distance_cm() if 2 <= distance <= 10: print("偵測到不明物 !!!") response = urequests.get(url) if response.status_code == 200: print("IFTTT 呼叫成功: 傳送 Line 通知") else: print("IFTTT 呼叫失敗") utime.sleep(5) else: utime.sleep(0.1) ### ex22-1 #### # 字串容器 >>> string = "52python" # 字串容器:由字元組成 >>> string[0] # 字串以索引取值,結果為:'5' # tuple 容器 >>> tuple = (1, '2', 3) # tuple 容器:由資料物件組成 >>> tuple[0] # tuple 以索引取值,結果為:1 # 串列容器 (list) >>> list = [1, '2', 3] # 串列容器:由資料物件組成 >>> list[2] # 串列以索引取值,結果為:'2' # 集合容器 (set) >>> set = {1, '2', 3} # 集合容器:由資料物件組成,無序所以沒有索引取值 # 字典容器 (directory) >>> dick = {'A':1, 'B':'2', 'C':3} # 字典容器:由資料物件組成,以鍵:值表示 >>> dick['B'] # 字典以鍵取值,結果為:'2' ### ex22-2 ### # ISS 官網 http://open-notify.org/ # 線上 jsonviewer http://jsonviewer.stack.hu/ # 線上 jsonviewer https://codebeautify.org/jsonviewer ### ex22-3 (Lab 22.py) ### import network, urequests, utime from machine import Pin, I2C from ssd1306 import SSD1306_I2C oled = SSD1306_I2C(128, 64, I2C(scl = Pin(5), sda = Pin(4))) ssid = "你的WiFi名稱" pw = "你的WiFi密碼" url = "http://api.open-notify.org/iss-pass.json?lat=25.066667&lon=121.516667" # 查詢最近一次經過台北的時間 print("連接 WiFi...") wifi = network.WLAN(network.STA_IF) wifi.active(True) wifi.connect(ssid, pw) while not wifi.isconnected(): pass print("已連上") response = urequests.get(url) if response.status_code == 200: parsed = response.json() print("JSON 資料查詢成功:") print("") print("國際太空站下次掠過時間:") data = parsed["response"][0] pass_time = int(data["risetime"]) - 946684800 + 28800 pass_localtime = utime.localtime(pass_time) year = str(pass_localtime[0]) month = str(pass_localtime[1]) day = str(pass_localtime[2]) hour = str(pass_localtime[3]) minute = str(pass_localtime[4]) second = str(pass_localtime[5]) duration = str(data["duration"]) date = str(year) + "/" + str(month) + "/" + str(day) time = str(hour) + ":" + str(minute) + ":" + str(second) print(date + " " + time + " (為時 " + duration + " 秒)") oled.fill(0) oled.text("ISS next flyby", 0, 0) oled.text("Date: " + str(year) + "/" + str(month) + "/" + str(day), 0, 24) oled.text("Time: " + str(hour) + ":" + str(minute) + ":" + str(second), 0, 40) oled.text("Duration: " + str(duration) + "s", 0, 56) oled.show() ### ex23-1 ### # marketstack 官網 https://marketstack.com/ # API 說明文件 https://marketstack.com/documentation # API 語法 http://api.marketstack.com/v1/eod?access_key=金鑰&symbols=股票代號 # API 範例 http://api.marketstack.com/v1/eod?access_key=6eda3c86f083f3082770cc05c48ae637&symbols=TSM&limit=1 ### ex23-2 (Lab 23.py) ### import network, urequests from machine import Pin, I2C from ssd1306 import SSD1306_I2C oled = SSD1306_I2C(128, 64, I2C(scl = Pin(5), sda = Pin(4))) ssid = "你的 WiFi 名稱" pw = "你的 WiFi 密碼" key = "你的查詢金鑰" stock_name = "TSM" # 欲查詢的股票名稱 # 產生查詢網址 url = "http://api.marketstack.com/v1/eod?access_key=" + key +"&limit=1&symbols=" + stock_name print("連接 WiFi...") wifi = network.WLAN(network.STA_IF) wifi.active(True) wifi.connect(ssid, pw) while not wifi.isconnected(): pass print("已連上") response = urequests.get(url) if response.status_code == 200: parsed = response.json() print("JSON 資料查詢成功:\n") # 取得第1筆股票資料 stock = parsed["data"][0] # 取得資料中的特定項目 name = stock["symbol"] price = str(stock["close"]) + "USD" trade_time = stock["date"] # 在編輯器輸出資料 print("公司名稱: " + name) print("股價: " + price) print("最後交易時間: " + trade_time) # 在OLED模組顯示資料 oled.fill(0) oled.text("Stock: " + name, 0, 0) oled.text("$: " + price, 0, 16) oled.text("Last trade time:", 0, 32) oled.text(trade_time, 0, 48) oled.show() ### ex24-1 ### # World Time API http://worldtimeapi.org/ # API for Asia/Taipei http://worldtimeapi.org/api/timezone/Asia/Taipei ### ex24-2 (Lab 24.py) ### from machine import Pin, I2C, RTC from ssd1306 import SSD1306_I2C import network, urequests, utime ssid = "你的WiFi名稱" pw = "你的WiFi密碼" url = "http://worldtimeapi.org/api/timezone/Asia/Taipei" web_query_delay = 600000 weekday_name = {0:"Monday", 1:"Tuesday", 2:"Wednesday", 3:"Thursday", 4:"Friday", 5: "Saturday", 6:"Sunday"} oled = SSD1306_I2C(128, 64, I2C(scl = Pin(5), sda = Pin(4))) rtc = RTC() print("連接 WiFi...") wifi = network.WLAN(network.STA_IF) wifi.active(True) wifi.connect(ssid, pw) while not wifi.isconnected(): pass print("已連上") update_time = utime.ticks_ms() - web_query_delay while True: if utime.ticks_ms() - update_time >= web_query_delay: response = urequests.get(url) if response.status_code == 200: parsed = response.json() print("JSON 資料查詢成功") datetime_str = str(parsed["datetime"]) year = int(datetime_str[0:4]) month = int(datetime_str[5:7]) day = int(datetime_str[8:10]) hour = int(datetime_str[11:13]) minute = int(datetime_str[14:16]) second = int(datetime_str[17:19]) subsecond = int(round(int(datetime_str[20:26]) / 10000)) weekday = int(parsed["day_of_week"]) - 1 rtc.datetime((year, month, day, weekday, hour, minute, second, subsecond)) print("系統時間已更新:") print(rtc.datetime()) update_time = utime.ticks_ms() else: print("JSON 資料查詢失敗") weekday_str = " " + weekday_name[rtc.datetime()[3]] date_str = " {:02}/{:02}/{:4}".format(rtc.datetime()[1], rtc.datetime()[2], rtc.datetime()[0]) time_str = " {:02}:{:02}:{:02}".format(rtc.datetime()[4], rtc.datetime()[5], rtc.datetime()[6]) oled.fill(0) oled.text(weekday_str, 0, 8) oled.text(date_str, 0, 24) oled.text(time_str, 0, 40) oled.show() utime.sleep(0.1)