mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 14:12:16 +03:00
Модуль Погода (Weather)
This commit is contained in:
@@ -53,6 +53,10 @@
|
||||
{
|
||||
"path": "src/modules/virtual/VButton",
|
||||
"active": true
|
||||
},
|
||||
{
|
||||
"path": "src/modules/virtual/Weather",
|
||||
"active": false
|
||||
}
|
||||
],
|
||||
"Сенсоры": [
|
||||
@@ -182,6 +186,10 @@
|
||||
"path": "src/modules/exec/IoTServo",
|
||||
"active": true
|
||||
},
|
||||
{
|
||||
"path": "src/modules/exec/Mcp23008",
|
||||
"active": false
|
||||
},
|
||||
{
|
||||
"path": "src/modules/exec/Mcp23017",
|
||||
"active": true
|
||||
|
||||
153
src/modules/virtual/Weather/Weather.cpp
Normal file
153
src/modules/virtual/Weather/Weather.cpp
Normal file
@@ -0,0 +1,153 @@
|
||||
#include "Global.h"
|
||||
#include "classes/IoTItem.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
long prevWeatherMillis = millis() - 60001;
|
||||
StaticJsonDocument<JSON_BUFFER_SIZE * 2> Weatherdoc;
|
||||
|
||||
extern IoTGpio IoTgpio;
|
||||
class Weather : public IoTItem
|
||||
{
|
||||
private:
|
||||
String _location;
|
||||
String _param;
|
||||
long interval;
|
||||
|
||||
public:
|
||||
Weather(String parameters) : IoTItem(parameters)
|
||||
{
|
||||
_location = jsonReadStr(parameters, "location");
|
||||
_param = jsonReadStr(parameters, "param");
|
||||
jsonRead(parameters, F("int"), interval);
|
||||
interval = interval * 1000 * 60 * 60; // интервал проверки погоды в часах
|
||||
}
|
||||
|
||||
void getWeather()
|
||||
{
|
||||
String ret;
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
{
|
||||
// char c;
|
||||
String payload;
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
http.begin(client, "http://live-control.com/iotm/weather.php");
|
||||
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
String httpRequestData = "loc=" + _location;
|
||||
int httpResponseCode = http.POST(httpRequestData);
|
||||
if (httpResponseCode > 0)
|
||||
{
|
||||
ret = httpResponseCode;
|
||||
|
||||
if (httpResponseCode == HTTP_CODE_OK)
|
||||
{
|
||||
payload = http.getString();
|
||||
|
||||
deserializeJson(Weatherdoc, payload);
|
||||
// ret += payload;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = http.errorToString(httpResponseCode).c_str();
|
||||
}
|
||||
SerialPrint("<-", F("getWeather"), httpRequestData);
|
||||
SerialPrint("->", F("getWeather"), "server: " + ret);
|
||||
|
||||
http.end();
|
||||
}
|
||||
}
|
||||
|
||||
void doByInterval()
|
||||
{
|
||||
|
||||
if (prevWeatherMillis + 60000 < millis())
|
||||
{
|
||||
getWeather();
|
||||
prevWeatherMillis = millis();
|
||||
}
|
||||
if (jsonReadStr(Weatherdoc["current_condition"][0], "temp_C", true) != "null")
|
||||
{
|
||||
if (_param == "temp_C")
|
||||
{
|
||||
value.valS = jsonReadStr(Weatherdoc["current_condition"][0], "temp_C", true);
|
||||
}
|
||||
if (_param == "avgtempC")
|
||||
{
|
||||
value.valS = jsonReadStr(Weatherdoc["weather"][0], "avgtempC", true);
|
||||
}
|
||||
if (_param == "humidity")
|
||||
{
|
||||
value.valS = jsonReadStr(Weatherdoc["current_condition"][0], "humidity", true);
|
||||
}
|
||||
if (_param == "weatherCode")
|
||||
{
|
||||
value.valS = jsonReadStr(Weatherdoc["current_condition"][0], "weatherCode", true);
|
||||
}
|
||||
if (_param == "sunrise")
|
||||
{
|
||||
|
||||
value.valS = jsonReadStr(Weatherdoc["weather"][0]["astronomy"][0], "sunrise", true);
|
||||
}
|
||||
if (_param == "sunset")
|
||||
{
|
||||
value.valS = jsonReadStr(Weatherdoc["weather"][0]["astronomy"][0], "sunset", true);
|
||||
}
|
||||
|
||||
if (_param == "rangetempC")
|
||||
{
|
||||
value.valS = jsonReadStr(Weatherdoc["weather"][0], "mintempC", true) + "..." + jsonReadStr(Weatherdoc["weather"][0], "maxtempC", true);
|
||||
}
|
||||
|
||||
// погода на завтра
|
||||
if (_param == "temp_C_tomorrow")
|
||||
{
|
||||
value.valS = jsonReadStr(Weatherdoc["weather"][1], "avgtempC", true);
|
||||
}
|
||||
if (_param == "rangetempC_tomorrow")
|
||||
{
|
||||
value.valS = jsonReadStr(Weatherdoc["weather"][1], "mintempC", true) + "..." + jsonReadStr(Weatherdoc["weather"][1], "maxtempC", true);
|
||||
}
|
||||
|
||||
regEvent(value.valS, "Weather");
|
||||
}
|
||||
}
|
||||
void loop()
|
||||
{
|
||||
if (enableDoByInt)
|
||||
{
|
||||
currentMillis = millis();
|
||||
difference = currentMillis - prevMillis;
|
||||
if (difference >= interval)
|
||||
{
|
||||
prevMillis = millis();
|
||||
this->doByInterval();
|
||||
}
|
||||
}
|
||||
}
|
||||
IoTValue execute(String command, std::vector<IoTValue> ¶m)
|
||||
{
|
||||
if (command == "get")
|
||||
{
|
||||
getWeather();
|
||||
doByInterval();
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
~Weather(){};
|
||||
};
|
||||
|
||||
void *getAPI_Weather(String subtype, String param)
|
||||
{
|
||||
if (subtype == F("Weather"))
|
||||
{
|
||||
return new Weather(param);
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
53
src/modules/virtual/Weather/modinfo.json
Normal file
53
src/modules/virtual/Weather/modinfo.json
Normal file
@@ -0,0 +1,53 @@
|
||||
{
|
||||
"menuSection": "Виртуальные элементы",
|
||||
|
||||
"configItem": [
|
||||
{
|
||||
"global": 0,
|
||||
"name": "Погода",
|
||||
"type": "Reading",
|
||||
"subtype": "Weather",
|
||||
"id": "Weather",
|
||||
"needSave": 0,
|
||||
"widget": "anydataDef",
|
||||
"page": "Погода",
|
||||
"descr": "",
|
||||
"int": 3,
|
||||
"location": "Moscow",
|
||||
"param": "temp_C",
|
||||
"round": 0,
|
||||
"val": "..."
|
||||
}
|
||||
],
|
||||
|
||||
"about": {
|
||||
"authorName": "AVAKS",
|
||||
"authorContact": "https://t.me/@avaks_dev",
|
||||
"authorGit": "https://github.com/avaksru",
|
||||
"specialThanks": "",
|
||||
"moduleName": "Weather",
|
||||
"moduleVersion": "1",
|
||||
"usedRam": {
|
||||
"esp32_4mb": 15,
|
||||
"esp8266_4mb": 15
|
||||
},
|
||||
"title": "Погода",
|
||||
"moduleDesc": "Получение погоды из интернет",
|
||||
"propInfo": {
|
||||
"location": "Город.",
|
||||
"param": "temp_C - температура, humidity - влажность, weatherCode - погодный код, sunrise - рассвет, sunset - закат, rangetempC - (-3...-10 C), temp_C_tomorrow - температура завтра, rangetempC_tomorrow - (-3...-10 C) на завтра",
|
||||
"int": "Интервал запроса погоды в часах"
|
||||
}
|
||||
},
|
||||
|
||||
"defActive": false,
|
||||
|
||||
"usedLibs": {
|
||||
"esp32_4mb": [],
|
||||
"esp8266_4mb": [],
|
||||
"esp8266_1mb": [],
|
||||
"esp8266_1mb_ota": [],
|
||||
"esp8285_1mb": [],
|
||||
"esp8285_1mb_ota": []
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user