初始化提交

This commit is contained in:
王立帮
2024-07-20 22:09:06 +08:00
commit c247dd07a6
6876 changed files with 2743096 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
#include "Weather_Forcast.h"
#include <WiFiClient.h>
boolean Weather_Forcast::RefreshData(String CityCode){
boolean result;
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
WiFiClient client;
http.begin(client, "\u0068\u0074\u0074\u0070\u003A\u002F\u002F\u0074\u002E\u0077\u0065\u0061\u0074\u0068\u0065\u0072\u002E\u0069\u0074\u0062\u006F\u0079\u002E\u006E\u0065\u0074\u002F\u0061\u0070\u0069\u002F\u0077\u0065\u0061\u0074\u0068\u0065\u0072\u002F\u0063\u0069\u0074\u0079\u002F"+CityCode);
int httpCode = http.GET();
if (httpCode > 0) {
String Request_result = http.getString();
// Serial.println(Request_result);
//Serial.println("-------------------");
result = ParseJson(Request_result);
}
else {
Serial.println("Invalid response!");
result = false;
}
http.end();
}else{
result = false;
}
return result;
}
boolean Weather_Forcast::ParseJson(String json) {
StaticJsonDocument<6144> doc;
DeserializationError error = deserializeJson(doc, json);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return false;
}
JsonObject data = doc["data"];
const char* data_shidu = data["shidu"]; // "61%"
int data_pm25 = data["pm25"]; // 31
int data_pm10 = data["pm10"]; // 66
const char* data_quality = data["quality"]; // "良"
const char* data_wendu = data["wendu"]; // "31"
Today[0] = data_shidu;
Today[1] = String(data_pm25);
Today[2] = String(data_pm10);
Today[3] = data_quality;
Today[4] = data_wendu;
int i=0;
for (JsonObject elem : data["forecast"].as<JsonArray>()) {
const char* date = elem["date"]; // "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", ...
const char* high = elem["high"]; // "高温 31℃", "高温 31℃", "高温 31℃", "高温 30℃", "高温 30℃", "高温 29℃", "高温 ...
const char* low = elem["low"]; // "低温 23℃", "低温 25℃", "低温 26℃", "低温 25℃", "低温 25℃", "低温 26℃", "低温 27℃", ...
const char* ymd = elem["ymd"]; // "2021-05-06", "2021-05-07", "2021-05-08", "2021-05-09", "2021-05-10", ...
const char* week = elem["week"]; // "星期四", "星期五", "星期六", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", ...
// const char* sunrise = elem["sunrise"]; // "05:48", "05:48", "05:47", "05:46", "05:46", "05:45", "05:45", ...
// const char* sunset = elem["sunset"]; // "18:53", "18:53", "18:54", "18:54", "18:54", "18:55", "18:55", ...
int aqi = elem["aqi"]; // 29, 38, 40, 37, 42, 17, 16, 18, 27, 35, 53, 34, 26, 31, 37
const char* fx = elem["fx"]; // "东南风", "南风", "南风", "南风", "南风", "南风", "南风", "南风", "东南风", "南风", "东南风", ...
const char* fl = elem["fl"]; // "3级", "2级", "2级", "2级", "3级", "3级", "2级", "2级", "3级", "2级", "2级", "2级", ...
const char* type = elem["type"]; // "晴", "晴", "多云", "多云", "多云", "阵雨", "阵雨", "多云", "多云", "晴", "阴", "多云", ...
// const char* notice = elem["notice"]; // "愿你拥有比阳光明媚的心情", "愿你拥有比阳光明媚的心情", "阴晴之间,谨防紫外线侵扰", "阴晴之间,谨防紫外线侵扰", ...
ForecastDate[i] = date;
ForecastHigh[i] = high;
ForecastLow[i] = low;
ForecastYmd[i] = ymd;
ForecastWeek[i] = week;
ForecastAqi[i] = String(aqi);
ForecastFx[i] = fx;
ForecastFl[i] = fl;
ForecastType[i] = type;
i++;
}
return true;
}

View File

@@ -0,0 +1,36 @@
#ifndef Weather_Forcast_H
#define Weather_Forcast_H
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <HTTPClient.h>
#endif
#include <ArduinoJson.h>
#include <Arduino.h>
class Weather_Forcast
{
public:
String getToday(uint8_t i){return Today[i];};
String getForecastDate(uint8_t i){return ForecastDate[i];};
String getForecastHigh(uint8_t i){return ForecastHigh[i];};
String getForecastLow(uint8_t i){return ForecastLow[i];};
String getForecastYmd(uint8_t i){return ForecastYmd[i];};
String getForecastWeek(uint8_t i){return ForecastWeek[i];};
String getForecastAqi(uint8_t i){return ForecastAqi[i];};
String getForecastFx(uint8_t i){return ForecastFx[i];};
String getForecastFl(uint8_t i){return ForecastFl[i];};
String getForecastType(uint8_t i){return ForecastType[i];};
boolean RefreshData(String CityCode);
boolean ParseJson(String json);
private:
String Today[5],ForecastDate[15],ForecastHigh[15],ForecastLow[15],ForecastYmd[15],ForecastWeek[15],ForecastAqi[15],ForecastFx[15],ForecastFl[15],ForecastType[15];
};
#endif