feat: 全量同步 254 个常用的 Arduino 扩展库文件

This commit is contained in:
yczpf2019
2026-01-24 16:05:38 +08:00
parent c665ba662b
commit 397b9a23a3
6878 changed files with 2732224 additions and 1 deletions

View File

@@ -0,0 +1,7 @@
#ifndef _ESP8266_SENIVERSE_H_
#define _ESP8266_SENIVERSE_H_
#include "WeatherNow.h"
#include "Forecast.h"
#include "LifeInfo.h"
#endif

View File

@@ -0,0 +1,214 @@
#include "Forecast.h"
Forecast::Forecast(){
}
/* 配置心知天气请求信息
* @param userKey 用户心知天气私钥
* @param location 获取信息的城市参数
* @param unit 获取信息的温度单位(摄氏/华氏)
* @param language 获取信息的语言(简体中文zh-Hans/繁体中文zh-Hant/英文en)
*/
void Forecast::config(String userKey, String location, String unit, String language){
_reqUserKey = userKey;
_reqLocation = location;
_reqUnit = unit;
_reqLanguage = language;
}
/* 尝试从心知天气更新天气预报信息
* @return: bool 成功更新返回真,否则返回假
*/
bool Forecast::update(){
WiFiClient _wifiClient;
String reqRes = "/v3/weather/daily.json?key=" + _reqUserKey +
+ "&location=" + _reqLocation + "&language=" + _reqLanguage + "&unit=" +
_reqUnit + "&start=0&days=3";
String httpRequest = String("GET ") + reqRes + " HTTP/1.1\r\n" +
"Host: " + _host + "\r\n" +
"Connection: close\r\n\r\n";
#ifdef DEBUG
Serial.print("Connecting to ");Serial.print(_host);
#endif DEBUG
if (_wifiClient.connect(_host, 80)){
#ifdef DEBUG
Serial.println(" Success!");
#endif DEBUG
// 向服务器发送http请求信息
_wifiClient.print(httpRequest);
#ifdef DEBUG
Serial.println("Sending request: ");
Serial.println(httpRequest);
#endif DEBUG
// 获取并显示服务器响应状态行
String _status_response = _wifiClient.readStringUntil('\n');
#ifdef DEBUG
Serial.print("_status_response: ");
Serial.println(_status_response);
#endif DEBUG
// 查验服务器是否响应200 OK
_response_code = _status_response.substring(9, 12);
if (_response_code == "200") {
#ifdef DEBUG
Serial.println("Response Code: 200");
#endif DEBUG
} else {
#ifdef DEBUG
Serial.println(F("Response Code: NOT 200"));
#endif DEBUG
_wifiClient.stop();
return false;
}
// 使用find跳过HTTP响应头
if (_wifiClient.find("\r\n\r\n")) {
#ifdef DEBUG
Serial.println("Found Header End. Start Parsing.");
#endif DEBUG
}
// 解析服务器响应信息
_parseForecastInfo(_wifiClient);
_wifiClient.stop();
return true;
} else {
#ifdef DEBUG
Serial.println(" connection failed!");
#endif DEBUG
_wifiClient.stop();
return false;
}
}
// 解析服务器响应信息
void Forecast::_parseForecastInfo(WiFiClient httpClient){
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 3*JSON_OBJECT_SIZE(14) + 860;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, httpClient);
JsonObject results_0 = doc["results"][0];
JsonArray results_0_daily = results_0["daily"];
JsonObject results_0_daily_0 = results_0_daily[0];
JsonObject results_0_daily_1 = results_0_daily[1];
JsonObject results_0_daily_2 = results_0_daily[2];
// 从以上信息中解析数值
_text_day[0] = results_0_daily_0["text_day"].as<String>();
_code_day[0] = results_0_daily_0["code_day"].as<int>();
_text_night[0] = results_0_daily_0["text_night"].as<String>();
_code_night[0] = results_0_daily_0["code_night"].as<int>();
_degree_high[0] = results_0_daily_0["high"].as<int>();
_degree_low[0] = results_0_daily_0["low"].as<int>();
_rainfall[0] = results_0_daily_0["rainfall"].as<float>();
_wind_direction[0] = results_0_daily_0["wind_direction"].as<String>();
_wind_speed[0] = results_0_daily_0["wind_speed"].as<float>();
_wind_scale[0] = results_0_daily_0["wind_scale"].as<int>();
_humidity[0] = results_0_daily_0["humidity"].as<int>();
_text_day[1] = results_0_daily_1["text_day"].as<String>();
_code_day[1] = results_0_daily_1["code_day"].as<int>();
_text_night[1] = results_0_daily_1["text_night"].as<String>();
_code_night[1] = results_0_daily_1["code_night"].as<int>();
_degree_high[1] = results_0_daily_1["high"].as<int>();
_degree_low[1] = results_0_daily_1["low"].as<int>();
_rainfall[1] = results_0_daily_1["rainfall"].as<float>();
_wind_direction[1] = results_0_daily_1["wind_direction"].as<String>();
_wind_speed[1] = results_0_daily_1["wind_speed"].as<float>();
_wind_scale[1] = results_0_daily_1["wind_scale"].as<int>();
_humidity[1] = results_0_daily_1["humidity"].as<int>();
_text_day[2] = results_0_daily_2["text_day"].as<String>();
_code_day[2] = results_0_daily_2["code_day"].as<int>();
_text_night[2] = results_0_daily_2["text_night"].as<String>();
_code_night[2] = results_0_daily_2["code_night"].as<int>();
_degree_high[2] = results_0_daily_2["high"].as<int>();
_degree_low[2] = results_0_daily_2["low"].as<int>();
_rainfall[2] = results_0_daily_2["rainfall"].as<float>();
_wind_direction[2] = results_0_daily_2["wind_direction"].as<String>();
_wind_speed[2] = results_0_daily_2["wind_speed"].as<float>();
_wind_scale[2] = results_0_daily_2["wind_scale"].as<int>();
_humidity[2] = results_0_daily_2["humidity"].as<int>();
_last_update = results_0["last_update"].as<String>();
}
// 返回白天天气(字符串格式)
String Forecast::getDayText(int index){
return _text_day[index];
}
// 返回白天天气(整数格式)
int Forecast::getDayCode(int index){
return _code_day[index];
}
// 返回夜晚天气(字符串格式)
String Forecast::getNightText(int index){
return _text_night[index];
}
// 返回夜晚天气(整数格式)
int Forecast::getNightCode(int index){
return _code_night[index];
}
// 返回最高气温
int Forecast::getHigh(int index){
return _degree_high[index];
}
// 返回最低气温
int Forecast::getLow(int index){
return _degree_low[index];
}
// 返回降水概率
float Forecast::getRain(int index){
return _rainfall[index];
}
// 获取风向信息
String Forecast::getWindDirection(int index){
return _wind_direction[index];
}
// 获取风速信息
float Forecast::getWindSpeed(int index){
return _wind_speed[index];
}
// 获取风力信息
int Forecast::getWindScale(int index){
return _wind_scale[index];
}
// 获取湿度信息
int Forecast::getHumidity(int index){
return _humidity[index];
}
// 返回心知天气信息更新时间
String Forecast::getLastUpdate(){
return _last_update;
}
// 返回服务器响应状态码
String Forecast::getServerCode(){
return _response_code;
}

