初始化提交
This commit is contained in:
40
arduino-cli/libraries/ESP8266-Seniverse/.gitignore
vendored
Normal file
40
arduino-cli/libraries/ESP8266-Seniverse/.gitignore
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
info.h
|
||||
|
||||
# Private
|
||||
info.h
|
||||
|
||||
# word temp
|
||||
~$*.*
|
||||
|
||||
21
arduino-cli/libraries/ESP8266-Seniverse/LICENSE
Normal file
21
arduino-cli/libraries/ESP8266-Seniverse/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 太极创客
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
202
arduino-cli/libraries/ESP8266-Seniverse/README.md
Normal file
202
arduino-cli/libraries/ESP8266-Seniverse/README.md
Normal file
@@ -0,0 +1,202 @@
|
||||
# ESP8266 心知天气库 / ESP8266-Seniverse
|
||||
|
||||
*English description can be found at the end of Chinese description.*
|
||||
|
||||
## 基本介绍
|
||||
|
||||
|
||||
此库用于ESP8266物联网开发板通过HTTP协议获取心知天气网站API所提供的免费信息。这些信息包括:
|
||||
|
||||
1. 天气预报信息(温度,天气,降水概率,风力,风向,湿度)
|
||||
2. 实时天气信息(温度,天气)
|
||||
3. 获取实时生活指数(穿衣,紫外线强度,洗车,旅游,感冒,运动)
|
||||
|
||||
关于以上信息的详细说明,请参考心知天气网站官方文档:https://www.seniverse.com/docs
|
||||
|
||||
心知天气成立于2016年,是中国领先的气象数据服务公司,致力于提供高精度的气象数据服务和产品。心知天气官网地址:www.seniverse.com
|
||||
|
||||
## 关于本库
|
||||
|
||||
本库为太极创客团队制作的免费视频教程《零基础入门学用物联网 》中一部分。该教程系统的向您讲述ESP8266的物联网应用相关的软件和硬件知识。如果您希望观看教程视频,可前往以下视频平台观看。
|
||||
|
||||
哔哩哔哩:https://www.bilibili.com/video/BV1L7411c7jw
|
||||
|
||||
YouTube: https://www.youtube.com/playlist?list=PL8mx3Pk-gVLI2GwuxuqR_T5WDKeAPRkzj
|
||||
|
||||
ESP8266-Seniverse库仅仅是我们团队所开发的诸多免费开源项目中的一个。我们坚持免费开源是为了让更多的朋友可以体会开源项目和开源协作的魅力,让我们开发的项目更富活力。假如您喜欢我们的项目,请为本项目打上一颗小星星,或者把我们推荐给更多热爱科技的朋友们。谢谢!您的鼓励是我们前进最大的动力!
|
||||
|
||||
## 使用前准备工作
|
||||
|
||||
1. 使用本库前请预先注册好心知天气账号并且开通免费服务。
|
||||
2. 本程序使用Arduino编程语言。如您使用Arduino IDE开发,请预先在Arduino IDE中安装好ESP8266扩展程序,如需了解详细安装方法,请参考太极创客团队制作的[《零基础入门学用物联网 - 基础知识篇》3-1-2 为ESP8266-NodeMCU搭建Arduino IDE开发环境](http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/iot-c/nodemcu-arduino-ide/)。
|
||||
3. 本程序使用ArduinoJson库
|
||||
请预先在Arduino IDE中安装[ArduinoJson库](www.arduinojson.org)。 如果您想了解该库的具体使用方法,请参考太极创客团队制作的免费视频教程《[零基础入门学用物联网](http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/)》
|
||||
|
||||
## 使用方法
|
||||
|
||||
### 获取当前天气信息
|
||||
|
||||
1. 您可以参考 **example** 目录中的 **weather_now** 程序了解具体使用方法
|
||||
|
||||
2. 首先通过`WeatherNow`建立对象
|
||||
```C++
|
||||
WeatherNow weatherNow
|
||||
```
|
||||
|
||||
3. 使用`config`函数配置连接心知天气的用户私钥、城市信息以及温度
|
||||
```C++
|
||||
weatherNow.config(reqUserKey, reqLocation, reqUnit);
|
||||
```
|
||||
|
||||
4. 使用`update`函数对天气信息进行更新(此函数返回值为`bool`型。在ESP8266成功连接心知天气服务器并且服务器返回响应状态码为`200`时,此函数将会返回真,否则会返回假。)
|
||||
```C++
|
||||
weatherNow.update();
|
||||
```
|
||||
|
||||
5. 使用下列函数获取当前天气信息
|
||||
|
||||
| 函数说明 | 函数示例 |
|
||||
| --------------------------------- | ----------------------------- |
|
||||
| 当前天气信息 (返回值类型:String) | `weatherNow.getWeatherText()` |
|
||||
| 当前天气代码 (返回值类型:int) | `weatherNow.getWeatherCode()` |
|
||||
| 当前温度信息 (返回值类型:int) | `weatherNow.getDegree()` |
|
||||
|
||||
6. 使用`getServerCode`函数可获取服务器响应状态码。
|
||||
当用户通过ESP8266向心知天气服务器发送的请求存在问题时(如私钥错误、请求信息内容错误等),服务器会通过响应状态码告知用户问题的具体原因。您可以通过心知天气的官方文档获得服务器响应状态码所对应的具体问题原因。
|
||||
```C++
|
||||
weatherNow.getServerCode()
|
||||
```
|
||||
|
||||
7. 使用`getLastUpdate`函数获取心知天气信息更新时间
|
||||
```C++
|
||||
weatherNow.getLastUpdate()
|
||||
```
|
||||
|
||||
### 获取天气预报信息
|
||||
|
||||
1. 您可以参考**example**目录中的**forecast**程序了解具体使用方法
|
||||
|
||||
2. 首先通过`Forecast`建立对象
|
||||
```C++
|
||||
Forecast forecast
|
||||
```
|
||||
|
||||
3. 使用`config`函数配置连接心知天气的用户私钥、城市信息以及温度
|
||||
```C++
|
||||
forecast.config(reqUserKey, reqLocation, reqUnit);
|
||||
```
|
||||
|
||||
4. 使用`update`函数对天气信息进行更新(此函数返回值为`bool`型。在ESP8266成功连接心知天气服务器并且服务器返回响应状态码为`200`时,此函数将会返回真,否则会返回假。)
|
||||
```C++
|
||||
forecast.update();
|
||||
```
|
||||
|
||||
5. 使用下列函数获取当前天气信息 (下列函数的参数i为获取第几天信息的天数序号。例:参数为`0`,则函数将会返回今天的天气预报信息。参数为`1`,则返回明天的天气预报...依此类推。参数i取值范围为`0-2`。)
|
||||
|
||||
| 函数说明 | 函数示例(参数i为第几天信息) |
|
||||
| ---------------------------------------- | ------------------------------ |
|
||||
| 白天天气信息(返回值类型:String) | `forecast.getDayText(i)` |
|
||||
| 白天天气代码(返回值类型:int) | `forecast.getDayCode(i)` |
|
||||
| 夜晚天气信息(返回值类型:String) | `forecast.getNightText(i)` |
|
||||
| 夜晚天气代码(返回值类型:int) | `forecast.getNightCode(i)` |
|
||||
| 最高气温(返回值类型:int) | `forecast.getHigh(i)` |
|
||||
| 最低气温(返回值类型:int) | `forecast.getLow(i)` |
|
||||
| 心知天气信息更新时间(返回值类型:String) | `forecast.getLastUpdate(i)` |
|
||||
| 获取降水概率信息(返回值类型:float) | `forecast.getRain(i)` |
|
||||
| 获取风向信息(返回值类型:String) | `forecast.getWindDirection(i)` |
|
||||
| 获取风速信息(返回值类型:float) | `forecast.getWindSpeed(i)` |
|
||||
| 获取风力信息(返回值类型:int) | `forecast.getWindScale(i)` |
|
||||
| 获取湿度信息(返回值类型:int) | `forecast.getHumidity(i)` |
|
||||
|
||||
6. 使用`getServerCode`函数可获取服务器响应状态码。
|
||||
当用户通过ESP8266向心知天气服务器发送的请求存在问题时(如私钥错误、请求信息内容错误等),服务器会通过响应状态码告知用户问题的具体原因。您可以通过心知天气的官方文档获得服务器响应状态码所对应的具体问题原因。
|
||||
```C++
|
||||
forecast.getServerCode()
|
||||
```
|
||||
|
||||
7. 使用`getLastUpdate`函数获取心知天气信息更新时间
|
||||
```C++
|
||||
forecast.getLastUpdate()
|
||||
```
|
||||
|
||||
### 获取生活指数信息
|
||||
|
||||
1. 您可以参考 **example** 目录中的 **life_info** 程序了解具体使用方法
|
||||
|
||||
2. 首先通过`LIfeInfo`建立对象
|
||||
```C++
|
||||
LifeInfo lifeInfo
|
||||
```
|
||||
|
||||
3. 使用`config`函数配置连接心知天气的用户私钥、城市信息以及温度
|
||||
```C++
|
||||
lifeInfo.config(reqUserKey, reqLocation, reqUnit);
|
||||
```
|
||||
|
||||
4. 使用`update`函数对天气信息进行更新(此函数返回值为`bool`型。在ESP8266成功连接心知天气服务器并且服务器返回响应状态码为`200`时,此函数将会返回真,否则会返回假。)
|
||||
```C++
|
||||
lifeInfo.update();
|
||||
```
|
||||
|
||||
5. 使用下列函数获取当前天气信息
|
||||
|
||||
| 函数说明 | 函数示例 |
|
||||
| ---------------------------------- | ------------------------- |
|
||||
| 获取洗车建议(返回值类型:String) | `lifeInfo.getCarWash()` |
|
||||
| 获取穿衣建议(返回值类型:String) | `lifeInfo.getDressing()` |
|
||||
| 获取流感建议(返回值类型:String) | `lifeInfo.getFactorFlu()` |
|
||||
| 获取运动建议(返回值类型:String) | `lifeInfo.getExercise()` |
|
||||
| 获取旅游建议(返回值类型:String) | `lifeInfo.getTravel()` |
|
||||
| 获取紫外线建议(返回值类型:String) | `lifeInfo.getUV()` |
|
||||
|
||||
6. 使用`getServerCode`函数可获取服务器响应状态码。
|
||||
当用户通过ESP8266向心知天气服务器发送的请求存在问题时(如私钥错误、请求信息内容错误等),服务器会通过响应状态码告知用户问题的具体原因。您可以通过心知天气的官方文档获得服务器响应状态码所对应的具体问题原因。
|
||||
```C++
|
||||
lifeInfo.getServerCode()
|
||||
```
|
||||
7. 使用`getLastUpdate`函数获取心知天气信息更新时间
|
||||
```C++
|
||||
lifeInfo.getLastUpdate()
|
||||
```
|
||||
|
||||
太极创客团队信息
|
||||
--------
|
||||
太极创客官网地址:http://www.taichi-maker.com/
|
||||
|
||||
太极创客哔哩哔哩主页:https://space.bilibili.com/103589285
|
||||
|
||||
太极创客YouTube:https://www.youtube.com/channel/UC8EkxMr5gGnrb9adVgR-UJw
|
||||
|
||||
太极创客GitHub:https://github.com/taichi-maker
|
||||
|
||||
太极创客码云:https://gitee.com/taijichuangke
|
||||
|
||||
-----------------------------
|
||||
|
||||
## ESP8266-Seniverse
|
||||
|
||||
This ESP8266-Arduino Library is for getting weather information from Seniverse API (Free) via HTTP protocol. The information includes:
|
||||
|
||||
1. Weather Forecast(Temperature,Weather,Precipitation probability,Wind Scale,Wind Direction,Humidity)
|
||||
2. Live Weather Info.(Temperature,Weather)
|
||||
3. Daily Life Information(Dressing Suggestion,UV level etc.)
|
||||
|
||||
For more infromation about the above information please refer to Seniverse API Doc (Chinese Only):https://www.seniverse.com/docs
|
||||
|
||||
founded in 2016, Seniverse is a leading Weather Data provider in China. Seniverse Official Website:www.seniverse.com
|
||||
|
||||
About Taichi-Maker Team
|
||||
--------
|
||||
|
||||
Taichi-Maker Official Website:http://www.taichi-maker.com/
|
||||
|
||||
Taichi-Maker BiliBili:https://space.bilibili.com/103589285
|
||||
|
||||
Taichi-Maker YouTube Chanel:https://www.youtube.com/channel/UC8EkxMr5gGnrb9adVgR-UJw
|
||||
|
||||
Taichi-Maker GitHub:https://github.com/taichi-maker
|
||||
|
||||
Taichi-Maker Gitee:https://gitee.com/taijichuangke
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
/**********************************************************************
|
||||
项目名称/Project : 零基础入门学用物联网
|
||||
程序名称/Program name : forecast(rev.001)
|
||||
团队/Team : 太极创客团队 / Taichi-Maker (www.taichi-maker.com)
|
||||
作者/Author : CYNO朔
|
||||
日期/Date(YYYYMMDD) : 初版建立时间20200603
|
||||
程序目的/Purpose :
|
||||
使用ESP8266_Seniverse库通过心知天气服务器获取天气预报信息
|
||||
-----------------------------------------------------------------------
|
||||
本程序所属仓库网址
|
||||
GitHub: https://github.com/taichi-maker/ESP8266-Seniverse
|
||||
Gitee: https://gitee.com/taijichuangke/ESP8266-Seniverse
|
||||
-----------------------------------------------------------------------
|
||||
其它说明 / Other Description
|
||||
心知天气API文档说明: https://www.seniverse.com/docs
|
||||
|
||||
本程序为太极创客团队制作的免费视频教程《零基础入门学用物联网 》中一部分。该教程系统的
|
||||
向您讲述ESP8266的物联网应用相关的软件和硬件知识。以下是该教程目录页:
|
||||
http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/
|
||||
***********************************************************************/
|
||||
#include <ArduinoJson.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266_Seniverse.h>
|
||||
|
||||
const char* ssid = "taichimaker"; // 连接WiFi名(此处使用taichi-maker为示例)
|
||||
// 请将您需要连接的WiFi名填入引号中
|
||||
const char* password = "12345678"; // 连接WiFi密码(此处使用12345678为示例)
|
||||
// 请将您需要连接的WiFi密码填入引号中
|
||||
|
||||
// 心知天气API请求所需信息
|
||||
// 请对以下信息进行修改,填入您的心知天气私钥以及需要获取天气信息的城市和温度单位
|
||||
// 如需进一步了解心知天气API所提供的城市列表等信息,请前往心知天气官方产品文档网址:
|
||||
// https://www.seniverse.com/docs
|
||||
String reqUserKey = "XXXXXXXXXXXXXXXXX"; // 私钥
|
||||
String reqLocation = "beijing"; // 城市
|
||||
String reqUnit = "c"; // 摄氏/华氏// 请将您需要连接的WiFi密码填入引号中
|
||||
|
||||
Forecast forecast; // 建立Forecast对象用于获取心知天气信息
|
||||
|
||||
void setup(){
|
||||
Serial.begin(9600);
|
||||
Serial.println("");
|
||||
|
||||
connectWiFi(); // 连接wifi
|
||||
|
||||
// 配置心知天气请求信息
|
||||
forecast.config(reqUserKey, reqLocation, reqUnit);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
if(forecast.update()){ // 更新天气信息
|
||||
for(int i = 0; i < 3; i++){
|
||||
Serial.print(F("========Day "));
|
||||
Serial.print(i);
|
||||
Serial.println(F("========"));
|
||||
|
||||
Serial.print(F("Day Weather: "));
|
||||
Serial.print(forecast.getDayText(i));
|
||||
Serial.print(F(" "));
|
||||
Serial.println(forecast.getDayCode(i));
|
||||
Serial.print(F("Night Weather: "));
|
||||
Serial.print(forecast.getNightText(i));
|
||||
Serial.print(F(" "));
|
||||
Serial.println(forecast.getNightCode(i));
|
||||
Serial.print(F("High: "));
|
||||
Serial.print(forecast.getHigh(i));
|
||||
Serial.println(F("°C"));
|
||||
Serial.print(F("LOW: "));
|
||||
Serial.print(forecast.getLow(i));
|
||||
Serial.println(F("°C"));
|
||||
|
||||
Serial.print(F("Rainfall: "));
|
||||
Serial.print(forecast.getRain(i));
|
||||
Serial.println(F("%"));
|
||||
Serial.print(F("Wind Direction: "));
|
||||
Serial.println(forecast.getWindDirection(i));
|
||||
Serial.print(F("Wind Speed: "));
|
||||
Serial.println(forecast.getWindSpeed(i));
|
||||
Serial.print(F("Wind Scale: "));
|
||||
Serial.println(forecast.getWindScale(i));
|
||||
Serial.print(F("Humidity: "));
|
||||
Serial.println(forecast.getHumidity(i));
|
||||
|
||||
Serial.print(F("Last Update: "));
|
||||
Serial.println(forecast.getLastUpdate());
|
||||
}
|
||||
Serial.print(F("Server Code: "));
|
||||
Serial.println(forecast.getServerCode());
|
||||
Serial.println(F("====================="));
|
||||
} else { // 更新失败
|
||||
Serial.println("Update Fail...");
|
||||
//Serial.print("Server Response: "); // 输出服务器响应状态码供用户查找问题
|
||||
//Serial.println(weatherNow.getServerCode()); // 心知天气服务器错误代码说明可通过以下网址获取
|
||||
} // https://docs.seniverse.com/api/start/error.html
|
||||
|
||||
delay(3000);
|
||||
}
|
||||
|
||||
// 连接WiFi
|
||||
void connectWiFi(){
|
||||
WiFi.begin(ssid, password); // 启动网络连接
|
||||
Serial.print("Connecting to "); // 串口监视器输出网络连接信息
|
||||
Serial.print(ssid); Serial.println(" ..."); // 告知用户NodeMCU正在尝试WiFi连接
|
||||
|
||||
int i = 0; // 这一段程序语句用于检查WiFi是否连接成功
|
||||
while (WiFi.status() != WL_CONNECTED) { // WiFi.status()函数的返回值是由NodeMCU的WiFi连接状态所决定的。
|
||||
delay(1000); // 如果WiFi连接成功则返回值为WL_CONNECTED
|
||||
Serial.print(i++); Serial.print(' '); // 此处通过While循环让NodeMCU每隔一秒钟检查一次WiFi.status()函数返回值
|
||||
} // 同时NodeMCU将通过串口监视器输出连接时长读秒。
|
||||
// 这个读秒是通过变量i每隔一秒自加1来实现的。
|
||||
Serial.println(""); // WiFi连接成功后
|
||||
Serial.println("Connection established!"); // NodeMCU将通过串口监视器输出"连接成功"信息。
|
||||
Serial.print("IP address: "); // 同时还将输出NodeMCU的IP地址。这一功能是通过调用
|
||||
Serial.println(WiFi.localIP()); // WiFi.localIP()函数来实现的。该函数的返回值即NodeMCU的IP地址。
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/**********************************************************************
|
||||
项目名称/Project : 零基础入门学用物联网
|
||||
程序名称/Program name : life_info(rev.0.01)
|
||||
团队/Team : 太极创客团队 / Taichi-Maker (www.taichi-maker.com)
|
||||
作者/Author : CYNO朔
|
||||
日期/Date(YYYYMMDD) : 初版建立时间20200603
|
||||
程序目的/Purpose :
|
||||
使用ESP8266_Seniverse库通过心知天气服务器获取生活信息
|
||||
-----------------------------------------------------------------------
|
||||
本程序所属仓库网址
|
||||
GitHub: https://github.com/taichi-maker/ESP8266-Seniverse
|
||||
Gitee: https://gitee.com/taijichuangke/ESP8266-Seniverse
|
||||
-----------------------------------------------------------------------
|
||||
其它说明 / Other Description
|
||||
心知天气API文档说明: https://www.seniverse.com/docs
|
||||
|
||||
本程序为太极创客团队制作的免费视频教程《零基础入门学用物联网 》中一部分。该教程系统的
|
||||
向您讲述ESP8266的物联网应用相关的软件和硬件知识。以下是该教程目录页:
|
||||
http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/
|
||||
***********************************************************************/
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266_Seniverse.h>
|
||||
|
||||
const char* ssid = "taichimaker"; // 连接WiFi名(此处使用taichi-maker为示例)
|
||||
// 请将您需要连接的WiFi名填入引号中
|
||||
const char* password = "12345678"; // 连接WiFi密码(此处使用12345678为示例)
|
||||
// 请将您需要连接的WiFi密码填入引号中
|
||||
|
||||
// 心知天气API请求所需信息
|
||||
// 请对以下信息进行修改,填入您的心知天气私钥以及需要获取天气信息的城市和温度单位
|
||||
// 如需进一步了解心知天气API所提供的城市列表等信息,请前往心知天气官方产品文档网址:
|
||||
// https://www.seniverse.com/docs
|
||||
String reqUserKey = "XXXXXXXXXXXXXXXXX"; // 私钥
|
||||
String reqLocation = "beijing"; // 城市
|
||||
String reqUnit = "c"; // 摄氏/华氏// 请将您需要连接的WiFi密码填入引号中
|
||||
|
||||
LifeInfo lifeInfo; // 建立Forecast对象用于获取心知天气信息
|
||||
|
||||
void setup(){
|
||||
Serial.begin(9600);
|
||||
Serial.println("");
|
||||
|
||||
connectWiFi(); // 连接wifi
|
||||
|
||||
// 配置心知天气请求信息
|
||||
lifeInfo.config(reqUserKey, reqLocation, reqUnit);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
if(lifeInfo.update()){ // 更新天气信息
|
||||
Serial.println("Update OK");
|
||||
Serial.print("Server Response: ");
|
||||
Serial.println(lifeInfo.getServerCode());
|
||||
Serial.println(F("======Life Info======="));
|
||||
Serial.print(F("Car Washing: "));
|
||||
Serial.println(lifeInfo.getCarWash());
|
||||
Serial.print(F("Dressing: "));
|
||||
Serial.println(lifeInfo.getDressing());
|
||||
Serial.print(F("Flu Factor: "));
|
||||
Serial.println(lifeInfo.getFactorFlu());
|
||||
Serial.print(F("Sport: "));
|
||||
Serial.println(lifeInfo.getExercise());
|
||||
Serial.print(F("Travel: "));
|
||||
Serial.println(lifeInfo.getTravel());
|
||||
Serial.print(F("UV: "));
|
||||
Serial.println(lifeInfo.getUV());
|
||||
Serial.print(F("Last Update: "));
|
||||
Serial.println(lifeInfo.getLastUpdate());
|
||||
Serial.println(F("======================"));
|
||||
} else { // 更新失败
|
||||
Serial.println("Update Fail...");
|
||||
Serial.print("Server Response: "); // 输出服务器响应状态码供用户查找问题
|
||||
Serial.println(lifeInfo.getServerCode()); // 心知天气服务器错误代码说明可通过以下网址获取
|
||||
} // https://docs.seniverse.com/api/start/error.html
|
||||
delay(3000);
|
||||
}
|
||||
|
||||
// 连接WiFi
|
||||
void connectWiFi(){
|
||||
WiFi.begin(ssid, password); // 启动网络连接
|
||||
Serial.print("Connecting to "); // 串口监视器输出网络连接信息
|
||||
Serial.print(ssid); Serial.println(" ..."); // 告知用户NodeMCU正在尝试WiFi连接
|
||||
|
||||
int i = 0; // 这一段程序语句用于检查WiFi是否连接成功
|
||||
while (WiFi.status() != WL_CONNECTED) { // WiFi.status()函数的返回值是由NodeMCU的WiFi连接状态所决定的。
|
||||
delay(1000); // 如果WiFi连接成功则返回值为WL_CONNECTED
|
||||
Serial.print(i++); Serial.print(' '); // 此处通过While循环让NodeMCU每隔一秒钟检查一次WiFi.status()函数返回值
|
||||
} // 同时NodeMCU将通过串口监视器输出连接时长读秒。
|
||||
// 这个读秒是通过变量i每隔一秒自加1来实现的。
|
||||
Serial.println(""); // WiFi连接成功后
|
||||
Serial.println("Connection established!"); // NodeMCU将通过串口监视器输出"连接成功"信息。
|
||||
Serial.print("IP address: "); // 同时还将输出NodeMCU的IP地址。这一功能是通过调用
|
||||
Serial.println(WiFi.localIP()); // WiFi.localIP()函数来实现的。该函数的返回值即NodeMCU的IP地址。
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/**********************************************************************
|
||||
项目名称/Project : 零基础入门学用物联网
|
||||
程序名称/Program name : weather_now(rev.002)
|
||||
团队/Team : 太极创客团队 / Taichi-Maker (www.taichi-maker.com)
|
||||
作者/Author : CYNO朔
|
||||
日期/Date(YYYYMMDD) : 初版建立时间20200602(最新修订时间20200607)
|
||||
程序目的/Purpose :
|
||||
使用ESP8266_Seniverse库通过心知天气服务器获取实时天气信息。
|
||||
-----------------------------------------------------------------------
|
||||
本程序所属仓库网址
|
||||
GitHub: https://github.com/taichi-maker/ESP8266-Seniverse
|
||||
Gitee: https://gitee.com/taijichuangke/ESP8266-Seniverse
|
||||
-----------------------------------------------------------------------
|
||||
其它说明 / Other Description
|
||||
心知天气API文档说明: https://www.seniverse.com/docs
|
||||
|
||||
本程序为太极创客团队制作的免费视频教程《零基础入门学用物联网 》中一部分。该教程系统的
|
||||
向您讲述ESP8266的物联网应用相关的软件和硬件知识。以下是该教程目录页:
|
||||
http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/
|
||||
***********************************************************************/
|
||||
#include <ArduinoJson.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266_Seniverse.h>
|
||||
|
||||
const char* ssid = "taichimaker"; // 连接WiFi名(此处使用taichi-maker为示例)
|
||||
// 请将您需要连接的WiFi名填入引号中
|
||||
const char* password = "12345678"; // 连接WiFi密码(此处使用12345678为示例) // 请将您需要连接的WiFi密码填入引号中
|
||||
// 请将您需要连接的WiFi密码填入引号中
|
||||
|
||||
// 心知天气HTTP请求所需信息
|
||||
// 请对以下信息进行修改,填入您的心知天气私钥以及需要获取天气信息的城市和温度单位
|
||||
// 如需进一步了解心知天气API所提供的城市列表等信息,请前往心知天气官方产品文档网址:
|
||||
// https://www.seniverse.com/docs
|
||||
String reqUserKey = "XXXXXXXXXXXXXXXXX"; // 私钥
|
||||
String reqLocation = "beijing"; // 城市
|
||||
String reqUnit = "c"; // 摄氏/华氏
|
||||
|
||||
WeatherNow weatherNow; // 建立WeatherNow对象用于获取心知天气信息
|
||||
|
||||
void setup(){
|
||||
Serial.begin(9600);
|
||||
Serial.println("");
|
||||
|
||||
connectWiFi(); // 连接wifi
|
||||
|
||||
// 配置心知天气请求信息
|
||||
weatherNow.config(reqUserKey, reqLocation, reqUnit);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
if(weatherNow.update()){ // 更新天气信息
|
||||
Serial.println(F("======Weahter Info======"));
|
||||
Serial.print("Server Response: ");
|
||||
Serial.println(weatherNow.getServerCode()); // 获取服务器响应码
|
||||
Serial.print(F("Weather Now: "));
|
||||
Serial.print(weatherNow.getWeatherText()); // 获取当前天气(字符串格式)
|
||||
Serial.print(F(" "));
|
||||
Serial.println(weatherNow.getWeatherCode());// 获取当前天气(整数格式)
|
||||
Serial.print(F("Temperature: "));
|
||||
Serial.println(weatherNow.getDegree()); // 获取当前温度数值
|
||||
Serial.print(F("Last Update: "));
|
||||
Serial.println(weatherNow.getLastUpdate()); // 获取服务器更新天气信息时间
|
||||
Serial.println(F("========================"));
|
||||
} else { // 更新失败
|
||||
Serial.println("Update Fail...");
|
||||
Serial.print("Server Response: "); // 输出服务器响应状态码供用户查找问题
|
||||
Serial.println(weatherNow.getServerCode()); // 心知天气服务器错误代码说明可通过以下网址获取
|
||||
} // https://docs.seniverse.com/api/start/error.html
|
||||
|
||||
delay(3000);
|
||||
}
|
||||
|
||||
// 连接WiFi
|
||||
void connectWiFi(){
|
||||
WiFi.begin(ssid, password); // 启动网络连接
|
||||
Serial.print("Connecting to "); // 串口监视器输出网络连接信息
|
||||
Serial.print(ssid); Serial.println(" ..."); // 告知用户NodeMCU正在尝试WiFi连接
|
||||
|
||||
int i = 0; // 这一段程序语句用于检查WiFi是否连接成功
|
||||
while (WiFi.status() != WL_CONNECTED) { // WiFi.status()函数的返回值是由NodeMCU的WiFi连接状态所决定的。
|
||||
delay(1000); // 如果WiFi连接成功则返回值为WL_CONNECTED
|
||||
Serial.print(i++); Serial.print(' '); // 此处通过While循环让NodeMCU每隔一秒钟检查一次WiFi.status()函数返回值
|
||||
} // 同时NodeMCU将通过串口监视器输出连接时长读秒。
|
||||
// 这个读秒是通过变量i每隔一秒自加1来实现的。
|
||||
Serial.println(""); // WiFi连接成功后
|
||||
Serial.println("Connection established!"); // NodeMCU将通过串口监视器输出"连接成功"信息。
|
||||
Serial.print("IP address: "); // 同时还将输出NodeMCU的IP地址。这一功能是通过调用
|
||||
Serial.println(WiFi.localIP()); // WiFi.localIP()函数来实现的。该函数的返回值即NodeMCU的IP地址。
|
||||
}
|
||||
/*-----------------------------------------------------------------------
|
||||
修订历史/Revision History
|
||||
日期/Date 作者/Author 参考号/Ref 修订说明/Revision Description
|
||||
20200603 CYNO朔 001 add http response code check
|
||||
20200603 CYNO朔 002 change file name to ESP8266_Seniverse
|
||||
-----------------------------------------------------------------------*/
|
||||
44
arduino-cli/libraries/ESP8266-Seniverse/keywords.txt
Normal file
44
arduino-cli/libraries/ESP8266-Seniverse/keywords.txt
Normal file
@@ -0,0 +1,44 @@
|
||||
#######################################
|
||||
# ESP8266-心知天气库 关键字
|
||||
# ESP8266-Seniverse Library Keywords
|
||||
# 太极创客 / www.taichi-maker.com
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# 数据类型/Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
WeatherNow KEYWORD1
|
||||
LifeInfo KEYWORD1
|
||||
Forecast KEYWORD1
|
||||
|
||||
#######################################
|
||||
# 函数/Functions (KEYWORD2)
|
||||
#######################################
|
||||
ESP8266_Seniverse KEYWORD2
|
||||
getWeatherText KEYWORD2
|
||||
getWeatherCode KEYWORD2
|
||||
getDegree KEYWORD2
|
||||
getLastUpdate KEYWORD2
|
||||
getServerCode KEYWORD2
|
||||
getCarWash KEYWORD2
|
||||
getDressing KEYWORD2
|
||||
getFactorFlu KEYWORD2
|
||||
getExercise KEYWORD2
|
||||
getTravel KEYWORD2
|
||||
getUV KEYWORD2
|
||||
getLastUpdate KEYWORD2
|
||||
getServerCode KEYWORD2
|
||||
getDayText KEYWORD2
|
||||
getDayCode KEYWORD2
|
||||
getNightText KEYWORD2
|
||||
getNightCode KEYWORD2
|
||||
getHigh KEYWORD2
|
||||
getLow KEYWORD2
|
||||
getRain KEYWORD2
|
||||
getWindDirection KEYWORD2
|
||||
getWindSpeed KEYWORD2
|
||||
getWindScale KEYWORD2
|
||||
getHumidity KEYWORD2
|
||||
getLastUpdate KEYWORD2
|
||||
getServerCode KEYWORD2
|
||||
@@ -0,0 +1,9 @@
|
||||
name=ESP8266-Seniverse
|
||||
version=0.0.1
|
||||
author=CYNO-Shuo(Taichi-Maker Team) taichimaker@163.com
|
||||
maintainer=CYNO-Shuo(Taichi-Maker Team) taichimaker@163.com
|
||||
sentence=ESP8266 seniverse API Application Library
|
||||
paragraph=Use ESP8266 to get Weather Forecast and Current Weahter information from from seniverse API.
|
||||
category=Other
|
||||
url=https://www.taichi-maker.com
|
||||
architectures=*
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef _ESP8266_SENIVERSE_H_
|
||||
#define _ESP8266_SENIVERSE_H_
|
||||
|
||||
#include "WeatherNow.h"
|
||||
#include "Forecast.h"
|
||||
#include "LifeInfo.h"
|
||||
#endif
|
||||
214
arduino-cli/libraries/ESP8266-Seniverse/src/Forecast.cpp
Normal file
214
arduino-cli/libraries/ESP8266-Seniverse/src/Forecast.cpp
Normal 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;
|
||||
}
|
||||
64
arduino-cli/libraries/ESP8266-Seniverse/src/Forecast.h
Normal file
64
arduino-cli/libraries/ESP8266-Seniverse/src/Forecast.h
Normal 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
|
||||
145
arduino-cli/libraries/ESP8266-Seniverse/src/LifeInfo.cpp
Normal file
145
arduino-cli/libraries/ESP8266-Seniverse/src/LifeInfo.cpp
Normal 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;
|
||||
}
|
||||
53
arduino-cli/libraries/ESP8266-Seniverse/src/LifeInfo.h
Normal file
53
arduino-cli/libraries/ESP8266-Seniverse/src/LifeInfo.h
Normal 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
|
||||
137
arduino-cli/libraries/ESP8266-Seniverse/src/WeatherNow.cpp
Normal file
137
arduino-cli/libraries/ESP8266-Seniverse/src/WeatherNow.cpp
Normal 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;
|
||||
}
|
||||
47
arduino-cli/libraries/ESP8266-Seniverse/src/WeatherNow.h
Normal file
47
arduino-cli/libraries/ESP8266-Seniverse/src/WeatherNow.h
Normal 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
|
||||
Reference in New Issue
Block a user