From 3bcdf8d8b5864c35884ed56ab3e6a55e5fb975e2 Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 13:26:34 +0300 Subject: [PATCH 01/12] =?UTF-8?q?=D0=94=D0=B5=D0=BB=D0=B0=D0=B5=D0=BC=20Ds?= =?UTF-8?q?18b20=20=D1=81=20=D0=B1=D0=BE=D0=BB=D1=8C=D1=88=D0=BE=D0=B9=20?= =?UTF-8?q?=D0=B1=D1=83=D0=BA=D0=B2=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_svelte/config.json | 12 ++++++++++++ src/modules/API.cpp | 4 ++-- src/modules/ds18b20.cpp | 14 +++++++------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/data_svelte/config.json b/data_svelte/config.json index 583d056b..2afecbd8 100644 --- a/data_svelte/config.json +++ b/data_svelte/config.json @@ -11,5 +11,17 @@ "multiply": 1, "pin": 0, "int": 15 + }, + { + "type": "Reading", + "subtype": "Ds18b20", + "id": "tmp", + "widget": "anydataTmp", + "page": "Сенсоры", + "descr": "Температура", + "int": 15, + "pin": 0, + "index": 0, + "addr": "" } ] \ No newline at end of file diff --git a/src/modules/API.cpp b/src/modules/API.cpp index 0812e534..37ef8ec3 100644 --- a/src/modules/API.cpp +++ b/src/modules/API.cpp @@ -3,7 +3,7 @@ //============================================================================================ //здесь скопируйте строку и вставьте ниже, заменив имя AnalogAdc на название вашего сенсора void* getAPI_AnalogAdc(String subtype, String params); -void* getAPI_ds18b20(String subtype, String params); +void* getAPI_Ds18b20(String subtype, String params); //============================================================================================ void* getAPI(String subtype, String params) { @@ -11,7 +11,7 @@ void* getAPI(String subtype, String params) { //=============================================================================================================== //здесь нужно скопировать строку еще раз и вставить ее ниже, переименовав AnalogAdc на название вашего сенсора if ((tmpAPI = getAPI_AnalogAdc(subtype, params)) != nullptr) return tmpAPI; - if ((tmpAPI = getAPI_ds18b20(subtype, params)) != nullptr) return tmpAPI; + if ((tmpAPI = getAPI_Ds18b20(subtype, params)) != nullptr) return tmpAPI; //================================================================================================================ return nullptr; diff --git a/src/modules/ds18b20.cpp b/src/modules/ds18b20.cpp index c227dc2c..628ec0bb 100644 --- a/src/modules/ds18b20.cpp +++ b/src/modules/ds18b20.cpp @@ -9,7 +9,7 @@ std::map oneWireTemperatureArray; std::map sensorsTemperatureArray; -class ds18b20 : public IoTSensor { +class Ds18b20 : public IoTSensor { private: //для работы библиотеки с несколькими линиями необходимо обеспечить каждый экземпляр класса ссылками на объекты настроенные на эти линии OneWire* oneWire; @@ -27,7 +27,7 @@ class ds18b20 : public IoTSensor { //Такие как ...begin и подставлять в них параметры полученные из web интерфейса. //Все параметры хранятся в перемененной parameters, вы можете прочитать любой параметр используя jsonRead функции: // jsonReadStr, jsonReadBool, jsonReadInt - ds18b20(String parameters): IoTSensor(parameters) { + Ds18b20(String parameters): IoTSensor(parameters) { _pin = jsonReadInt(parameters, "pin"); _index = jsonReadInt(parameters, "index"); @@ -79,15 +79,15 @@ class ds18b20 : public IoTSensor { } //======================================================================================================= - ~ds18b20(){}; + ~Ds18b20(){}; }; //после замены названия сенсора, на функцию можно не обращать внимания //если сенсор предполагает использование общего объекта библиотеки для нескольких экземпляров сенсора, то в данной функции необходимо предусмотреть -//создание и контроль соответствующих глобальных переменных (см. пример реализации сенсора ds18b20) -void* getAPI_ds18b20(String subtype, String param) { - if (subtype == F("ds18b20")) { - return new ds18b20(param); +//создание и контроль соответствующих глобальных переменных +void* getAPI_Ds18b20(String subtype, String param) { + if (subtype == F("Ds18b20")) { + return new Ds18b20(param); } else { return nullptr; } From 66d1fffefa63b8825d661bb2fda5b144972d7cf8 Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 14:48:52 +0300 Subject: [PATCH 02/12] =?UTF-8?q?=D0=9F=D0=BE=D1=80=D1=82=D0=B8=D1=80?= =?UTF-8?q?=D1=83=D0=B5=D0=BC=20=D0=B4=D0=B0=D1=82=D1=87=D0=B8=D0=BA=20SHT?= =?UTF-8?q?20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_svelte/config.json | 18 +++++++++++++++ platformio.ini | 2 ++ src/modules/API.cpp | 2 ++ src/modules/Sht20.cpp | 49 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 src/modules/Sht20.cpp diff --git a/data_svelte/config.json b/data_svelte/config.json index 2afecbd8..1cd18a06 100644 --- a/data_svelte/config.json +++ b/data_svelte/config.json @@ -23,5 +23,23 @@ "pin": 0, "index": 0, "addr": "" + }, + { + "type": "Reading", + "subtype": "Sht20t", + "id": "tmp2", + "widget": "anydataTmp", + "page": "Сенсоры", + "descr": "Температура", + "int": 15 + }, + { + "type": "Reading", + "subtype": "Sht20h", + "id": "Hum2", + "widget": "anydataTmp", + "page": "Сенсоры", + "descr": "Влажность", + "int": 15 } ] \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index c0e5c915..265aef96 100644 --- a/platformio.ini +++ b/platformio.ini @@ -28,6 +28,7 @@ platform = espressif8266 @2.6.3 lib_deps = ${common_env_data.lib_deps_external} milesburton/DallasTemperature@^3.9.1 + robtillaart/SHT2x@^0.1.1 monitor_filters = esp8266_exception_decoder upload_speed = 921600 monitor_speed = 115200 @@ -41,6 +42,7 @@ platform = espressif32 @3.3.0 lib_deps = ${common_env_data.lib_deps_external} milesburton/DallasTemperature@^3.9.1 + robtillaart/SHT2x@^0.1.1 monitor_filters = esp32_exception_decoder upload_speed = 921600 monitor_speed = 115200 diff --git a/src/modules/API.cpp b/src/modules/API.cpp index 37ef8ec3..2c22c4be 100644 --- a/src/modules/API.cpp +++ b/src/modules/API.cpp @@ -4,6 +4,7 @@ //здесь скопируйте строку и вставьте ниже, заменив имя AnalogAdc на название вашего сенсора void* getAPI_AnalogAdc(String subtype, String params); void* getAPI_Ds18b20(String subtype, String params); +void* getAPI_Sht20(String subtype, String params); //============================================================================================ void* getAPI(String subtype, String params) { @@ -12,6 +13,7 @@ void* getAPI(String subtype, String params) { //здесь нужно скопировать строку еще раз и вставить ее ниже, переименовав AnalogAdc на название вашего сенсора if ((tmpAPI = getAPI_AnalogAdc(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Ds18b20(subtype, params)) != nullptr) return tmpAPI; + if ((tmpAPI = getAPI_Sht20(subtype, params)) != nullptr) return tmpAPI; //================================================================================================================ return nullptr; diff --git a/src/modules/Sht20.cpp b/src/modules/Sht20.cpp new file mode 100644 index 00000000..23f69508 --- /dev/null +++ b/src/modules/Sht20.cpp @@ -0,0 +1,49 @@ +#include "Global.h" +#include "Classes/IoTSensor.h" + +#include "Wire.h" +#include "SHT2x.h" + + +SHT2x* sht = nullptr; + +class Sht20t : public IoTSensor { + public: + Sht20t(String parameters): IoTSensor(parameters) { } + + void doByInterval() { + sht->read(); + float value = sht->getTemperature(); + regEvent((String)value, "Sht20t"); + } + + ~Sht20t(); +}; + +class Sht20h : public IoTSensor { + public: + Sht20h(String parameters): IoTSensor(parameters) { } + + void doByInterval() { + float value = sht->getHumidity(); + regEvent((String)value, "Sht20h"); + } + + ~Sht20h(); +}; + + +void* getAPI_Sht20(String subtype, String param) { + if (!sht) { + sht = new SHT2x; + if (sht) sht->begin(); + } + + if (subtype == F("Sht20t")) { + return new Sht20t(param); + } else if (subtype == F("Sht20h")) { + return new Sht20h(param); + } else { + return nullptr; + } +} From ac0d82f74e5a62ff05a36a947a9b7fbee1e23f9f Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 20:28:17 +0300 Subject: [PATCH 03/12] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D1=8F=D0=B5=D0=BC=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E?= =?UTF-8?q?=20=D1=87=D1=82=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=D0=B7=20Json?= =?UTF-8?q?=20=D0=B2=20Float?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/JsonUtils.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/utils/JsonUtils.cpp b/src/utils/JsonUtils.cpp index 7c42034a..8e9e39a1 100644 --- a/src/utils/JsonUtils.cpp +++ b/src/utils/JsonUtils.cpp @@ -12,6 +12,21 @@ void jsonWriteStrDoc(DynamicJsonDocument& doc, String name, String value) { } // new============================================================================== +bool jsonRead(String& json, String key, float& value) { + bool ret = true; + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + DeserializationError error = deserializeJson(doc, json); + if (error) { + SerialPrint("EE", F("jsonRead"), error.f_str()); + ret = false; + } else if (!doc.containsKey(key)) { + SerialPrint("EE", F("jsonRead"), key + " missing"); + ret = false; + } + value = doc[key].as(); + return ret; +} + bool jsonRead(String& json, String key, String& value) { bool ret = true; DynamicJsonDocument doc(JSON_BUFFER_SIZE); From fabe47bed43b0822beaa76310845962007ae56e5 Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 20:39:51 +0300 Subject: [PATCH 04/12] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D1=8F=D0=B5=D0=BC=20=D1=87=D1=82=D0=B5=D0=BD=D0=B8=D0=B5=20Jso?= =?UTF-8?q?n=20=D0=B2=20unsigned=20long?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/utils/JsonUtils.h | 2 ++ src/utils/JsonUtils.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/utils/JsonUtils.h b/include/utils/JsonUtils.h index 9053ab41..c7ab1c61 100644 --- a/include/utils/JsonUtils.h +++ b/include/utils/JsonUtils.h @@ -10,6 +10,8 @@ extern String jsonWriteInt(String& json, String name, int value); extern String jsonWriteFloat(String& json, String name, float value); extern String jsonWriteBool(String& json, String name, boolean value); +extern bool jsonRead(String& json, String key, unsigned long& value); +extern bool jsonRead(String& json, String key, float& value); extern bool jsonRead(String& json, String key, String& value); extern bool jsonRead(String& json, String key, bool& value); extern bool jsonRead(String& json, String key, int& value); diff --git a/src/utils/JsonUtils.cpp b/src/utils/JsonUtils.cpp index 8e9e39a1..a02df298 100644 --- a/src/utils/JsonUtils.cpp +++ b/src/utils/JsonUtils.cpp @@ -12,6 +12,21 @@ void jsonWriteStrDoc(DynamicJsonDocument& doc, String name, String value) { } // new============================================================================== +bool jsonRead(String& json, String key, unsigned long& value) { + bool ret = true; + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + DeserializationError error = deserializeJson(doc, json); + if (error) { + SerialPrint("EE", F("jsonRead"), error.f_str()); + ret = false; + } else if (!doc.containsKey(key)) { + SerialPrint("EE", F("jsonRead"), key + " missing"); + ret = false; + } + value = doc[key].as(); + return ret; +} + bool jsonRead(String& json, String key, float& value) { bool ret = true; DynamicJsonDocument doc(JSON_BUFFER_SIZE); From 3b743a768d8d08579c51dde6e9a5d657c214469b Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 21:44:57 +0300 Subject: [PATCH 05/12] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D1=8F=D0=B5=D0=BC=20=D1=87=D1=82=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D0=B8=D0=B7=20SHT20=20?= =?UTF-8?q?=D0=B2=20=D0=BB=D1=8E=D0=B1=D0=BE=D0=BC=20=D1=81=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D0=B0=D0=B5,=20=D0=BD=D0=B5=20=D0=B2=D0=B7=D0=B8=D1=80?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=B2=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BD=D1=8B=D0=B9=20=D0=B2=D1=8B=D0=B7=D0=BE=D0=B2.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/Sht20.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/Sht20.cpp b/src/modules/Sht20.cpp index 23f69508..3685df20 100644 --- a/src/modules/Sht20.cpp +++ b/src/modules/Sht20.cpp @@ -25,6 +25,7 @@ class Sht20h : public IoTSensor { Sht20h(String parameters): IoTSensor(parameters) { } void doByInterval() { + sht->read(); float value = sht->getHumidity(); regEvent((String)value, "Sht20h"); } From 5b8c5793967a58d91269f53196fe83214326f727 Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 22:49:18 +0300 Subject: [PATCH 06/12] =?UTF-8?q?=D0=97=D0=B0=D0=BF=D1=80=D0=B5=D1=89?= =?UTF-8?q?=D0=B0=D0=B5=D0=BC=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D1=81=D0=BE=D0=B1?= =?UTF-8?q?=D1=8B=D1=82=D0=B8=D0=B5=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=BE?= =?UTF-8?q?=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20=D1=87=D1=82=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D0=B2=20Ds18b2?= =?UTF-8?q?0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/ds18b20.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/modules/ds18b20.cpp b/src/modules/ds18b20.cpp index 628ec0bb..ca142ffe 100644 --- a/src/modules/ds18b20.cpp +++ b/src/modules/ds18b20.cpp @@ -17,8 +17,8 @@ class Ds18b20 : public IoTSensor { //описание параметров передаваемых из настроек датчика из веба String _addr; - unsigned int _pin; - unsigned int _index; + int _pin; + int _index; public: //======================================================================================================= @@ -28,10 +28,9 @@ class Ds18b20 : public IoTSensor { //Все параметры хранятся в перемененной parameters, вы можете прочитать любой параметр используя jsonRead функции: // jsonReadStr, jsonReadBool, jsonReadInt Ds18b20(String parameters): IoTSensor(parameters) { - - _pin = jsonReadInt(parameters, "pin"); - _index = jsonReadInt(parameters, "index"); - _addr = jsonReadStr(parameters, "addr"); + jsonRead(parameters, "pin", _pin); + jsonRead(parameters, "index", _index); + jsonRead(parameters, "addr", _addr); //учитываем, что библиотека может работать с несколькими линиями на разных пинах, поэтому инициируем библиотеку, если линия ранее не использовалась if (oneWireTemperatureArray.find(_pin) == oneWireTemperatureArray.end()) { @@ -75,7 +74,8 @@ class Ds18b20 : public IoTSensor { char addrStr[20] = ""; hex2string(deviceAddress, 8, addrStr); - regEvent((String)value, "addr: " + String(addrStr)); //обязательный вызов для отправки результата работы + if (value != -127) regEvent((String)value, "addr: " + String(addrStr)); //обязательный вызов для отправки результата работы + else SerialPrint("E", "Sensor Ds18b20", "Error"); } //======================================================================================================= From d19ebe8bfc732713da9130fb96bc652779e2d663 Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 22:50:36 +0300 Subject: [PATCH 07/12] =?UTF-8?q?=D0=9F=D0=BE=D1=80=D1=82=D0=B8=D1=80?= =?UTF-8?q?=D1=83=D0=B5=D0=BC=20=D1=81=D0=B5=D0=BD=D1=81=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=20Dht11=20=D0=B8=20Dht22=20=D0=BE=D0=B4=D0=BD=D0=B8=D0=BC=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D0=B5=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_svelte/config.json | 23 ++++++++++++ platformio.ini | 2 ++ src/modules/API.cpp | 2 ++ src/modules/Dht1122.cpp | 80 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 src/modules/Dht1122.cpp diff --git a/data_svelte/config.json b/data_svelte/config.json index 1cd18a06..c1d69e0c 100644 --- a/data_svelte/config.json +++ b/data_svelte/config.json @@ -9,6 +9,7 @@ "map": "1,1024,1,1024", "plus": 0, "multiply": 1, + "round": 1, "pin": 0, "int": 15 }, @@ -41,5 +42,27 @@ "page": "Сенсоры", "descr": "Влажность", "int": 15 + }, + { + "type": "Reading", + "subtype": "Dht1122t", + "id": "tmp3", + "widget": "anydataTmp", + "page": "Сенсоры", + "descr": "Температура", + "int": 15, + "pin": 0, + "senstype": "dht11" + }, + { + "type": "Reading", + "subtype": "Dht1122h", + "id": "Hum3", + "widget": "anydataTmp", + "page": "Сенсоры", + "descr": "Влажность", + "int": 15, + "pin": 0, + "senstype": "dht11" } ] \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 265aef96..1cdb8748 100644 --- a/platformio.ini +++ b/platformio.ini @@ -29,6 +29,7 @@ lib_deps = ${common_env_data.lib_deps_external} milesburton/DallasTemperature@^3.9.1 robtillaart/SHT2x@^0.1.1 + beegee-tokyo/DHT sensor library for ESPx monitor_filters = esp8266_exception_decoder upload_speed = 921600 monitor_speed = 115200 @@ -43,6 +44,7 @@ lib_deps = ${common_env_data.lib_deps_external} milesburton/DallasTemperature@^3.9.1 robtillaart/SHT2x@^0.1.1 + beegee-tokyo/DHT sensor library for ESPx monitor_filters = esp32_exception_decoder upload_speed = 921600 monitor_speed = 115200 diff --git a/src/modules/API.cpp b/src/modules/API.cpp index 2c22c4be..7df50ce9 100644 --- a/src/modules/API.cpp +++ b/src/modules/API.cpp @@ -5,6 +5,7 @@ void* getAPI_AnalogAdc(String subtype, String params); void* getAPI_Ds18b20(String subtype, String params); void* getAPI_Sht20(String subtype, String params); +void* getAPI_Dht1122(String subtype, String params); //============================================================================================ void* getAPI(String subtype, String params) { @@ -14,6 +15,7 @@ void* getAPI(String subtype, String params) { if ((tmpAPI = getAPI_AnalogAdc(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Ds18b20(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Sht20(subtype, params)) != nullptr) return tmpAPI; + if ((tmpAPI = getAPI_Dht1122(subtype, params)) != nullptr) return tmpAPI; //================================================================================================================ return nullptr; diff --git a/src/modules/Dht1122.cpp b/src/modules/Dht1122.cpp new file mode 100644 index 00000000..20c29349 --- /dev/null +++ b/src/modules/Dht1122.cpp @@ -0,0 +1,80 @@ +/****************************************************************** + Used DHT Temperature & Humidity Sensor library for Arduino & ESP32. + Support for DHT11 and DHT22/AM2302/RHT03 + https://github.com/beegee-tokyo/arduino-DHTesp + ******************************************************************/ + + +#include "Global.h" +#include "Classes/IoTSensor.h" + +#include "DHTesp.h" +#include + + +std::map dhts; + +class Dht1122t : public IoTSensor { + private: + int _pin; + + public: + Dht1122t(String parameters): IoTSensor(parameters) { + jsonRead(parameters, "pin", _pin); + } + + void doByInterval() { + float value = dhts[_pin]->getTemperature(); + if (String(value) != "nan") regEvent((String)value, "Dht1122t"); + else SerialPrint("E", "Sensor DHTt", "Error"); + } + + ~Dht1122t(); +}; + + +class Dht1122h : public IoTSensor { + private: + int _pin; + + public: + Dht1122h(String parameters): IoTSensor(parameters) { + jsonRead(parameters, "pin", _pin); + } + + void doByInterval() { + float value = dhts[_pin]->getHumidity(); + if (String(value) != "nan") regEvent((String)value, "Dht1122h"); + else SerialPrint("E", "Sensor DHTh", "Error"); + } + + ~Dht1122h(); +}; + + +void* getAPI_Dht1122(String subtype, String param) { + int pin; + String senstype; + jsonRead(param, "pin", pin); + jsonRead(param, "senstype", senstype); + + if (dhts.find(pin) == dhts.end()) { + DHTesp* dht = new DHTesp(); + + if (senstype == "dht11") { + dht->setup(pin, DHTesp::DHT11); + } else if (senstype == "dht22") { + dht->setup(pin, DHTesp::DHT22); + } + + dhts[pin] = dht; + } + + if (subtype == F("Dht1122t")) { + return new Dht1122t(param); + } else if (subtype == F("Dht1122h")) { + return new Dht1122h(param); + } else { + return nullptr; + } +} From e06be184327b7b3bc2c5d9c0470591228299ad30 Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 23:00:36 +0300 Subject: [PATCH 08/12] =?UTF-8?q?=D0=97=D0=B0=D0=BF=D1=80=D0=B5=D1=89?= =?UTF-8?q?=D0=B0=D0=B5=D0=BC=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D1=81=D0=BE=D0=B1?= =?UTF-8?q?=D1=8B=D1=82=D0=B8=D0=B5,=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=BE?= =?UTF-8?q?=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20=D1=87=D1=82=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B2=20Sht20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/Sht20.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/modules/Sht20.cpp b/src/modules/Sht20.cpp index 3685df20..e53994ff 100644 --- a/src/modules/Sht20.cpp +++ b/src/modules/Sht20.cpp @@ -14,7 +14,8 @@ class Sht20t : public IoTSensor { void doByInterval() { sht->read(); float value = sht->getTemperature(); - regEvent((String)value, "Sht20t"); + if (value != -46.85) regEvent(value, "Sht20t"); + else SerialPrint("E", "Sensor Sht20t", "Error"); } ~Sht20t(); @@ -27,8 +28,9 @@ class Sht20h : public IoTSensor { void doByInterval() { sht->read(); float value = sht->getHumidity(); - regEvent((String)value, "Sht20h"); - } + if (value != -6) regEvent(value, "Sht20h"); + else SerialPrint("E", "Sensor Sht20h", "Error"); + } ~Sht20h(); }; From 6373f36eb5c732bf99806074534f6bacb21dc174 Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 23:01:44 +0300 Subject: [PATCH 09/12] =?UTF-8?q?=D0=A0=D0=B5=D0=B3=D0=B8=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=B8=D1=80=D1=83=D0=B5=D0=BC=20=D0=BF=D0=BE=D0=BA=D0=B0?= =?UTF-8?q?=D0=B7=D0=B0=D1=82=D0=B5=D0=BB=D0=B8=20=D1=81=D1=80=D0=B0=D0=B7?= =?UTF-8?q?=D1=83=20=D0=B2=20float=20=D0=B4=D0=BB=D1=8F=20=D0=B2=D0=BE?= =?UTF-8?q?=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B8=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D0=B5=D0=BC=D0=B0=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA?= =?UTF-8?q?=D0=B8=D1=85=20=D0=BC=D0=BE=D0=B4=D0=B8=D1=84=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/Dht1122.cpp | 4 ++-- src/modules/ds18b20.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/Dht1122.cpp b/src/modules/Dht1122.cpp index 20c29349..788e7da4 100644 --- a/src/modules/Dht1122.cpp +++ b/src/modules/Dht1122.cpp @@ -25,7 +25,7 @@ class Dht1122t : public IoTSensor { void doByInterval() { float value = dhts[_pin]->getTemperature(); - if (String(value) != "nan") regEvent((String)value, "Dht1122t"); + if (String(value) != "nan") regEvent(value, "Dht1122t"); else SerialPrint("E", "Sensor DHTt", "Error"); } @@ -44,7 +44,7 @@ class Dht1122h : public IoTSensor { void doByInterval() { float value = dhts[_pin]->getHumidity(); - if (String(value) != "nan") regEvent((String)value, "Dht1122h"); + if (String(value) != "nan") regEvent(value, "Dht1122h"); else SerialPrint("E", "Sensor DHTh", "Error"); } diff --git a/src/modules/ds18b20.cpp b/src/modules/ds18b20.cpp index ca142ffe..9cbd6109 100644 --- a/src/modules/ds18b20.cpp +++ b/src/modules/ds18b20.cpp @@ -74,7 +74,7 @@ class Ds18b20 : public IoTSensor { char addrStr[20] = ""; hex2string(deviceAddress, 8, addrStr); - if (value != -127) regEvent((String)value, "addr: " + String(addrStr)); //обязательный вызов для отправки результата работы + if (value != -127) regEvent(value, "addr: " + String(addrStr)); //обязательный вызов для отправки результата работы else SerialPrint("E", "Sensor Ds18b20", "Error"); } //======================================================================================================= From 3898234c6d440d78f9467cd80b112107c7413461 Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 23:04:06 +0300 Subject: [PATCH 10/12] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D1=8F=D0=B5=D0=BC=20=D0=B2=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D1=8B=D0=B9=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA=D1=83=20=D0=BC=D0=B0=D1=82?= =?UTF-8?q?=D0=B5=D0=BC=D0=B0=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8?= =?UTF-8?q?=D1=85=20=D0=BC=D0=BE=D0=B4=D0=B8=D1=84=D0=B8=D0=BA=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BE=D0=B2=20=D0=B4=D0=BB=D1=8F=20=D0=B2=D1=81?= =?UTF-8?q?=D0=B5=D1=85=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80=D0=B8?= =?UTF-8?q?=D1=80=D1=83=D0=B5=D0=BC=D1=8B=D1=85=20=D1=81=D0=BE=D0=B1=D1=8B?= =?UTF-8?q?=D1=82=D0=B8=D0=B9=20float=20=D1=82=D0=B8=D0=BF=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/classes/IoTSensor.h | 9 +++++++++ src/classes/IoTSensor.cpp | 39 ++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/include/classes/IoTSensor.h b/include/classes/IoTSensor.h index 40807f75..f58dd40c 100644 --- a/include/classes/IoTSensor.h +++ b/include/classes/IoTSensor.h @@ -10,6 +10,7 @@ class IoTSensor { void loop(); virtual void doByInterval(); void regEvent(String value, String consoleInfo); + void regEvent(float value, String consoleInfo); String getSubtype(); String getID(); @@ -22,4 +23,12 @@ class IoTSensor { String _subtype; String _id; unsigned long _interval; + + float _multiply; // умножаем на значение + float _plus; // увеличиваем на значение + int _map1; + int _map2; + int _map3; + int _map4; + int _round; // 1, 10, 100, 1000, 10000 }; diff --git a/src/classes/IoTSensor.cpp b/src/classes/IoTSensor.cpp index fa253b5c..3781d4ec 100644 --- a/src/classes/IoTSensor.cpp +++ b/src/classes/IoTSensor.cpp @@ -5,9 +5,22 @@ IoTSensor::IoTSensor(String parameters) { - _interval = jsonReadInt(parameters, "int") * 1000; - _subtype = jsonReadStr(parameters, "subtype"); - _id = jsonReadStr(parameters, "id"); + jsonRead(parameters, "int", _interval); + _interval = _interval * 1000; + jsonRead(parameters, "subtype", _subtype); + jsonRead(parameters, "id", _id); + jsonRead(parameters, "multiply", _multiply); + jsonRead(parameters, "plus", _plus); + jsonRead(parameters, "round", _round); + + String map; + jsonRead(parameters, "map", map); + if (map != "") { + _map1 = selectFromMarkerToMarker(map, ",", 0).toInt(); + _map2 = selectFromMarkerToMarker(map, ",", 1).toInt(); + _map3 = selectFromMarkerToMarker(map, ",", 2).toInt(); + _map4 = selectFromMarkerToMarker(map, ",", 3).toInt(); + } } IoTSensor::~IoTSensor() {} @@ -29,10 +42,22 @@ void IoTSensor::loop() { } void IoTSensor::regEvent(String value, String consoleInfo = "") { - eventGen2(_id, String(value)); - jsonWriteStr(paramsFlashJson, _id, String(value)); - publishStatus(_id, String(value)); - SerialPrint("I", "Sensor " + consoleInfo, "'" + _id + "' data: " + String(value) + "'"); + eventGen2(_id, value); + jsonWriteStr(paramsFlashJson, _id, value); + publishStatus(_id, value); + SerialPrint("I", "Sensor " + consoleInfo, "'" + _id + "' data: " + value + "'"); +} + +void IoTSensor::regEvent(float value, String consoleInfo = "") { + if (_multiply) value = value * _multiply; + if (_plus) value = value + _multiply; + if (_round != 0) { + if (value > 0) value = (int)(value * _round + 0.5) / _round; + if (value < 0) value = (int)(value * _round - 0.5) / _round; + } + if (_map1 != _map2) value = map(value, _map1, _map2, _map3, _map4); + + regEvent((String)value, consoleInfo); } void IoTSensor::doByInterval() {} From a745d61e037c482918f66af28b35a06679ad7a87 Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 23:07:39 +0300 Subject: [PATCH 11/12] =?UTF-8?q?=D0=A0=D0=B5=D0=B3=D0=B8=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=B8=D1=80=D1=83=D0=B5=D0=BC=20=D0=BF=D0=BE=D0=BA=D0=B0?= =?UTF-8?q?=D0=B7=D0=B0=D1=82=D0=B5=D0=BB=D0=B8=20=D1=81=D0=B5=D0=BD=D1=81?= =?UTF-8?q?=D0=BE=D1=80=D0=B0=20AnalogAdc=20=D0=B2=20=D1=84=D0=BE=D1=80?= =?UTF-8?q?=D0=BC=D0=B0=D1=82=D0=B5=20float?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/AnalogAdc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/AnalogAdc.cpp b/src/modules/AnalogAdc.cpp index ae298f1d..2f3ecac7 100644 --- a/src/modules/AnalogAdc.cpp +++ b/src/modules/AnalogAdc.cpp @@ -34,7 +34,7 @@ class AnalogAdc : public IoTSensor { void doByInterval() { float value = analogRead(_pin); - regEvent((String)value, "AnalogAdc"); //обязательный вызов хотяб один + regEvent(value, "AnalogAdc"); //обязательный вызов хотяб один } //======================================================================================================= From 5f076d50e84c3739f340c6fb29fd1c5c15301f8c Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 23:22:53 +0300 Subject: [PATCH 12/12] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D1=8F=D0=B5=D0=BC=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D1=83?= =?UTF-8?q?=20=D1=81=20=D0=BE=D0=BF=D1=80=D0=B5=D0=B4=D0=B5=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=D0=BC=20=D0=BD=D0=B5=20=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=BD=D1=8B=D1=85=20=D0=BF=D0=BE=D0=BA=D0=B0=D0=B7=D0=B0=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D0=B5=D0=B9=20=D1=81=D0=B5=D0=BD=D1=81=D0=BE=D1=80?= =?UTF-8?q?=D0=B0=20Sht20h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/Sht20.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/Sht20.cpp b/src/modules/Sht20.cpp index e53994ff..6f30f78d 100644 --- a/src/modules/Sht20.cpp +++ b/src/modules/Sht20.cpp @@ -14,7 +14,7 @@ class Sht20t : public IoTSensor { void doByInterval() { sht->read(); float value = sht->getTemperature(); - if (value != -46.85) regEvent(value, "Sht20t"); + if (value > -46.85F) regEvent(value, "Sht20t"); else SerialPrint("E", "Sensor Sht20t", "Error"); }