Модуль Погода (Weather)

This commit is contained in:
avaksru
2022-12-26 13:01:08 +03:00
parent 7c050b0492
commit 821aeb5ad2
3 changed files with 214 additions and 0 deletions

View File

@@ -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

View 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> &param)
{
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;
}
}

View 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": []
}
}