### ex8-1 #### >>> string = "52python" # 字串容器:由字元組成 >>> tuple = (1, '2', 3) # tuple 容器:由資料物件組成 >>> list = [1, '2', 3] # 串列容器:由資料物件組成 >>> set = {1, '2', 3} # 集合容器:由資料物件組成 >>> dick = {'A':1, 'B':'2', 'C':3} # 字典容器:由資料物件組成,以鍵:值表示 ### ex8-2 ### import network, urequests ssid = "你的WiFi名稱" pw = "你的WiFi密碼" print("連接 WiFi...") wifi = network.WLAN(network.STA_IF) wifi.active(True) wifi.connect(ssid, pw) while not wifi.isconnected(): pass print("已連上") url = "http://api.icndb.com/jokes/random" # API 網址 response = urequests.get(url) # 呼叫 API 和取得回應資料 parsed = response.json() # 把回應資料轉成 JSON 格式 print(parsed) # 印出 JSON 內容 ### ex8-3 ### # JSON 線上排版器 http://jsonviewer.stack.hu/ ### ex8-4 ### import network, urequests ssid = "你的WiFi名稱" pw = "你的WiFi密碼" print("連接 WiFi...") wifi = network.WLAN(network.STA_IF) wifi.active(True) wifi.connect(ssid, pw) while not wifi.isconnected(): pass print("已連上") url = "https://api.icndb.com/jokes/random" # API 網址 response = urequests.get(url) # 呼叫 API 和取得回應資料 parsed = response.json() # 把回應資料轉成 JSON 格式 joke = parsed["value"]["joke"] print(joke) ### ex8-5 ### # 國際太空站官網 http://open-notify.org/ # API 網址 http://api.open-notify.org/iss-pass.json?lat=緯度&lon=經度 # 華盛頓中學的經緯度參數 http://api.open-notify.org/iss-pass.json?lat=24.158500138139527&lon=120.74514781162085 ### ex8-6 ### # Unix timestamp 時間戳線上轉換工具 https://www.cadch.com/article/timestamp/index.php ### ex8-7 ### import network, urequests ssid = "你的WiFi名稱" pw = "你的WiFi密碼" print("連接 WiFi...") wifi = network.WLAN(network.STA_IF) wifi.active(True) wifi.connect(ssid, pw) while not wifi.isconnected(): pass print("已連上") url = "http://api.open-notify.org/iss-pass.json?lat=24.158500138139527&lon=120.74514781162085" # API 網址 response = urequests.get(url) # 呼叫 API 和取得回應資料 parsed = response.json() # 把回應資料轉成 JSON 格式 data = parsed["response"][0] # 取得 response 下的第一筆資料 time = data["risetime"] print(time) ### ex8-8 ### 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=24.158500138139527&lon=120.74514781162085" 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] # D1 mini 的初始時間是 2000/01/01,0 時,與 Unix 時間戳差了 946684800 秒,此外 Unix 時間戳是世界協調時間 (UTC),需加上 28800 (8 * 60 * 60 秒 = 8 小時),才能得到台北當地時間 (UTC + 8) 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() ### ex8-9 ### # marketstack 官網 https://marketstack.com/ # API 語法 http://api.marketstack.com/v1/eod?access_key=金鑰&symbols=股票代號 # API 範例 http://api.marketstack.com/v1/eod?access_key=6eda3c86f083f3082770cc05c48ae637&symbols=TSM ### ex8-10 ### 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() ### ex8-11 ### # World Time API http://worldtimeapi.org/ # API for Asia/Taipei http://worldtimeapi.org/api/timezone/Asia/Taipei ### ex8-12 ### 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)