### ex5-1 ### from machine import Pin import tm1637 tm = tm1637.TM1637(clk = Pin(16), dio = Pin(17)) >>> tm.number(7) # 顯示一組數字 >>> tm.numbers(7, 5) # 顯示兩組數字 ### ex5-2 ### >>> import time >>> time.localtime() ### ex5-3 ### import network import ntptime import time sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect('無線網路名稱', '無線網路密碼') while not sta_if.isconnected(): pass print(sta_if.ifconfig()[0]) >>> ntptime.settime() # 與 UTC (世界協調時間) 同步 >>> time.localtime() # 擷取時間 >>> time.mktime(time.localtime()) # 將時間簡化為秒 >>> TW_sec = time.mktime(time.localtime()) + 28800 # 增加 8 小時 (28800 秒) >>> TW = time.localtime(TW_sec) # 將秒數轉換回正常時間 ### ex5-4 ### import tm1637 from machine import Pin import ntptime import time import network tm = tm1637.TM1637(clk = Pin(16), dio = Pin(17)) sta=network.WLAN(network.STA_IF) sta.active(True) sta.connect('無線網路名稱', '無線網路密碼') while not sta.isconnected(): pass print('Wi-Fi連線成功') ntptime.settime() TW_sec = time.mktime(time.localtime()) + 28800 while True: TW = time.localtime(TW_sec) print(TW) hour = TW[3] # 時 minu = TW[4] # 分 tm.numbers(hour, minu) time.sleep(1) TW_sec += 1 ### ex5-5 ### from machine import Pin, PWM buzzer = PWM(Pin(23, Pin.OUT)) buzzer.freq(0) buzzer.duty(512) ### ex5-6 ### from machine import Pin, PWM import time buzzer = PWM(Pin(23, Pin.OUT), freq = 0, duty = 512) # 建立 PWM 物件 buzzer.freq(349) # 發出 Fa 聲 time.sleep(1) buzzer.freq(294) # 發出 Re 聲 time.sleep(1) buzzer.deinit() ### ex5-7 ### # PTX 服務平台 https://ptx.transportdata.tw/PTX/ ### ex5-8 ### # 取得指定[車站]列車即時到離站電子看板(動態前後30分鐘的車次)。更新頻率:2分鐘。 https://ptx.transportdata.tw/MOTC/v2/Rail/TRA/LiveBoard/Station/火車站 ID # 取得台北車站,取得前 5 筆資料 (top = 5),以 JSON 格式顯示。 https://ptx.transportdata.tw/MOTC/v2/Rail/TRA/LiveBoard/Station/1000?$top=5&$format=JSON ### ex5-9 ### # 線上 JSON Viewer https://codebeautify.org/jsonviewer ### ex5-10 ### import network, urequests sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect('無線網路名稱', '無線網路密碼') while not sta_if.isconnected(): pass print(sta_if.ifconfig()[0]) headers = {'user-agent':'curl/7.76.1'} res = urequests.get("https://ptx.transportdata.tw/MOTC/v2/Rail/TRA/LiveBoard/Station/1000?$top=5&$format=JSON", headers = headers) print(res.text) >>> j = res.json() # 載入並解析 JSON 格式資料 (轉成 Python 的資料結構) >>> j[0]["DelayTime"] # 從第 0 筆資料取出 DelayTime 項目的資料 ### ex5-11 ### # IFTTT https://ifttt.com/ ### ex5-12 ### # 無會員:當天次數 50 次 from machine import Pin, PWM import network import urequests import time import tm1637 import ntptime tm = tm1637.TM1637(clk=Pin(16), dio=Pin(17)) # 四位數顯示器 tm.write([0, 0, 0, 0]) # 清空四位數顯示器 sta=network.WLAN(network.STA_IF) sta.active(True) sta.connect('無線網路名稱','無線網路密碼') while not sta.isconnected() : pass print('Wi-Fi連線成功') ntptime.settime() s_number = 1202 # 想查詢的車號 sta_number = 1000 # 想查詢的車站 (1000是台北) ifttt_url = "IFTTT請求網址" headers = {'user-agent':'curl/7.76.1'} # 讓網站認為請求是使用瀏覽器發出。因為有些網頁會擋爬蟲程式 # 查詢指定火車起點與終點 train_url = ("https://ptx.transportdata.tw/MOTC/v2/Rail/TRA/GeneralTrainInfo/TrainNo/" + str(s_number) +"?$format=JSON") train_res = urequests.get(train_url, headers = headers) if (train_res.status_code == 200): pass else: print("傳送失敗") print("錯誤碼:", train_res.status_code) train_j = train_res.json() print("\n車號:", s_number) print(train_j[0]['StartingStationName']['Zh_tw'] + " → " + train_j[0]['EndingStationName']['Zh_tw']) train_res.close() remind = False # 車子第一次出現在時刻表時要提醒使用者 # 查詢火車時刻表 time_url = ("https://ptx.transportdata.tw/MOTC/v2/Rail/TRA/LiveBoard/Station/" + str(sta_number) + "?$top=20&$format=JSON") while True: time_res = urequests.get(time_url, headers = headers) if (time_res.status_code == 200): pass else: print("傳送失敗") print("錯誤碼:", time_res.status_code) time_j = time_res.json() time_res.close() number = [] # 將車號加入 number 列表中 for i in range(len(time_j)): number.append(time_j[i]['TrainNo']) print("\n即時班車號碼:", number) if (str(s_number) in number): # 有查到對應車號, 顯示延遲時間 if (remind == False): buzzer_pin = Pin(23, Pin.OUT) # 蜂鳴器 buzzer = PWM(buzzer_pin, freq = 0, duty = 50) buzzer.freq(349) time.sleep(1) buzzer.freq(294) time.sleep(1) buzzer.deinit() remind = True ind = number.index(str(s_number)) print("\n表定發車時間:", time_j[ind]['ScheduledArrivalTime']) print("延遲時間:", time_j[ind]['DelayTime'], "分鐘") tm.number(time_j[ind]['DelayTime']) res = urequests.get(ifttt_url + "?value1=" + str(time_j[ind]['DelayTime'])) if (res.status_code == 200): pass else: print("傳送失敗") print("錯誤碼:", res.status_code) res.close() else: # 沒有查到對應車號 print("\n目前無" + str(s_number) + "號火車") TW_sec = time.mktime(time.localtime()) + 28800 TW = time.localtime(TW_sec) hour = TW[3] minu = TW[4] tm.numbers(hour, minu) remind = False time.sleep(300) # 暫停 300 秒