From 821aeb5ad2a9b2809c5606b613b97b135e1f6e67 Mon Sep 17 00:00:00 2001 From: avaksru Date: Mon, 26 Dec 2022 13:01:08 +0300 Subject: [PATCH 1/4] =?UTF-8?q?=D0=9C=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20?= =?UTF-8?q?=D0=9F=D0=BE=D0=B3=D0=BE=D0=B4=D0=B0=20(Weather)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- myProfile.json | 8 ++ src/modules/virtual/Weather/Weather.cpp | 153 +++++++++++++++++++++++ src/modules/virtual/Weather/modinfo.json | 53 ++++++++ 3 files changed, 214 insertions(+) create mode 100644 src/modules/virtual/Weather/Weather.cpp create mode 100644 src/modules/virtual/Weather/modinfo.json diff --git a/myProfile.json b/myProfile.json index a7c149a4..8d86cea2 100644 --- a/myProfile.json +++ b/myProfile.json @@ -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 diff --git a/src/modules/virtual/Weather/Weather.cpp b/src/modules/virtual/Weather/Weather.cpp new file mode 100644 index 00000000..2ff85df4 --- /dev/null +++ b/src/modules/virtual/Weather/Weather.cpp @@ -0,0 +1,153 @@ +#include "Global.h" +#include "classes/IoTItem.h" +#include + +long prevWeatherMillis = millis() - 60001; +StaticJsonDocument 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 ¶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; + } +} diff --git a/src/modules/virtual/Weather/modinfo.json b/src/modules/virtual/Weather/modinfo.json new file mode 100644 index 00000000..0530ebdf --- /dev/null +++ b/src/modules/virtual/Weather/modinfo.json @@ -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": [] + } +} From ee279870850bb1a4b0e7e039b9cc470fd3925919 Mon Sep 17 00:00:00 2001 From: avaksru Date: Mon, 26 Dec 2022 13:04:52 +0300 Subject: [PATCH 2/4] =?UTF-8?q?=D0=9C=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20?= =?UTF-8?q?=D0=A6=D0=B2=D0=B5=D1=82=D0=BD=D0=BE=D0=B9=20=D1=82=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D1=82=20(VariableColor)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_svelte/items.json | 162 ++++++++++-------- myProfile.json | 4 + platformio.ini | 1 + src/modules/API.cpp | 2 + .../virtual/VariableColor/VariableColor.cpp | 46 +++++ .../virtual/VariableColor/modinfo.json | 46 +++++ 6 files changed, 187 insertions(+), 74 deletions(-) create mode 100644 src/modules/virtual/VariableColor/VariableColor.cpp create mode 100644 src/modules/virtual/VariableColor/modinfo.json diff --git a/data_svelte/items.json b/data_svelte/items.json index 40756f1a..7ee634ce 100644 --- a/data_svelte/items.json +++ b/data_svelte/items.json @@ -162,7 +162,21 @@ }, { "global": 0, - "name": "11. Виртуальная кнопка", + "name": "11. Цветной текст", + "type": "Reading", + "subtype": "VariableColor", + "id": "color", + "needSave": 0, + "widget": "anydataDef", + "page": "Вывод", + "descr": "Цветной текст", + "val": "...", + "round": 0, + "num": 11 + }, + { + "global": 0, + "name": "12. Виртуальная кнопка", "type": "Reading", "subtype": "VButton", "id": "vbtn", @@ -172,13 +186,13 @@ "descr": "Кнопка", "int": "0", "val": "0", - "num": 11 + "num": 12 }, { "header": "Сенсоры" }, { - "name": "12. Acs712 Ток", + "name": "13. Acs712 Ток", "type": "Reading", "subtype": "Acs712", "id": "amp", @@ -188,11 +202,11 @@ "round": 3, "pin": 39, "int": 5, - "num": 12 + "num": 13 }, { "global": 0, - "name": "13. AHTXX Температура", + "name": "14. AHTXX Температура", "type": "Reading", "subtype": "AhtXXt", "id": "Temp20", @@ -203,11 +217,11 @@ "addr": "0x38", "shtType": 1, "round": 1, - "num": 13 + "num": 14 }, { "global": 0, - "name": "14. AHTXX Влажность", + "name": "15. AHTXX Влажность", "type": "Reading", "subtype": "AhtXXh", "id": "Hum20", @@ -218,11 +232,11 @@ "addr": "0x38", "shtType": 1, "round": 1, - "num": 14 + "num": 15 }, { "global": 0, - "name": "15. Аналоговый сенсор", + "name": "16. Аналоговый сенсор", "type": "Reading", "subtype": "AnalogAdc", "id": "t", @@ -236,11 +250,11 @@ "pin": 0, "int": 15, "avgSteps": 1, - "num": 15 + "num": 16 }, { "global": 0, - "name": "16. BME280 Температура", + "name": "17. BME280 Температура", "type": "Reading", "subtype": "Bme280t", "id": "tmp3", @@ -250,11 +264,11 @@ "int": 15, "addr": "0x77", "round": 1, - "num": 16 + "num": 17 }, { "global": 0, - "name": "17. BME280 Давление", + "name": "18. BME280 Давление", "type": "Reading", "subtype": "Bme280p", "id": "Press3", @@ -264,11 +278,11 @@ "int": 15, "addr": "0x77", "round": 1, - "num": 17 + "num": 18 }, { "global": 0, - "name": "18. BME280 Влажность", + "name": "19. BME280 Влажность", "type": "Reading", "subtype": "Bme280h", "id": "Hum3", @@ -278,11 +292,11 @@ "int": 15, "addr": "0x77", "round": 1, - "num": 18 + "num": 19 }, { "global": 0, - "name": "19. BMP280 Температура", + "name": "20. BMP280 Температура", "type": "Reading", "subtype": "Bmp280t", "id": "tmp3", @@ -292,11 +306,11 @@ "int": 15, "addr": "0x77", "round": 1, - "num": 19 + "num": 20 }, { "global": 0, - "name": "20. BMP280 Давление", + "name": "21. BMP280 Давление", "type": "Reading", "subtype": "Bmp280p", "id": "Press3", @@ -306,11 +320,11 @@ "int": 15, "addr": "0x77", "round": 1, - "num": 20 + "num": 21 }, { "global": 0, - "name": "21. DHT11 Температура", + "name": "22. DHT11 Температура", "type": "Reading", "subtype": "Dht1122t", "id": "tmp3", @@ -320,11 +334,11 @@ "int": 15, "pin": 0, "senstype": "dht11", - "num": 21 + "num": 22 }, { "global": 0, - "name": "22. DHT11 Влажность", + "name": "23. DHT11 Влажность", "type": "Reading", "subtype": "Dht1122h", "id": "Hum3", @@ -334,11 +348,11 @@ "int": 15, "pin": 0, "senstype": "dht11", - "num": 22 + "num": 23 }, { "global": 0, - "name": "23. DS18B20 Температура", + "name": "24. DS18B20 Температура", "type": "Reading", "subtype": "Ds18b20", "id": "dstmp", @@ -350,11 +364,11 @@ "index": 0, "addr": "", "round": 1, - "num": 23 + "num": 24 }, { "global": 0, - "name": "24. PZEM 004t Напряжение", + "name": "25. PZEM 004t Напряжение", "type": "Reading", "subtype": "Pzem004v", "id": "v", @@ -364,11 +378,11 @@ "int": 15, "addr": "0xF8", "round": 1, - "num": 24 + "num": 25 }, { "global": 0, - "name": "25. PZEM 004t Сила тока", + "name": "26. PZEM 004t Сила тока", "type": "Reading", "subtype": "Pzem004a", "id": "a", @@ -378,11 +392,11 @@ "int": 15, "addr": "0xF8", "round": 1, - "num": 25 + "num": 26 }, { "global": 0, - "name": "26. PZEM 004t Мощность", + "name": "27. PZEM 004t Мощность", "type": "Reading", "subtype": "Pzem004w", "id": "w", @@ -392,11 +406,11 @@ "int": 15, "addr": "0xF8", "round": 1, - "num": 26 + "num": 27 }, { "global": 0, - "name": "27. PZEM 004t Энергия", + "name": "28. PZEM 004t Энергия", "type": "Reading", "subtype": "Pzem004wh", "id": "wh", @@ -406,11 +420,11 @@ "int": 15, "addr": "0xF8", "round": 1, - "num": 27 + "num": 28 }, { "global": 0, - "name": "28. PZEM 004t Частота", + "name": "29. PZEM 004t Частота", "type": "Reading", "subtype": "Pzem004hz", "id": "hz", @@ -420,11 +434,11 @@ "int": 15, "addr": "0xF8", "round": 1, - "num": 28 + "num": 29 }, { "global": 0, - "name": "29. PZEM 004t Косинус", + "name": "30. PZEM 004t Косинус", "type": "Reading", "subtype": "Pzem004pf", "id": "pf", @@ -434,11 +448,11 @@ "int": 15, "addr": "0xF8", "round": 1, - "num": 29 + "num": 30 }, { "global": 0, - "name": "30. PZEM настройка", + "name": "31. PZEM настройка", "type": "Reading", "subtype": "Pzem004cmd", "id": "set", @@ -450,11 +464,11 @@ "changeaddr": 0, "setaddr": "0x01", "reset": 0, - "num": 30 + "num": 31 }, { "global": 0, - "name": "31. Sht20 Температура", + "name": "32. Sht20 Температура", "type": "Reading", "subtype": "Sht20t", "id": "tmp2", @@ -463,11 +477,11 @@ "descr": "Температура", "int": 15, "round": 1, - "num": 31 + "num": 32 }, { "global": 0, - "name": "32. Sht20 Влажность", + "name": "33. Sht20 Влажность", "type": "Reading", "subtype": "Sht20h", "id": "Hum2", @@ -476,11 +490,11 @@ "descr": "Влажность", "int": 15, "round": 1, - "num": 32 + "num": 33 }, { "global": 0, - "name": "33. Sht30 Температура", + "name": "34. Sht30 Температура", "type": "Reading", "subtype": "Sht30t", "id": "tmp30", @@ -489,11 +503,11 @@ "descr": "SHT30 Температура", "int": 15, "round": 1, - "num": 33 + "num": 34 }, { "global": 0, - "name": "34. Sht30 Влажность", + "name": "35. Sht30 Влажность", "type": "Reading", "subtype": "Sht30h", "id": "Hum30", @@ -502,12 +516,12 @@ "descr": "SHT30 Влажность", "int": 15, "round": 1, - "num": 34 + "num": 35 }, { "global": 0, - "name": "35. HC-SR04 Ультразвуковой дальномер", - "num": 35, + "name": "36. HC-SR04 Ультразвуковой дальномер", + "num": 36, "type": "Reading", "subtype": "Sonar", "id": "sonar", @@ -519,7 +533,7 @@ "int": 5 }, { - "name": "36. UART", + "name": "37. UART", "type": "Reading", "subtype": "UART", "page": "", @@ -531,14 +545,14 @@ "line": 2, "speed": 9600, "eventFormat": 0, - "num": 36 + "num": 37 }, { "header": "Исполнительные устройства" }, { "global": 0, - "name": "37. Кнопка подключенная к пину", + "name": "38. Кнопка подключенная к пину", "type": "Writing", "subtype": "ButtonIn", "id": "btn", @@ -552,11 +566,11 @@ "pinMode": "INPUT", "debounceDelay": 50, "fixState": 0, - "num": 37 + "num": 38 }, { "global": 0, - "name": "38. Управление пином", + "name": "39. Управление пином", "type": "Writing", "subtype": "ButtonOut", "needSave": 0, @@ -567,11 +581,11 @@ "int": 0, "inv": 0, "pin": 2, - "num": 38 + "num": 39 }, { "global": 0, - "name": "39. Сервопривод", + "name": "40. Сервопривод", "type": "Writing", "subtype": "IoTServo", "id": "servo", @@ -582,11 +596,11 @@ "pin": 12, "apin": -1, "amap": "0, 4096, 0, 180", - "num": 39 + "num": 40 }, { "global": 0, - "name": "40. Расширитель портов Mcp23017", + "name": "41. Расширитель портов Mcp23017", "type": "Reading", "subtype": "Mcp23017", "id": "Mcp", @@ -596,11 +610,11 @@ "int": "0", "addr": "0x20", "index": 1, - "num": 40 + "num": 41 }, { "global": 0, - "name": "41. MP3 плеер", + "name": "42. MP3 плеер", "type": "Reading", "subtype": "Mp3", "id": "mp3", @@ -610,11 +624,11 @@ "int": 1, "pins": "14,12", "volume": 20, - "num": 41 + "num": 42 }, { "global": 0, - "name": "42. Сенсорная кнопка", + "name": "43. Сенсорная кнопка", "type": "Writing", "subtype": "Multitouch", "id": "impulse", @@ -628,11 +642,11 @@ "pinMode": "INPUT", "debounceDelay": 50, "PWMDelay": 500, - "num": 42 + "num": 43 }, { "global": 0, - "name": "43. Расширитель портов Pcf8574", + "name": "44. Расширитель портов Pcf8574", "type": "Reading", "subtype": "Pcf8574", "id": "Pcf", @@ -642,11 +656,11 @@ "int": "0", "addr": "0x20", "index": 1, - "num": 43 + "num": 44 }, { "global": 0, - "name": "44. PWM ESP8266", + "name": "45. PWM ESP8266", "type": "Writing", "subtype": "Pwm8266", "id": "pwm", @@ -658,11 +672,11 @@ "freq": 5000, "val": 0, "apin": -1, - "num": 44 + "num": 45 }, { "global": 0, - "name": "45. Телеграм-Лайт", + "name": "46. Телеграм-Лайт", "type": "Writing", "subtype": "TelegramLT", "id": "tg", @@ -671,14 +685,14 @@ "descr": "", "token": "", "chatID": "", - "num": 45 + "num": 46 }, { "header": "Экраны" }, { "global": 0, - "name": "46. LCD экран 2004", + "name": "47. LCD экран 2004", "type": "Reading", "subtype": "Lcd2004", "id": "Lcd", @@ -690,10 +704,10 @@ "size": "20,4", "coord": "0,0", "id2show": "id датчика", - "num": 46 + "num": 47 }, { - "name": "47. LCD экран 1602", + "name": "48. LCD экран 1602", "type": "Reading", "subtype": "Lcd2004", "id": "Lcd", @@ -705,6 +719,6 @@ "size": "16,2", "coord": "0,0", "id2show": "id датчика", - "num": 47 + "num": 48 } ] \ No newline at end of file diff --git a/myProfile.json b/myProfile.json index 8d86cea2..c2d9cd48 100644 --- a/myProfile.json +++ b/myProfile.json @@ -50,6 +50,10 @@ "path": "src/modules/virtual/Variable", "active": true }, + { + "path": "src/modules/virtual/VariableColor", + "active": true + }, { "path": "src/modules/virtual/VButton", "active": true diff --git a/platformio.ini b/platformio.ini index 117bb9e5..d95e5db4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -264,6 +264,7 @@ build_src_filter = + + + + + + + + diff --git a/src/modules/API.cpp b/src/modules/API.cpp index 12df2c22..02ae7391 100644 --- a/src/modules/API.cpp +++ b/src/modules/API.cpp @@ -5,6 +5,7 @@ void* getAPI_Loging(String subtype, String params); void* getAPI_LogingDaily(String subtype, String params); void* getAPI_Timer(String subtype, String params); void* getAPI_Variable(String subtype, String params); +void* getAPI_VariableColor(String subtype, String params); void* getAPI_VButton(String subtype, String params); void* getAPI_Acs712(String subtype, String params); void* getAPI_AhtXX(String subtype, String params); @@ -36,6 +37,7 @@ if ((tmpAPI = getAPI_Loging(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_LogingDaily(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Timer(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Variable(subtype, params)) != nullptr) return tmpAPI; +if ((tmpAPI = getAPI_VariableColor(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_VButton(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Acs712(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_AhtXX(subtype, params)) != nullptr) return tmpAPI; diff --git a/src/modules/virtual/VariableColor/VariableColor.cpp b/src/modules/virtual/VariableColor/VariableColor.cpp new file mode 100644 index 00000000..4bdc4bff --- /dev/null +++ b/src/modules/virtual/VariableColor/VariableColor.cpp @@ -0,0 +1,46 @@ +#include "Global.h" +#include "classes/IoTItem.h" + +// дочь - родитель +class VariableColor : public IoTItem +{ +private: +public: + VariableColor(String parameters) : IoTItem(parameters) + { + } + + void doByInterval() + { + } + + // событие когда пользователь подключается приложением или веб интерфейсом к усройству + void onMqttWsAppConnectEvent() + { + SerialPrint("i", "Connecting", "Dashbord open "); + regEvent(value.valD, "VariableColor", false, true); + } + + IoTValue execute(String command, std::vector ¶m) + { + if (command == "widget" && param.size() == 2) + { + String json = "{}"; + jsonWriteStr(json, param[0].valS, param[1].valS); + sendSubWidgetsValues(_id, json); + } + return {}; + } +}; + +void *getAPI_VariableColor(String subtype, String param) +{ + if (subtype == F("VariableColor")) + { + return new VariableColor(param); + } + else + { + return nullptr; + } +} diff --git a/src/modules/virtual/VariableColor/modinfo.json b/src/modules/virtual/VariableColor/modinfo.json new file mode 100644 index 00000000..7a2a6d72 --- /dev/null +++ b/src/modules/virtual/VariableColor/modinfo.json @@ -0,0 +1,46 @@ +{ + "menuSection": "Виртуальные элементы", + "configItem": [ + { + "global": 0, + "name": "Цветной текст", + "type": "Reading", + "subtype": "VariableColor", + "id": "color", + "needSave": 0, + "widget": "anydataDef", + "page": "Вывод", + "descr": "Цветной текст", + "val": "...", + "round": 0 + } + ], + "about": { + "authorName": "AVAKS", + "authorContact": "https://t.me/@avaks_dev", + "authorGit": "https://github.com/avaksru", + "specialThanks": "", + "moduleName": "VariableColor", + "moduleVersion": "1", + "usedRam": { + "esp32_4mb": 15, + "esp8266_4mb": 15 + }, + "title": "Цветной текст", + "moduleDesc": "Текст с возможностью динамического изменения цвета", + "propInfo": { + "val": "Значение при старте" + } + }, + + "defActive": true, + + "usedLibs": { + "esp32_4mb": [], + "esp8266_4mb": [], + "esp8266_1mb": [], + "esp8266_1mb_ota": [], + "esp8285_1mb": [], + "esp8285_1mb_ota": [] + } +} \ No newline at end of file From 987461ed289f14fd4c61def8e69742521747ec0c Mon Sep 17 00:00:00 2001 From: avaksru Date: Mon, 26 Dec 2022 15:45:12 +0300 Subject: [PATCH 3/4] change to value.valS --- src/modules/virtual/VariableColor/VariableColor.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/virtual/VariableColor/VariableColor.cpp b/src/modules/virtual/VariableColor/VariableColor.cpp index 4bdc4bff..12e0ff34 100644 --- a/src/modules/virtual/VariableColor/VariableColor.cpp +++ b/src/modules/virtual/VariableColor/VariableColor.cpp @@ -18,10 +18,10 @@ public: void onMqttWsAppConnectEvent() { SerialPrint("i", "Connecting", "Dashbord open "); - regEvent(value.valD, "VariableColor", false, true); + regEvent(value.valS, "VariableColor", false, true); } - - IoTValue execute(String command, std::vector ¶m) + + IoTValue execute(String command, std::vector ¶m) { if (command == "widget" && param.size() == 2) { From 3e24bf61e0b0a1e4d752deaf9162ee1efa61cae3 Mon Sep 17 00:00:00 2001 From: avaksru Date: Tue, 27 Dec 2022 17:54:56 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=9C=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20Exte?= =?UTF-8?q?rnalMQTT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MqttClient.cpp | 4 +- .../sensors/ExternalMQTT/ExternalMQTT.cpp | 133 ++++++++++++++++++ src/modules/sensors/ExternalMQTT/modinfo.json | 58 ++++++++ 3 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 src/modules/sensors/ExternalMQTT/ExternalMQTT.cpp create mode 100644 src/modules/sensors/ExternalMQTT/modinfo.json diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index a74c66ca..8ec11fe8 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -110,8 +110,8 @@ void mqttSubscribe() { mqtt.subscribe((mqttRootDevice + "/update").c_str()); if (jsonReadBool(settingsFlashJson, "mqttin")) { - mqtt.subscribe((mqttPrefix + "/+/+/event").c_str()); - mqtt.subscribe((mqttPrefix + "/+/+/order").c_str()); + mqtt.subscribe((mqttPrefix + "/+/+/event/#").c_str()); + mqtt.subscribe((mqttPrefix + "/+/+/order/#").c_str()); mqtt.subscribe((mqttPrefix + "/+/+/info").c_str()); } } diff --git a/src/modules/sensors/ExternalMQTT/ExternalMQTT.cpp b/src/modules/sensors/ExternalMQTT/ExternalMQTT.cpp new file mode 100644 index 00000000..3ad82a54 --- /dev/null +++ b/src/modules/sensors/ExternalMQTT/ExternalMQTT.cpp @@ -0,0 +1,133 @@ +#include "Global.h" +#include "classes/IoTItem.h" + +class ExternalMQTT : public IoTItem +{ +private: + String _MAC; + String _sensor; + IoTItem *tmp; + int _minutesPassed = 0; + String json = "{}"; + int orange = 0; + int red = 0; + int offline = 0; + bool dataFromNode = false; + +public: + ExternalMQTT(String parameters) : IoTItem(parameters) + { + _MAC = jsonReadStr(parameters, "MAC"); + _sensor = jsonReadStr(parameters, "sensor"); + jsonRead(parameters, F("orange"), orange); + jsonRead(parameters, F("red"), red); + jsonRead(parameters, F("offline"), offline); + dataFromNode = false; + } + char *TimeToString(unsigned long t) + { + static char str[12]; + long h = t / 3600; + t = t % 3600; + int m = t / 60; + int s = t % 60; + sprintf(str, "%02ld:%02d:%02d", h, m, s); + return str; + } + void onMqttRecive(String &topic, String &msg) + { + if (msg.indexOf("HELLO") == -1) + { + + // SerialPrint("i", "onMqttRecive", "Прилетело " + topic); + // SerialPrint("i", "onMqttRecive", "Прилетело " + msg); + String dev = selectToMarkerLast(topic, "/"); + dev.toUpperCase(); + dev.replace(":", ""); + if (_MAC == "") + { + SerialPrint("i", "onMqttRecive", dev + " --> " + msg); + } + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + DeserializationError error = deserializeJson(doc, msg); + if (error) + { + SerialPrint("E", F("onMqttRecive"), error.f_str()); + } + JsonObject jsonObject = doc.as(); + + for (JsonPair kv : jsonObject) + { + String key = kv.key().c_str(); + String val = kv.value(); + if (_MAC == dev && _sensor == key) + { + dataFromNode = true; + _minutesPassed = 0; + setValue(val); + // setNewWidgetAttributes(); + } + + // Serial.println("Key: " + key); + // Serial.println("Value: " + val); + } + } + } + + void doByInterval() + { + _minutesPassed++; + setNewWidgetAttributes(); + } + void onMqttWsAppConnectEvent() + { + setNewWidgetAttributes(); + } + + void setNewWidgetAttributes() + { + + jsonWriteStr(json, F("info"), prettyMinutsTimeout(_minutesPassed)); + if (dataFromNode) + { + if (orange != 0 && red != 0 && offline != 0) + { + if (_minutesPassed < orange) + { + jsonWriteStr(json, F("color"), ""); + } + if (_minutesPassed >= orange && _minutesPassed < red) + { + jsonWriteStr(json, F("color"), F("orange")); // сделаем виджет оранжевым + } + if (_minutesPassed >= red && _minutesPassed < offline) + { + jsonWriteStr(json, F("color"), F("red")); // сделаем виджет красным + } + if (_minutesPassed >= offline) + { + jsonWriteStr(json, F("info"), F("offline")); + } + } + } + else + { + jsonWriteStr(json, F("info"), F("awaiting")); + } + sendSubWidgetsValues(_id, json); + } + + ~ExternalMQTT(){}; +}; + +void *getAPI_ExternalMQTT(String subtype, String param) +{ + if (subtype == F("ExternalMQTT")) + { + return new ExternalMQTT(param); + } + else + { + return nullptr; + } +} diff --git a/src/modules/sensors/ExternalMQTT/modinfo.json b/src/modules/sensors/ExternalMQTT/modinfo.json new file mode 100644 index 00000000..090344e1 --- /dev/null +++ b/src/modules/sensors/ExternalMQTT/modinfo.json @@ -0,0 +1,58 @@ +{ + "menuSection": "Сенсоры", + + "configItem": [ + { + "global": 0, + "name": "MQTT парсер", + "type": "Reading", + "subtype": "ExternalMQTT", + "id": "MQTTin", + "widget": "", + "page": "", + "descr": "", + "MAC": "", + "sensor": "", + "round": "", + "orange": 60, + "red": 120, + "offline": 180, + "int": 60 + } + ], + + "about": { + "authorName": "AVAKS", + "authorContact": "https://t.me/@avaks_dev", + "authorGit": "https://github.com/avaksru", + "specialThanks": "", + "moduleName": "ExternalMQTT", + "moduleVersion": "1", + "usedRam": { + "esp32_4mb": 15, + "esp8266_4mb": 15 + }, + "title": "ExternalMQTT", + "moduleDesc": "Модуль получения данных из OpenMQTTGateway, Zigbee2MQTT, SLS, Tasmota, NodeRead, HA, openHAB, Fhem, domotiz, EEdom", + "propInfo": { + "round": "Округление после запятой.", + "int": "Интервал для изменения цвета", + "orange": "количество минут после которого окрасить виджет в оранжевый цвет", + "red": "количество минут после которого окрасить виджет в красный цвет", + "offline": "количество минут после которого отобразить что устройство offline, если все три orange red и offline поставить в ноль - то функция окраски выключится", + "MAC": "MAC адрес беспроводного датчика", + "sensor": "Тип сенсора: температура / влажность / время / ... " + } + }, + + "defActive": false, + + "usedLibs": { + "esp32_4mb": [], + "esp8266_4mb": [], + "esp8266_1mb": [], + "esp8266_1mb_ota": [], + "esp8285_1mb": [], + "esp8285_1mb_ota": [] + } +}