From cbae9d77a16a1c497c7d4b9f42c344304cd2dd41 Mon Sep 17 00:00:00 2001 From: Markus Kirberg Date: Thu, 9 Nov 2023 21:55:33 +0100 Subject: [PATCH] fix dht discovery --- fs_src/index.html | 1 + src/DHT/shelly_dht_sensor.cpp | 21 ++++++++++++++------- src/Shelly1/shelly_init.cpp | 2 ++ src/shelly_hap_humidity_sensor.cpp | 4 ++-- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/fs_src/index.html b/fs_src/index.html index 6828093d..d27bce5e 100644 --- a/fs_src/index.html +++ b/fs_src/index.html @@ -766,6 +766,7 @@

Sensor

diff --git a/src/DHT/shelly_dht_sensor.cpp b/src/DHT/shelly_dht_sensor.cpp index 885babfe..0f5a1968 100644 --- a/src/DHT/shelly_dht_sensor.cpp +++ b/src/DHT/shelly_dht_sensor.cpp @@ -16,8 +16,8 @@ */ #include "shelly_dht_sensor.hpp" - #include +#include "mgos_hal.h" namespace shelly { @@ -36,17 +36,24 @@ Status DHTSensor::Init() { dht = mgos_dht_create_separate_io(pin_in_, pin_out_, DHT21); if (dht == NULL) { - return mgos::Errorf(STATUS_NOT_FOUND, "No DHT Sensor found"); + return mgos::Errorf(STATUS_NOT_FOUND, "dht sensor init unsuccesfull"); } - result_ = mgos_dht_get_temp(dht); - + size_t tries = + 2; // first read was observed to be not successfull, reason unknown mgos_dht_stats stats; - if (mgos_dht_getStats(dht, &stats)) { - if (stats.read == 1 && stats.read_success == 1) { - return Status::OK(); + for (size_t i = 0; i < tries; i++) { + result_ = mgos_dht_get_temp(dht); + if (mgos_dht_getStats(dht, &stats)) { + if (stats.read_success >= 1) { + return Status::OK(); + } else if (i != (tries - 1)) { + mgos_msleep(2 * + 1000); // try again after wait time of MGOs_DHT_READ_DELAY + } } } + return mgos::Errorf(STATUS_NOT_FOUND, "No DHT Sensor found"); } diff --git a/src/Shelly1/shelly_init.cpp b/src/Shelly1/shelly_init.cpp index c8dfb667..bd56511e 100644 --- a/src/Shelly1/shelly_init.cpp +++ b/src/Shelly1/shelly_init.cpp @@ -83,6 +83,8 @@ void CreateComponents(std::vector> *comps, if (status == Status::OK()) { sensors.push_back(std::move(dht)); dht_found = true; + } else { + LOG(LL_ERROR, ("dht init failed: %s", status.ToString().c_str())); } } diff --git a/src/shelly_hap_humidity_sensor.cpp b/src/shelly_hap_humidity_sensor.cpp index 7043d21b..de24af62 100644 --- a/src/shelly_hap_humidity_sensor.cpp +++ b/src/shelly_hap_humidity_sensor.cpp @@ -63,7 +63,7 @@ Status HumiditySensor::SetConfig(const std::string &config_json, return mgos::Errorf(STATUS_INVALID_ARGUMENT, "invalid %s", "name (too long, max 64)"); } - if (cfg.unit < 0 || cfg.unit > 1) { + if (cfg.unit != 2) { return mgos::Errorf(STATUS_INVALID_ARGUMENT, "invalid unit"); } if (cfg.update_interval < 1) { @@ -134,7 +134,7 @@ StatusOr HumiditySensor::GetInfoJSON() const { std::string res = mgos::JSONPrintStringf( "{id: %d, type: %d, name: %Q, unit: %d, " "update_interval: %d, ", - id(), type(), cfg_->name, cfg_->unit, cfg_->update_interval); + id(), type(), cfg_->name, 2, cfg_->update_interval); auto tempval = hum_sensor_->GetHumidity(); if (tempval.ok()) { mgos::JSONAppendStringf(&res, "value: %.1f", tempval.ValueOrDie());