View File

@@ -0,0 +1,64 @@
#ifndef _FORECAST_H_
#define _FORECAST_H_
#include <Arduino.h>
#include <ArduinoJson.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
// #define DEBUG // 调试用宏定义
// 获取天气预报信息类
class Forecast {
public:
Forecast();
void config(String userKey, String location, String unit, String language);
bool update();
String getDayText(int index);
int getDayCode(int index);
String getNightText(int index);
int getNightCode(int index);
int getHigh(int index);
int getLow(int index);
float getRain(int index); // 获取降水概率信息
String getWindDirection(int index);// 获取风向信息
float getWindSpeed(int index); // 获取风速信息
int getWindScale(int index); // 获取风力信息
int getHumidity(int index); // 获取湿度信息
String getLastUpdate(); // 获取心知天气信息更新时间
String getServerCode(); // 获取服务器响应状态码
private:
const char* _host = "api.seniverse.com"; // 服务器地址
String _reqUserKey; // 私钥
String _reqLocation; // 城市
String _reqUnit; // 摄氏/华氏
String _reqLanguage; // 简体中文zh-Hans/繁体中文zh-Hant/英文en
String _response_code = "no_init"; // 服务器响应状态码
void _parseForecastInfo(WiFiClient client); // 解析实时天气信息信息
String _text_day[3] = {"no_init", "no_init", "no_init"}; // 白天天气(字符串)
int _code_day[3] = {999, 999, 999}; // 白天天气(代码)
String _text_night[3] = {"no_init", "no_init", "no_init"}; // 晚上天气(字符串)
int _code_night[3] = {999, 999, 999}; // 晚上天气(代码)
int _degree_high[3] = {999, 999, 999}; // 最高气温
int _degree_low[3] = {999, 999, 999}; // 最低气温
float _rainfall[3] = {999, 999, 999}; // 降水概率
String _wind_direction[3] = {"no_init", "no_init", "no_init"}; // 风向
float _wind_speed[3] = {999, 999, 999}; // 风速
int _wind_scale[3] = {999, 999, 999}; // 风力
int _humidity[3] = {999, 999, 999}; // 湿度
String _last_update; // 心知天气信息更新时间
};
#endif

