Wio Terminal 从网络获取天气数据
本文主要介紹如何使用 Wio Terminal 的 WiFi 網(wǎng)絡獲取天氣數(shù)據(jù)。
WiFi 配置
Wio Terminal 配備的無線網(wǎng)卡是 Realtek RTL8720,在開始本次學習之前,請確保你已經(jīng)閱讀 Wio Terminal 網(wǎng)卡固件更新,并完成固件更新和 Arduino 依賴庫的安裝。
我們知道,WiFi 有兩種工作模式:AP 模式和 STA 模式。
- AP(Access Point)也就是無線接入點,是一個無線網(wǎng)絡的創(chuàng)建者,是網(wǎng)絡的中心節(jié)點。一般家庭或辦公室使用的無線路由器就一個 AP。
- STA(Station)也就是站點,每一個連接到無線網(wǎng)絡中的終端(如筆記本電腦、PDA 及其它可以聯(lián)網(wǎng)的用戶設備)都可稱為一個站點。
在本示例中,Wio Terminal 工作在 STA 模式。下面代碼使用 rpcWiFi 庫將 Wio Terminal 連接到指定的 WiFi 網(wǎng)絡。
提示:需要將 ssid 和 password 修改為你的 WiFi 網(wǎng)絡。
#include "rpcWiFi.h"const char* ssid = "yourNetworkName"; const char* password = "yourNetworkPassword";void setup() {Serial.begin(115200);while(!Serial); // Wait for Serial to be ready// Set WiFi to station mode and disconnect from an AP if it was previously connectedWiFi.mode(WIFI_STA);WiFi.disconnect();Serial.println("Connecting to WiFi..");WiFi.begin(ssid, password);while (WiFi.status() != WL_CONNECTED) {delay(500);Serial.println("Connecting to WiFi..");WiFi.begin(ssid, password);}Serial.println("Connected to the WiFi network");Serial.print("IP Address: ");Serial.println (WiFi.localIP()); // prints out the device's IP address}void loop() {}天氣 API
有很多提供天氣信息的 Web API,可以參考《一些好用的天氣 API》,本文使用高德地圖 API 獲取實時天氣及天氣預測。GET 請求的 URL 如下:
實時天氣(當天)
https://restapi.amap.com/v3/weather/weatherInfo?city=441802&key=yourkey天氣預測(未來三天)
https://restapi.amap.com/v3/weather/weatherInfo?city=441802&key=yourkey&extensions=all參數(shù)說明:
- city 是城市編碼,比如 441802 代表廣州;
- key 是應用對應的代碼,需要在平臺申請(提示:將 yourkey 替換為你申請的 Key 代碼);
- extensions 表示獲取類型,缺省值是 base,表示獲取實況天氣,all 表示獲取預報天氣;
- output 表示返回格式,可選 JSON 或 XML,默認返回 JSON 格式數(shù)據(jù)。
以實時天氣 API 為例,返回的 JSON 數(shù)據(jù)如下:
{"status":"1","count":"1","info":"OK","infocode":"10000","lives":[{"province":"廣東","city":"廣州市","adcode":"440100","weather":"晴","temperature":"17","winddirection":"北","windpower":"≤3","humidity":"64","reporttime":"2021-12-12 19:00:44"}] }HTTP 客戶端
下面代碼使用 HTTPClient 創(chuàng)建 HTTP 連接,通過高德地圖 API 請求實況天氣信息,并輸出到串口。
提示:將下面代碼中的 ssid 和 password 替換成你的 WiFi 網(wǎng)絡;將 URL 中的 cityCode 替換成需要查詢的城市,將 yourKey 替換成你的 Key。
#include <rpcWiFi.h> #include <HTTPClient.h> #include <WiFiClientSecure.h>const char* ssid = "yourNetworkName"; const char* password = "yourNetworkPassword";const char* URL = "https://restapi.amap.com/v3/weather/weatherInfo?city=cityCode&key=yourKey";WiFiClientSecure client;void setup() {Serial.begin(115200);WiFi.begin(ssid, password);while (WiFi.status() != WL_CONNECTED) { //Check for the connectiondelay(500);Serial.println("Connecting WiFi...");}Serial.print("Connected to the WiFi network with IP: ");Serial.println(WiFi.localIP());//client.setCACert(test_root_ca); }void loop() {if(&client) { getWeather();}delay(30000); }void getWeather() {// Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client isHTTPClient https;Serial.print("[HTTPS] begin...\n");if (https.begin(client, URL)){Serial.print("[HTTPS] GET...\n");int httpCode = https.GET();if (httpCode > 0){Serial.printf("[HTTPS] GET... code: %d\n", httpCode);if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {String payload = https.getString();Serial.println(payload);}} else {Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());}https.end();} else {Serial.printf("[HTTPS] Unable to connect\n");}// End extra scoping block }編譯上傳到 Wio Terminal,打開 Arduino IDE 的串口監(jiān)視器,輸出內容如下:
Connected to the WiFi network with IP: 192.168.3.189 [HTTPS] begin... weather lives [HTTPS] GET... [HTTPS] GET... code: 200 {"status":"1","count":"1","info":"OK","infocode":"10000","lives":[{"province":"廣東","city":"清城區(qū)","adcode":"441802","weather":"晴","temperature":"19","winddirection":"東北","windpower":"≤3","humidity":"54","reporttime":"2021-12-12 12:30:59"}]}總結
以上是生活随笔為你收集整理的Wio Terminal 从网络获取天气数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决:Tomcat启动提示At leas
- 下一篇: JAVA程序计算圆的周长面积