Files
IoTManager/include/items/SensorDhtClass.h

80 lines
2.7 KiB
C
Raw Normal View History

2020-09-04 13:44:49 +03:00
#pragma once
2020-12-16 13:59:01 +01:00
#include "Consts.h"
#ifdef SensorDhtEnabled
2020-09-04 13:44:49 +03:00
#include <Arduino.h>
#include "Class/LineParsing.h"
#include "Global.h"
#include "items/SensorConvertingClass.h"
2020-09-17 22:49:55 +03:00
2020-09-04 15:35:35 +03:00
DHTesp dht;
2020-09-04 13:44:49 +03:00
class SensorDhtClass : public SensorConvertingClass {
public:
SensorDhtClass() : SensorConvertingClass(){};
void SensorDhtInit() {
2020-09-04 15:35:35 +03:00
if (_type == "dht11") {
dht.setup(_pin.toInt(), DHTesp::DHT11);
}
if (_type == "dht22") {
dht.setup(_pin.toInt(), DHTesp::DHT22);
}
2020-10-20 22:55:45 +03:00
sensorReadingMap10sec += _key + ",";
2020-09-04 13:44:49 +03:00
2020-09-04 15:35:35 +03:00
//to do если надо будет читать несколько dht
//dhtEnterCounter++;
2020-09-04 13:44:49 +03:00
//jsonWriteInt(configOptionJson, _key + "_num", dhtEnterCounter);
2020-09-04 15:35:35 +03:00
2020-09-04 13:44:49 +03:00
jsonWriteStr(configOptionJson, _key + "_map", _map);
jsonWriteStr(configOptionJson, _key + "_с", _c);
}
2020-09-04 15:35:35 +03:00
void SensorDhtReadTemp(String key) {
//to do если надо будет читать несколько dht
//int cnt = jsonReadInt(configOptionJson, key + "_num");
2020-09-04 13:44:49 +03:00
float value;
2020-09-04 15:35:35 +03:00
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);
2020-11-07 01:11:32 +03:00
eventGen2(key, String(valueFl));
2020-09-04 15:35:35 +03:00
jsonWriteStr(configLiveJson, key, String(valueFl));
2020-10-02 01:14:45 +03:00
publishStatus(key, String(valueFl));
2020-10-20 22:55:45 +03:00
SerialPrint("I", "Sensor", "'" + key + "' data: " + String(valueFl));
2020-09-04 15:35:35 +03:00
} else {
Serial.println("[E] sensor '" + key);
}
}
}
2020-09-04 13:44:49 +03:00
2020-09-04 15:35:35 +03:00
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") {
2020-09-04 13:44:49 +03:00
//value = this->mapping(key, value);
float valueFl = this->correction(key, value);
2020-11-07 01:11:32 +03:00
eventGen2(key, String(valueFl));
2020-09-04 13:44:49 +03:00
jsonWriteStr(configLiveJson, key, String(valueFl));
2020-10-02 01:14:45 +03:00
publishStatus(key, String(valueFl));
2020-10-20 22:55:45 +03:00
SerialPrint("I", "Sensor", "'" + key + "' data: " + String(valueFl));
2020-09-04 15:35:35 +03:00
} else {
Serial.println("[E] sensor '" + key);
2020-09-04 13:44:49 +03:00
}
}
}
};
2020-12-16 13:59:01 +01:00
extern SensorDhtClass mySensorDht;
#endif