From d19ebe8bfc732713da9130fb96bc652779e2d663 Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 22:50:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D1=80=D1=82=D0=B8=D1=80=D1=83?= =?UTF-8?q?=D0=B5=D0=BC=20=D1=81=D0=B5=D0=BD=D1=81=D0=BE=D1=80=D1=8B=20Dht?= =?UTF-8?q?11=20=D0=B8=20Dht22=20=D0=BE=D0=B4=D0=BD=D0=B8=D0=BC=20=D0=BC?= =?UTF-8?q?=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; + } +}