View File

@@ -0,0 +1,145 @@
#include "LifeInfo.h"
LifeInfo::LifeInfo(){
}
/* 配置心知天气请求信息
* @param userKey 用户心知天气私钥
* @param location 获取信息的城市参数
* @param location 获取信息的温度单位(摄氏/华氏)
*/
void LifeInfo::config(String userKey, String location, String unit, String language){
_reqUserKey = userKey;
_reqLocation = location;
_reqLanguage = language;
}
/* 尝试从心知天气更新信息
* @return bool 成功更新返回真,否则返回假
*/
bool LifeInfo::update(){
WiFiClient _wifiClient;
String reqRes = "/v3/life/suggestion.json?key=" + _reqUserKey +
+ "&location=" + _reqLocation +
"&language="+_reqLanguage;
String httpRequest = String("GET ") + reqRes + " HTTP/1.1\r\n" +
"Host: " + _host + "\r\n" +
"Connection: close\r\n\r\n";
#ifdef DEBUG
Serial.print("Connecting to ");Serial.print(_host);
#endif DEBUG
if (_wifiClient.connect(_host, 80)){
#ifdef DEBUG
Serial.println(" Success!");
#endif DEBUG
// 向服务器发送http请求信息
_wifiClient.print(httpRequest);
#ifdef DEBUG
Serial.println("Sending request: ");
Serial.println(httpRequest);
#endif DEBUG
// 获取并显示服务器响应状态行
String _status_response = _wifiClient.readStringUntil('\n');
#ifdef DEBUG
Serial.print("_status_response: ");
Serial.println(_status_response);
#endif DEBUG
// 查验服务器是否响应200 OK
_response_code = _status_response.substring(9, 12);
if (_response_code == "200") {
#ifdef DEBUG
Serial.println("Response Code: 200");
#endif DEBUG
} else {
#ifdef DEBUG
Serial.println(F("Response Code: NOT 200"));
#endif DEBUG
_wifiClient.stop();
return false;
}
// 使用find跳过HTTP响应头
if (_wifiClient.find("\r\n\r\n")) {
#ifdef DEBUG
Serial.println("Found Header End. Start Parsing.");
#endif DEBUG
}
_parseLifeInfo(_wifiClient);
_wifiClient.stop();
return true;
} else {
#ifdef DEBUG
Serial.println(" connection failed!");
#endif DEBUG
_wifiClient.stop();
return false;
}
}
// 利用ArduinoJson库解析心知天气响应信息
void LifeInfo::_parseLifeInfo(WiFiClient client){
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 6*JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 2*JSON_OBJECT_SIZE(6) + 400;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, client);
JsonObject results_0 = doc["results"][0];
JsonObject results_0_suggestion = results_0["suggestion"];
_carWash = results_0_suggestion["car_washing"]["brief"].as<String>();
_dressing = results_0_suggestion["dressing"]["brief"].as<String>();
_factorFlu = results_0_suggestion["flu"]["brief"].as<String>();
_exercise = results_0_suggestion["sport"]["brief"].as<String>();
_travel = results_0_suggestion["travel"]["brief"].as<String>();
_uv = results_0_suggestion["uv"]["brief"].as<String>();
_last_update_str = results_0["last_update"].as<String>();
}
// 获取洗车建议
String LifeInfo::getCarWash(){
return _carWash;
}
// 获取穿衣建议
String LifeInfo::getDressing(){
return _dressing;
}
// 获取流感建议
String LifeInfo::getFactorFlu(){
return _factorFlu;
}
// 获取运动建议
String LifeInfo::getExercise(){
return _exercise;
}
// 获取旅游建议
String LifeInfo::getTravel(){
return _travel;
}
// 获取紫外线建议
String LifeInfo::getUV(){
return _uv;
}
// 返回心知天气信息更新时间
String LifeInfo::getLastUpdate(){
return _last_update_str;
}
// 返回服务器响应状态码
String LifeInfo::getServerCode(){
return _response_code;
}

