102 lines
3.1 KiB
C++
102 lines
3.1 KiB
C++
#include "feature_dht.h"
|
|
|
|
// esp-idf includes
|
|
#include <esp_log.h>
|
|
|
|
// 3rdparty lib includes
|
|
#include <fmt/core.h>
|
|
#include <DHT.h>
|
|
|
|
// local includes
|
|
#include "myconfig.h"
|
|
#include "mymqtt.h"
|
|
#include "delayedconstruction.h"
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
namespace deckenlampe {
|
|
std::optional<DhtValue> lastDhtValue;
|
|
espchrono::millis_clock::time_point last_dht11_temperature_pub;
|
|
espchrono::millis_clock::time_point last_dht11_humidity_pub;
|
|
bool dhtInitialized{};
|
|
|
|
namespace {
|
|
constexpr const char * const TAG = "DHT";
|
|
|
|
cpputils::DelayedConstruction<DHT> dht;
|
|
espchrono::millis_clock::time_point last_dht11_readout;
|
|
} // namespace
|
|
|
|
void init_dht()
|
|
{
|
|
if (!config::enable_dht.value())
|
|
return;
|
|
|
|
dht.construct(config::pins_dht.value(), config::dht_11_or_22.value() ? DHT22 : DHT11);
|
|
|
|
ESP_LOGI(TAG, "calling dht.begin()...");
|
|
dhtInitialized = dht->begin();
|
|
ESP_LOGI(TAG, "finished with %s", dhtInitialized ? "true" : "false");
|
|
}
|
|
|
|
void update_dht()
|
|
{
|
|
if (!config::enable_dht.value())
|
|
return;
|
|
|
|
if (dhtInitialized)
|
|
{
|
|
if (espchrono::ago(last_dht11_readout) < 5s)
|
|
return;
|
|
|
|
last_dht11_readout = espchrono::millis_clock::now();
|
|
|
|
if (const auto data = dht->read())
|
|
{
|
|
DhtValue dhtValue {
|
|
.timestamp = espchrono::millis_clock::now(),
|
|
.temperature = dht->readTemperature(*data),
|
|
.humidity = dht->readHumidity(*data)
|
|
};
|
|
|
|
ESP_LOGI(TAG, "read dht temperature: %.1f C", dhtValue.temperature);
|
|
ESP_LOGI(TAG, "read dht humidity: %.1f %%", dhtValue.humidity);
|
|
|
|
if (mqttConnected)
|
|
{
|
|
if (!lastDhtValue)
|
|
mqttVerbosePub(config::topic_dht11_availability.value(), "online", 0, 1);
|
|
if (!lastDhtValue || espchrono::ago(last_dht11_temperature_pub) >= config::valueUpdateInterval.value())
|
|
{
|
|
if (mqttVerbosePub(config::topic_dht11_temperature.value(), fmt::format("{:.1f}", dhtValue.temperature), 0, 1) >= 0)
|
|
last_dht11_temperature_pub = espchrono::millis_clock::now();
|
|
}
|
|
if (!lastDhtValue || espchrono::ago(last_dht11_humidity_pub) >= config::valueUpdateInterval.value())
|
|
{
|
|
if (mqttVerbosePub(config::topic_dht11_humidity.value(), fmt::format("{:.1f}", dhtValue.humidity), 0, 1) >= 0)
|
|
last_dht11_humidity_pub = espchrono::millis_clock::now();
|
|
}
|
|
}
|
|
|
|
lastDhtValue = dhtValue;
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGW(TAG, "dht failed");
|
|
goto dhtOffline;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dhtOffline:
|
|
if (lastDhtValue && espchrono::ago(lastDhtValue->timestamp) >= config::availableTimeoutTime.value())
|
|
{
|
|
ESP_LOGW(TAG, "dht timeouted");
|
|
if (mqttConnected)
|
|
mqttVerbosePub(config::topic_dht11_availability.value(), "offline", 0, 1);
|
|
lastDhtValue = std::nullopt;
|
|
}
|
|
}
|
|
}
|
|
} // namespace deckenlampe
|