mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-03-21 17:58:38 +05:00
feat: Added support DHT11/DHT22 sensors
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include <unordered_map>
|
||||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
#include <esp32DHT.h>
|
||||
|
||||
#if USE_BLE
|
||||
#include <NimBLEDevice.h>
|
||||
@@ -50,19 +51,27 @@ protected:
|
||||
class SensorsTask : public LeanTask {
|
||||
public:
|
||||
SensorsTask(bool _enabled = false, unsigned long _interval = 0) : LeanTask(_enabled, _interval) {
|
||||
// OneWire
|
||||
this->owInstances.reserve(2);
|
||||
this->dallasInstances.reserve(2);
|
||||
this->dallasSearchTime.reserve(2);
|
||||
this->dallasPolling.reserve(2);
|
||||
this->dallasLastPollingTime.reserve(2);
|
||||
|
||||
// DHT
|
||||
this->dhtLastPollingTime.reserve(2);
|
||||
}
|
||||
|
||||
~SensorsTask() {
|
||||
// OneWire
|
||||
this->dallasInstances.clear();
|
||||
this->owInstances.clear();
|
||||
this->dallasSearchTime.clear();
|
||||
this->dallasPolling.clear();
|
||||
this->dallasLastPollingTime.clear();
|
||||
|
||||
// DHT
|
||||
this->dhtLastPollingTime.clear();
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -70,17 +79,26 @@ protected:
|
||||
const unsigned int wirelessDisconnectTimeout = 600000u;
|
||||
const unsigned short dallasSearchInterval = 60000;
|
||||
const unsigned short dallasPollingInterval = 10000;
|
||||
const unsigned short dhtPollingInterval = 15000;
|
||||
const unsigned short globalPollingInterval = 15000;
|
||||
#if USE_BLE
|
||||
const unsigned int bleSetDtInterval = 7200000;
|
||||
#endif
|
||||
|
||||
// OneWire
|
||||
std::unordered_map<uint8_t, OneWire> owInstances;
|
||||
std::unordered_map<uint8_t, DallasTemperature> dallasInstances;
|
||||
std::unordered_map<uint8_t, unsigned long> dallasSearchTime;
|
||||
std::unordered_map<uint8_t, bool> dallasPolling;
|
||||
std::unordered_map<uint8_t, unsigned long> dallasLastPollingTime;
|
||||
|
||||
// DHT
|
||||
DHT dhtInstance;
|
||||
bool dhtIsPolling = false;
|
||||
std::unordered_map<uint8_t, unsigned long> dhtLastPollingTime;
|
||||
|
||||
#if USE_BLE
|
||||
// Bluetooth
|
||||
std::unordered_map<uint8_t, NimBLEClient*> bleClients;
|
||||
std::unordered_map<uint8_t, bool> bleSubscribed;
|
||||
std::unordered_map<uint8_t, unsigned long> bleLastSetDtTime;
|
||||
@@ -116,6 +134,9 @@ protected:
|
||||
this->yield();
|
||||
}
|
||||
|
||||
pollingDhtSensors();
|
||||
this->yield();
|
||||
|
||||
if (millis() - this->globalLastPollingTime > this->globalPollingInterval) {
|
||||
cleanDallasInstances();
|
||||
makeDallasInstances();
|
||||
@@ -399,6 +420,95 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
void pollingDhtSensors() {
|
||||
if (this->dhtIsPolling) {
|
||||
// busy
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint8_t sensorId = 0; sensorId <= Sensors::getMaxSensorId(); sensorId++) {
|
||||
auto& sSensor = Sensors::settings[sensorId];
|
||||
|
||||
if (!sSensor.enabled || sSensor.purpose == Sensors::Purpose::NOT_CONFIGURED) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sSensor.type != Sensors::Type::DHT11 && sSensor.type != Sensors::Type::DHT22) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this->dhtLastPollingTime.count(sSensor.gpio) && millis() - this->dhtLastPollingTime[sSensor.gpio] < this->dhtPollingInterval) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto sensorGpio = static_cast<gpio_num_t>(sSensor.gpio);
|
||||
if (this->dhtInstance.getGpio() != sensorGpio) {
|
||||
this->dhtInstance.reset();
|
||||
this->dhtInstance.onData([this, sensorId](float humidity, float temperature) {
|
||||
auto& sSensor = Sensors::settings[sensorId];
|
||||
|
||||
Log.straceln(
|
||||
FPSTR(L_SENSORS_DHT), F("GPIO %hhu, sensor #%hhu '%s', temp: %.2f, humidity: %.2f%%"),
|
||||
sSensor.gpio, sensorId, sSensor.name, temperature, humidity
|
||||
);
|
||||
|
||||
// set temperature
|
||||
Sensors::setValueById(sensorId, temperature, Sensors::ValueType::TEMPERATURE, true, true);
|
||||
|
||||
// set humidity
|
||||
Sensors::setValueById(sensorId, humidity, Sensors::ValueType::HUMIDITY, true, true);
|
||||
|
||||
auto& rSensor = Sensors::results[sensorId];
|
||||
if (rSensor.signalQuality < 100) {
|
||||
rSensor.signalQuality++;
|
||||
}
|
||||
|
||||
this->dhtLastPollingTime[sSensor.gpio] = millis();
|
||||
this->dhtIsPolling = false;
|
||||
});
|
||||
|
||||
this->dhtInstance.onError([this, sensorId](DHT::Status status) {
|
||||
auto& sSensor = Sensors::settings[sensorId];
|
||||
|
||||
Log.swarningln(
|
||||
FPSTR(L_SENSORS_DHT), F("GPIO %hhu, sensor #%hhu '%s': failed receiving data (err: %s)"),
|
||||
sSensor.gpio, sensorId, sSensor.name, DHT::statusToString(this->dhtInstance.getStatus())
|
||||
);
|
||||
|
||||
auto& rSensor = Sensors::results[sensorId];
|
||||
if (rSensor.signalQuality > 0) {
|
||||
rSensor.signalQuality--;
|
||||
}
|
||||
|
||||
this->dhtLastPollingTime[sSensor.gpio] = millis();
|
||||
this->dhtIsPolling = false;
|
||||
});
|
||||
|
||||
DHT::Type sType;
|
||||
if (sSensor.type == Sensors::Type::DHT11) {
|
||||
sType = DHT::Type::DHT11;
|
||||
|
||||
} else if (sSensor.type == Sensors::Type::DHT11) {
|
||||
sType = DHT::Type::DHT22;
|
||||
|
||||
} else {
|
||||
sType = DHT::Type::DHT22;
|
||||
}
|
||||
|
||||
if (this->dhtInstance.setup(sensorGpio, sType)) {
|
||||
Log.sinfoln(FPSTR(L_SENSORS_DHT), F("Started on GPIO %hhu"), sSensor.gpio);
|
||||
|
||||
} else {
|
||||
Log.swarningln(FPSTR(L_SENSORS_DHT), F("Failed to start on GPIO %hhu"), sSensor.gpio);
|
||||
}
|
||||
}
|
||||
|
||||
this->dhtIsPolling = this->dhtInstance.poll();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void pollingNtcSensors() {
|
||||
for (uint8_t sensorId = 0; sensorId <= Sensors::getMaxSensorId(); sensorId++) {
|
||||
auto& sSensor = Sensors::settings[sensorId];
|
||||
|
||||
Reference in New Issue
Block a user