View File

@@ -0,0 +1,53 @@
#ifndef _LIFE_INFO_H_
#define _LIFE_INFO_H_
#include <Arduino.h>
#include <ArduinoJson.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
//#define DEBUG // 调试用宏定义
// 获取当前天气信息类
class LifeInfo {
public:
LifeInfo();
void config(String userKey, String location, String unit, String language);
bool update();
String getCarWash(); // 获取洗车建议
String getDressing(); // 获取穿衣建议
String getFactorFlu(); // 获取流感建议
String getExercise(); // 获取运动建议
String getTravel(); // 获取旅游建议
String getUV(); // 获取紫外线建议
String getLastUpdate(); // 返回心知天气信息更新时间
String getServerCode(); // 返回服务器响应状态码
private:
const char* _host = "api.seniverse.com"; // 服务器地址
String _status_response = "no_init"; // 服务器响应状态行
String _response_code = "no_init"; // 服务器响应状态码
void _parseLifeInfo(WiFiClient client);
String _reqUserKey; // 私钥
String _reqLocation; // 城市
String _reqUnit; // 摄氏/华氏
String _reqLanguage; // 简体中文zh-Hans/繁体中文zh-Hant/英文en
String _carWash = "no_init"; // 洗车建议
String _dressing = "no_init"; // 穿衣建议
String _factorFlu = "no_init"; // 流感建议
String _exercise = "no_init"; // 运动建议
String _travel = "no_init"; // 旅游建议
String _uv = "no_init"; // 紫外线建议
String _last_update_str; // 心知天气信息更新时间
};
#endif

View File

