获取并解析心知天气数据

杜翰林
2023-12-01
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>                 //v6版本,下载地址:https://github.com/bblanchon/ArduinoJson

const char* ssid     = "Xiaomi_1EDE";       // 连接 WiFi名 ,修改为自己的WiFi名                                      
const char* password = "1234567890";          // 连接 WiFi密码,修改为自己的WiFi密码
                                           
const char* host = "api.seniverse.com";     // 将要连接的服务器地址  
const int httpPort = 80;                    // 将要连接的服务器端口      

 
// 心知天气HTTP请求所需信息
String key = "SeAoWRtUCAR8wPv4A";      // 私钥
String location = "rizhao";            // 城市

void setup() {
  Serial.begin(115200);
  connectWiFi();   // 连接WiFi
}

void loop() {
  // 建立心知天气API当前天气请求资源地址
  String reqRes = "/v3/weather/now.json?key=" + key +
                  + "&location=" + location ;
                             
  // 向心知天气服务器服务器请求信息并对信息进行解析
  httpRequest(reqRes);
  delay(5000);
}

// 向心知天气服务器服务器请求信息并对信息进行解析
void httpRequest(String reqRes){
  WiFiClient client;
 
  // 建立http请求信息
  String httpRequest = String("GET ") + reqRes + " HTTP/1.1\r\n" + 
                              "Host: " + host + "\r\n" + 
                              "Connection: close\r\n\r\n";
  Serial.println(""); 
  Serial.print("Connecting to "); Serial.print(host);
 
  // 尝试连接服务器
  if (client.connect(host, httpPort)){
    Serial.println(" Success!");
 
    // 向服务器发送http请求信息
    client.print(httpRequest);
    Serial.println("Sending request: ");
    Serial.println(httpRequest);  
 
    // 获取并显示服务器响应状态行 
    String status_response = client.readStringUntil('\n');
    Serial.print("status_response: ");
    Serial.println(status_response);
 
    // 使用find跳过HTTP响应头
    if (client.find("\r\n\r\n")) {
      Serial.println("Found Header End. Start Parsing.");
    }  
    // 利用ArduinoJson库解析心知天气响应信息
    parseInfo(client); 
  } else {
    Serial.println(" connection failed!");
  }   
  //断开客户端与服务器连接工作
  client.stop(); 
}

// 利用ArduinoJson库解析心知天气响应信息
void parseInfo(WiFiClient client){
  const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 210;
  DynamicJsonDocument doc(capacity);
  
  deserializeJson(doc, client);

  JsonObject results_0 = doc["results"][0];
  
  JsonObject results_0_now = results_0["now"];
  const char* results_0_now_text = results_0_now["text"]; // "多云"
  const char* results_0_now_code = results_0_now["code"]; // "4"
  const char* results_0_now_temperature = results_0_now["temperature"]; // "23"
  
  const char* results_0_last_update = results_0["last_update"]; // "2020-09-25T09:20:00+08:00" 
 
  Serial.println(F("======Weahter Now======="));
  Serial.print(F("Weather Now: "));
  Serial.print(results_0_now_text);
  Serial.print(F(" "));
  Serial.println(results_0_now_code);
  Serial.print(F("Temperature: "));
  Serial.println(results_0_now_temperature);
  Serial.print(F("Last Update: "));
  Serial.println(results_0_last_update);
  Serial.println(F("========================"));
}

void connectWiFi(){
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.mode(WIFI_STA);                      //设置WiFi为station模式
  WiFi.begin(ssid, password);               //启动连接
 
  while (WiFi.status() != WL_CONNECTED) {   //连接不成功,一直输出".",成功则向下执行
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());            //输出模块的IP地址
  
}

 

 类似资料: