onewire for esp32 improved, retry for sensors

This commit is contained in:
MichaelDvP
2020-08-02 08:54:30 +02:00
parent e735ac0338
commit 91573f5594
4 changed files with 39 additions and 15 deletions

View File

@@ -72,7 +72,6 @@ void Sensors::loop() {
YIELD;
bus_.skip();
bus_.write(CMD_CONVERT_TEMP);
state_ = State::READING;
} else {
// no sensors found
@@ -86,20 +85,15 @@ void Sensors::loop() {
// LOG_DEBUG(F("Scanning for sensors")); // uncomment for debug
bus_.reset_search();
found_.clear();
state_ = State::SCANNING;
last_activity_ = time_now;
state_ = State::SCANNING;
} else if (time_now - last_activity_ > READ_TIMEOUT_MS) {
LOG_ERROR(F("Sensor read timeout"));
state_ = State::IDLE;
last_activity_ = time_now;
state_ = State::IDLE;
}
} else if (state_ == State::SCANNING) {
if (time_now - last_activity_ > SCAN_TIMEOUT_MS) {
LOG_ERROR(F("Sensor scan timeout"));
state_ = State::IDLE;
last_activity_ = time_now;
state_ = State::IDLE;
} else {
uint8_t addr[ADDR_LEN] = {0};
@@ -133,11 +127,15 @@ void Sensors::loop() {
}
} else {
bus_.depower();
devices_ = std::move(found_);
if ((found_.size() >= devices_.size()) || (retrycnt_ > 5)) {
devices_ = std::move(found_);
retrycnt_ = 0;
} else {
retrycnt_++;
}
found_.clear();
// LOG_DEBUG(F("Found %zu sensor(s). Adding them."), devices_.size()); // uncomment for debug
state_ = State::IDLE;
last_activity_ = time_now;
state_ = State::IDLE;
}
}
}

View File

@@ -90,7 +90,7 @@ class Sensors {
static constexpr uint32_t READ_INTERVAL_MS = 5000; // 5 seconds
static constexpr uint32_t CONVERSION_MS = 1000; // 1 seconds
static constexpr uint32_t READ_TIMEOUT_MS = 2000; // 2 seconds
static constexpr uint32_t SCAN_TIMEOUT_MS = 30000; // 30 seconds
static constexpr uint32_t SCAN_TIMEOUT_MS = 3000; // 3 seconds
static constexpr uint8_t CMD_CONVERT_TEMP = 0x44;
static constexpr uint8_t CMD_READ_SCRATCHPAD = 0xBE;
@@ -111,6 +111,8 @@ class Sensors {
std::vector<Device> devices_;
uint8_t mqtt_format_;
uint8_t retrycnt_ = 0;
};
} // namespace emsesp