@@ -0,0 +1,137 @@
#include "WeatherNow.h"
WeatherNow::WeatherNow(){
}
/* 配置心知天气请求信息
* @param userKey 用户心知天气私钥
* @param location 获取信息的城市参数
* @param location 获取信息的温度单位(摄氏/华氏)
*/
void WeatherNow::config(String userKey, String location, String unit, String language){
_reqUserKey = userKey;
_reqLocation = location;
_reqUnit = unit;
_reqLanguage = language;
}
/* 尝试从心知天气更新信息
* @return: bool 成功更新返回真,否则返回假
*/
bool WeatherNow::update(){
WiFiClient _wifiClient;
String reqRes = "/v3/weather/now.json?key=" + _reqUserKey +
+ "&location=" + _reqLocation +
"&language="+_reqLanguage+"&unit=" +_reqUnit;
String httpRequest = String("GET ") + reqRes + " HTTP/1.1\r\n" +
"Host: " + _host + "\r\n" +
"Connection: close\r\n\r\n";
#ifdef DEBUG
Serial.print("Connecting to ");Serial.print(_host);
#endif DEBUG
if (_wifiClient.connect(_host, 80)){
#ifdef DEBUG
Serial.println(" Success!");
#endif DEBUG
// 向服务器发送http请求信息
_wifiClient.print(httpRequest);
#ifdef DEBUG
Serial.println("Sending request: ");
Serial.println(httpRequest);
#endif DEBUG
// 获取并显示服务器响应状态行
String _status_response = _wifiClient.readStringUntil('\n');
#ifdef DEBUG
Serial.print("_status_response: ");
Serial.println(_status_response);
#endif DEBUG
// 查验服务器是否响应200 OK
_response_code = _status_response.substring(9, 12);
if (_response_code == "200") {
#ifdef DEBUG
Serial.println("Response Code: 200");
#endif DEBUG
} else {
#ifdef DEBUG
Serial.println(F("Response Code: NOT 200"));
#endif DEBUG
_wifiClient.stop();
return false;
}
// 使用find跳过HTTP响应头
if (_wifiClient.find("\r\n\r\n")) {
#ifdef DEBUG
Serial.println("Found Header End. Start Parsing.");
#endif DEBUG
}
_parseNowInfo(_wifiClient);
_wifiClient.stop();
return true;
} else {
#ifdef DEBUG
Serial.println(" connection failed!");
#endif DEBUG
_wifiClient.stop();
return false;
}
}
// 配置心知天气请求信息
void WeatherNow::_parseNowInfo(WiFiClient httpClient){
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 230;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, httpClient);
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"];
const char* results_0_now_temperature = results_0_now["temperature"];
const char* results_0_last_update = results_0["last_update"];
// 通过串口监视器显示以上信息
_now_text_str = results_0_now["text"].as<String>();
_now_code_int = results_0_now["code"].as<int>();
_now_temperature_int = results_0_now["temperature"].as<int>();
_last_update_str = results_0["last_update"].as<String>();
}
// 返回当前天气信息(字符串格式)
String WeatherNow::getWeatherText(){
return _now_text_str;
}
// 返回当前天气信息(整数格式)
int WeatherNow::getWeatherCode(){
return _now_code_int;
}
// 返回当前气温
int WeatherNow::getDegree(){
return _now_temperature_int;
}
// 返回心知天气信息更新时间
String WeatherNow::getLastUpdate(){
return _last_update_str;
}
// 返回服务器响应状态码
String WeatherNow::getServerCode(){
return _response_code;
}

View File

@@ -0,0 +1,47 @@
#ifndef _WEATHERNOW_H_
#define _WEATHERNOW_H_
#include <Arduino.h>
#include <ArduinoJson.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
//#define DEBUG // 调试用宏定义
// 获取当前天气信息类
class WeatherNow {
public:
WeatherNow();
void config(String userKey, String location, String unit, String language);
bool update();
String getWeatherText();
int getWeatherCode();
int getDegree();
String getLastUpdate();
String getServerCode();
private:
const char* _host = "api.seniverse.com"; // 服务器地址
String _reqUserKey; // 私钥
String _reqLocation; // 城市
String _reqUnit; // 摄氏/华氏
String _reqLanguage; // 简体中文zh-Hans/繁体中文zh-Hant/英文en
void _parseNowInfo(WiFiClient client); // 解析实时天气信息信息
String _status_response = "no_init"; // 服务器响应状态行
String _response_code = "no_init"; // 服务器响应状态码
String _now_text_str = "no_init";
int _now_code_int = 999;
int _now_temperature_int = 999;
String _last_update_str = "no_init";
};
#endif