Files
IoTManager/include/items/SensorDhtClass.h

78 lines
2.7 KiB
C
Raw Normal View History

2020-09-04 13:44:49 +03:00
#pragma once
#include <Arduino.h>
2020-09-04 15:35:35 +03:00
2020-09-04 13:44:49 +03:00
#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-09-04 13:44:49 +03:00
sensorReadingMap += _key + ",";
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);
eventGen(key, "");
jsonWriteStr(configLiveJson, key, String(valueFl));
MqttClient::publishStatus(key, String(valueFl));
2020-09-17 17:27:44 +03:00
Serial.println("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);
eventGen(key, "");
jsonWriteStr(configLiveJson, key, String(valueFl));
MqttClient::publishStatus(key, String(valueFl));
2020-09-17 17:27:44 +03:00
Serial.println("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
}
}
}
};
extern SensorDhtClass mySensorDht;