### Lab9-1 ### import network # 新增 network 模組 sta_if = network.WLAN(network.STA_IF) # 設定為 STA_IF 模式 sta_if.active(True) # 啟用 wifi 介面 sta_if.connect('無線網路名稱', '無線網路密碼') # 設定 wifi 帳號與密碼 print(sta_if.ifconfig()[0]) # 取出並印出 IP 位置 ### Lab9-2 ### import network, urequests # 新增 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]) res = urequests.get("http://micropython.org/ks/test.html") # 使用 urequests 模組的 get() 函數,取得網頁內容 print(res.text) # 取得網頁的內容 ### Lab9-3 ### # 註冊 OpenWeatherMap https://openweathermap.org ### Lab9-4 ### # 台北範例語法 http://api.openweathermap.org/data/2.5/weather?q=Taipei,tw&lang=zh_tw&APPID=你的金鑰 # 台北範例語法 + 金鑰 http://api.openweathermap.org/data/2.5/weather?q=Taipei,tw&lang=zh_tw&APPID=94091eb580974bc257c3cb64eb7d237d ### Lab9-5 ### # 將絕對溫度改攝氏溫度的語法 http://api.openweathermap.org/data/2.5/weather?q=Taipei,tw&lang=zh_tw&APPID=94091eb580974bc257c3cb64eb7d237d&units=metric ### Lab9-6 ### 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]) res = urequests.get("http://api.openweathermap.org/data/2.5/weather?q=Taipei,tw&lang=zh_tw&APPID=94091eb580974bc257c3cb64eb7d237d&units=metric") # 取得 OpenWeatherMap 的網站內容 print(res.text) ### Lab9-7 ### {"coord":{"lon":121.53,"lat":25.05},"weather":[{"id":803,"main":"Clouds","description":"多雲","icon":"04n"}],"base":"stations","main":{"temp":29.67,"feels_like":35.34,"temp_min":27.78,"temp_max":31,"pressure":1009,"humidity":89},"visibility":10000,"wind":{"speed":3.6,"deg":90},"clouds":{"all":75},"dt":1600079605,"sys":{"type":1,"id":7949,"country":"TW","sunrise":1600033177,"sunset":1600077574},"timezone":28800,"id":1668341,"name":"Taipei","cod":200} # 線上 jsonviewer http://jsonviewer.stack.hu/ # 線上 jsonviewer https://codebeautify.org/jsonviewer ### Lab9-8 ### import network, urequests, ujson # 新增 ujson 模組 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]) res = urequests.get("http://api.openweathermap.org/data/2.5/weather?q=Taipei,tw&lang=zh_tw&APPID=94091eb580974bc257c3cb64eb7d237d&units=metric") print(res.text) j = ujson.loads(res.text) # 載入 JSON 格式資料 print(j["main"]["temp"]) # 依照結構透過字典取得資料 print(j["weather"][0]["id"]) # 依照結構循字典、串列取得資料 ### Lab9-9 (Lab13.py) ### import network, urequests, ujson, machine sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect('FLAG-SCHOOL', '12345678') while not sta_if.isconnected(): pass print(sta_if.ifconfig()[0]) # 顯示 IP 位址 res = urequests.get( # API 網址 "https://api.openweathermap.org/data/2.5/weather?" + "q=" + "Taipei" + ",TW" + # 指定城市與國別 "&units=metric&lang=zh_tw&" + # 使用攝氏單位 "appid=" + # 以下填入註冊後取得的 API key "be95d10b9f29a0b5dfde30d109afb831") j = ujson.loads(res.text); # 從 JSON 轉成字典 gLED = machine.Pin(16, machine.Pin.OUT) # 控制綠燈 rLED = machine.Pin(14, machine.Pin.OUT) # 控制紅燈 weatherID = j["weather"][0]["id"] # 天氣狀況代碼 weatherDesc = j["weather"][0]["description"] # 天氣狀況 if weatherID < 800: # 雨天 rLED.value(1) # 亮紅燈 gLED.value(0) else: # 沒下雨 rLED.value(0) # 亮綠燈 gLED.value(1) print("目前天氣:", str(weatherID)) print("代碼意義:", weatherDesc)