Files
IoTManager/src/modules/Dht1122.cpp

79 lines
1.8 KiB
C++
Raw Normal View History

/******************************************************************
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/IoTItem.h"
#include "DHTesp.h"
#include <map>
std::map<int, DHTesp*> dhts;
class Dht1122t : public IoTItem {
private:
2022-02-02 11:05:59 +03:00
DHTesp* _dht;
public:
Dht1122t(DHTesp* dht, String parameters): IoTItem(parameters) {
2022-02-02 11:05:59 +03:00
_dht = dht;
}
void doByInterval() {
2022-02-02 11:05:59 +03:00
float value = _dht->getTemperature();
if (String(value) != "nan") regEvent(value, "Dht1122t");
else SerialPrint("E", "Sensor DHTt", "Error");
}
~Dht1122t();
};
class Dht1122h : public IoTItem {
private:
2022-02-02 11:05:59 +03:00
DHTesp* _dht;
public:
Dht1122h(DHTesp* dht, String parameters): IoTItem(parameters) {
2022-02-02 11:05:59 +03:00
_dht = dht;
}
void doByInterval() {
2022-02-02 11:05:59 +03:00
float value = _dht->getHumidity();
if (String(value) != "nan") regEvent(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()) {
2022-02-02 11:05:59 +03:00
dhts[pin] = new DHTesp();
if (senstype == "dht11") {
2022-02-02 11:05:59 +03:00
dhts[pin]->setup(pin, DHTesp::DHT11);
} else if (senstype == "dht22") {
2022-02-02 11:05:59 +03:00
dhts[pin]->setup(pin, DHTesp::DHT22);
}
}
if (subtype == F("Dht1122t")) {
2022-02-02 11:05:59 +03:00
return new Dht1122t(dhts[pin], param);
} else if (subtype == F("Dht1122h")) {
2022-02-02 11:05:59 +03:00
return new Dht1122h(dhts[pin], param);
} else {
return nullptr;
}
}