1.Arduino中使用API接口的方式有很多,但最直接的就是基于http的方式了。
*API接口簡單來說就是服務(wù)器用于提供信息的接入點(diǎn),可獲取如JSON和XML等格式的天氣、交通、污染指數(shù)等信息
Arduino中的http函數(shù)也非常好用:
1.首先調(diào)用函數(shù)
#include <ESP8266HTTPClient.h>
2.配置函數(shù)
HTTPClient http;
3.發(fā)送請求
String a=“http://flash.weather.com.cn/wmaps/xml/nanjing.xml”;
http.begin(a);
/*這里我就直接用我制作天氣預(yù)報顯示屏所使用的XML格式的API接口 *P.S 定義的a是一個用于存儲API地址的變量,也可指直接調(diào)用 http.begin(“http://flash.weather.com.cn/wmaps/xml/nanjing.xml”); */P.P.S API接口格式前一定要加{http://},否則無法解析。
4.獲取反饋數(shù)據(jù)
String payload;
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
? ? payload = http.getString();
}
這樣,就完成了全部流程,當(dāng)然,如果你想在你的代碼中多次調(diào)用,也可以將其封裝為一個函數(shù),如下
