Files
IoTManager/include/items/SensorDhtClass.h
Dmitry Borisenko a258c8c1b6 Add DHT sensor
2020-09-04 15:35:35 +03:00

77 lines
2.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
#include <Arduino.h>
#include "Class/LineParsing.h"
#include "Global.h"
#include "items/SensorConvertingClass.h"
DHTesp dht;
class SensorDhtClass : public SensorConvertingClass {
public:
SensorDhtClass() : SensorConvertingClass(){};
void SensorDhtInit() {
if (_type == "dht11") {
dht.setup(_pin.toInt(), DHTesp::DHT11);
}
if (_type == "dht22") {
dht.setup(_pin.toInt(), DHTesp::DHT22);
}
sensorReadingMap += _key + ",";
//to do если надо будет читать несколько dht
//dhtEnterCounter++;
//jsonWriteInt(configOptionJson, _key + "_num", dhtEnterCounter);
jsonWriteStr(configOptionJson, _key + "_map", _map);
jsonWriteStr(configOptionJson, _key + "_с", _c);
}
void SensorDhtReadTemp(String key) {
//to do если надо будет читать несколько dht
//int cnt = jsonReadInt(configOptionJson, key + "_num");
float value;
static int counter;
if (dht.getStatus() != 0 && counter < 5) {
counter++;
//return;
} else {
counter = 0;
value = dht.getTemperature();
if (String(value) != "nan") {
//value = this->mapping(key, value);
float valueFl = this->correction(key, value);
eventGen(key, "");
jsonWriteStr(configLiveJson, key, String(valueFl));
MqttClient::publishStatus(key, String(valueFl));
Serial.println("[I] sensor '" + key + "' data: " + String(valueFl));
} else {
Serial.println("[E] sensor '" + key);
}
}
}
void SensorDhtReadHum(String key) {
//to do если надо будет читать несколько dht
//int cnt = jsonReadInt(configOptionJson, key + "_num");
float value;
static int counter;
if (dht.getStatus() != 0 && counter < 5) {
counter++;
//return;
} else {
counter = 0;
value = dht.getHumidity();
if (String(value) != "nan") {
//value = this->mapping(key, value);
float valueFl = this->correction(key, value);
eventGen(key, "");
jsonWriteStr(configLiveJson, key, String(valueFl));
MqttClient::publishStatus(key, String(valueFl));
Serial.println("[I] sensor '" + key + "' data: " + String(valueFl));
} else {
Serial.println("[E] sensor '" + key);
}
}
}
};
extern SensorDhtClass mySensorDht;