ESP8266获取网络天气数据
ESP8266获取网络天气数据快速实现:
安装库:
arduino
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
核心代码:
arduino
void getWeather() {
WiFiClient client;
if(client.connect("api.seniverse.com", 80)) {
client.print("GET /v3/weather/now.json?key=YOUR_KEY&location=beijing");
while(client.connected()) {
String line = client.readStringUntil('\n');
if(line.startsWith("\"temperature\":")) {
Serial.println("当前温度:" + line.substring(14));
}
}
}
}
调试技巧:
使用串口监视器查看原始JSON数据
用Postman先测试API接口
页:
[1]