Skip to content

Commit

Permalink
fix dht discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
markirb committed Nov 10, 2023
1 parent f56f7d9 commit cbae9d7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions fs_src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ <h1 id="head">Sensor</h1>
<select id="unit" class="short">
<option id="unit_0" value="0">&deg;C</option>
<option id="unit_1" value="1">&deg;F</option>
<option id="unit_2" value="2">&percnt;</option>
</select>
</div>
<div class="form-control">
Expand Down
21 changes: 14 additions & 7 deletions src/DHT/shelly_dht_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/

#include "shelly_dht_sensor.hpp"

#include <cmath>
#include "mgos_hal.h"

namespace shelly {

Expand All @@ -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");
}

Expand Down
2 changes: 2 additions & 0 deletions src/Shelly1/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ void CreateComponents(std::vector<std::unique_ptr<Component>> *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()));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/shelly_hap_humidity_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -134,7 +134,7 @@ StatusOr<std::string> 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());
Expand Down

0 comments on commit cbae9d7

Please sign in to comment.