diff --git a/lib/OneWire/OneWire.cpp b/lib/OneWire/OneWire.cpp index 5828e804d..cd9dc8448 100644 --- a/lib/OneWire/OneWire.cpp +++ b/lib/OneWire/OneWire.cpp @@ -161,7 +161,11 @@ void OneWire::begin(uint8_t pin) { // // Returns 1 if a device asserted a presence pulse, 0 otherwise. // +#ifdef ARDUINO_ARCH_ESP32 +uint8_t IRAM_ATTR OneWire::reset(void) { +#else uint8_t OneWire::reset(void) { +#endif IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask; volatile IO_REG_TYPE * reg IO_REG_BASE_ATTR = baseReg; uint8_t r; @@ -195,7 +199,11 @@ uint8_t OneWire::reset(void) { // Write a bit. Port and bit is used to cut lookup time and provide // more certain timing. // +#ifdef ARDUINO_ARCH_ESP32 +void IRAM_ATTR OneWire::write_bit(uint8_t v) { +#else void OneWire::write_bit(uint8_t v) { +#endif IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask; volatile IO_REG_TYPE * reg IO_REG_BASE_ATTR = baseReg; @@ -222,7 +230,11 @@ void OneWire::write_bit(uint8_t v) { // Read a bit. Port and bit is used to cut lookup time and provide // more certain timing. // +#ifdef ARDUINO_ARCH_ESP32 +uint8_t IRAM_ATTR OneWire::read_bit(void) { +#else uint8_t OneWire::read_bit(void) { +#endif IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask; volatile IO_REG_TYPE * reg IO_REG_BASE_ATTR = baseReg; uint8_t r; @@ -473,6 +485,7 @@ bool OneWire::search(uint8_t * newAddr, bool search_mode /* = true */) { for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i]; } + // depower(); // https://github.com/PaulStoffregen/OneWire/pull/80 return search_result; } diff --git a/lib/OneWire/OneWire.h b/lib/OneWire/OneWire.h index 4b1933060..436eecd1f 100644 --- a/lib/OneWire/OneWire.h +++ b/lib/OneWire/OneWire.h @@ -78,7 +78,11 @@ class OneWire { // Perform a 1-Wire reset cycle. Returns 1 if a device responds // with a presence pulse. Returns 0 if there is no device or the // bus is shorted or otherwise held low for more than 250uS +#ifdef ARDUINO_ARCH_ESP32 + uint8_t IRAM_ATTR reset(void); +#else uint8_t reset(void); +#endif // Issue a 1-Wire rom select command, you do the reset first. void select(const uint8_t rom[8]); @@ -101,11 +105,18 @@ class OneWire { // Write a bit. The bus is always left powered at the end, see // note in write() about that. - void write_bit(uint8_t v); + #ifdef ARDUINO_ARCH_ESP32 + void IRAM_ATTR write_bit(uint8_t v); + #else + void write_bit(uint8_t v); +#endif // Read a bit. +#ifdef ARDUINO_ARCH_ESP32 + uint8_t IRAM_ATTR read_bit(void); +#else uint8_t read_bit(void); - +#endif // Stop forcing power onto the bus. You only need to do this if // you used the 'power' flag to write() or used a write_bit() call // and aren't about to do another read or write. You would rather diff --git a/src/devices/thermostat.cpp b/src/devices/thermostat.cpp index ef61c55d5..3468abe06 100644 --- a/src/devices/thermostat.cpp +++ b/src/devices/thermostat.cpp @@ -141,8 +141,7 @@ Thermostat::Thermostat(uint8_t device_type, uint8_t device_id, uint8_t product_i // if we're on auto mode, register this thermostat if it has a device id of 0x10 or 0x17 // or if its the master thermostat we defined // see https://github.com/proddy/EMS-ESP/issues/362#issuecomment-629628161 - if (((num_devices == 1) && (actual_master_thermostat == EMSESP_DEFAULT_MASTER_THERMOSTAT) && ((device_id == 0x10) || (device_id == 0x17))) - || (master_thermostat == device_id)) { + if (((num_devices == 1) && (actual_master_thermostat == EMSESP_DEFAULT_MASTER_THERMOSTAT)) || (master_thermostat == device_id)) { EMSESP::actual_master_thermostat(device_id); LOG_DEBUG(F("Registering new thermostat with device ID 0x%02X (as master)"), device_id); init_mqtt(); @@ -427,8 +426,6 @@ void Thermostat::thermostat_cmd(const char * message) { } return; } - - // thermostat mode changes if (strcmp(command, "mode") == 0) { std::string mode = doc["data"]; if (mode.empty()) { @@ -437,7 +434,6 @@ void Thermostat::thermostat_cmd(const char * message) { set_mode(mode, hc_num); return; } - if (strcmp(command, "nighttemp") == 0) { float f = doc["data"]; if (f) { @@ -445,7 +441,6 @@ void Thermostat::thermostat_cmd(const char * message) { } return; } - if (strcmp(command, "daytemp") == 0) { float f = doc["data"]; if (f) { @@ -453,7 +448,6 @@ void Thermostat::thermostat_cmd(const char * message) { } return; } - if (strcmp(command, "holidaytemp") == 0) { float f = doc["data"]; if (f) { @@ -461,7 +455,6 @@ void Thermostat::thermostat_cmd(const char * message) { } return; } - if (strcmp(command, "ecotemp") == 0) { float f = doc["data"]; if (f) { @@ -469,7 +462,6 @@ void Thermostat::thermostat_cmd(const char * message) { } return; } - if (strcmp(command, "heattemp") == 0) { float f = doc["data"]; if (f) { @@ -477,7 +469,6 @@ void Thermostat::thermostat_cmd(const char * message) { } return; } - if (strcmp(command, "nofrosttemp") == 0) { float f = doc["data"]; if (f) { @@ -485,7 +476,6 @@ void Thermostat::thermostat_cmd(const char * message) { } return; } - if (strcmp(command, "summertemp") == 0) { float f = doc["data"]; if (f) { @@ -493,7 +483,6 @@ void Thermostat::thermostat_cmd(const char * message) { } return; } - if (strcmp(command, "designtemp") == 0) { float f = doc["data"]; if (f) { @@ -501,7 +490,6 @@ void Thermostat::thermostat_cmd(const char * message) { } return; } - if (strcmp(command, "offsettemp") == 0) { float f = doc["data"]; if (f) { @@ -509,6 +497,40 @@ void Thermostat::thermostat_cmd(const char * message) { } return; } + if (strcmp(command, "remotetemp") == 0) { + float f = doc["data"]; + if (f > 100 || f < 0) { + Roomctrl::set_remotetemp(hc_num - 1, EMS_VALUE_SHORT_NOTSET); + } else { + Roomctrl::set_remotetemp(hc_num - 1, (int16_t)(f * 10)); + } + return; + } + if (strcmp(command, "control") == 0) { + uint8_t ctrl = doc["data"]; + set_control(ctrl, hc_num); + return; + } + if (strcmp(command, "pause") == 0) { + uint8_t p = doc["data"]; + set_pause(p, hc_num); + return; + } + if (strcmp(command, "party") == 0) { + uint8_t p = doc["data"]; + set_party(p, hc_num); + return; + } + if (strcmp(command, "holiday") == 0) { + std::string holiday = doc["data"]; + set_holiday(holiday.c_str(), hc_num); + return; + } + if (strcmp(command, "date") == 0) { + std::string date = doc["data"]; + set_datetime(date.c_str()); + return; + } } void Thermostat::thermostat_cmd_temp(const char * message) { diff --git a/src/sensors.cpp b/src/sensors.cpp index 76bf98c39..26a0a1223 100644 --- a/src/sensors.cpp +++ b/src/sensors.cpp @@ -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; } } } diff --git a/src/sensors.h b/src/sensors.h index 07a1d0793..7ec37d6d3 100644 --- a/src/sensors.h +++ b/src/sensors.h @@ -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 devices_; uint8_t mqtt_format_; + uint8_t retrycnt_ = 0; + }; } // namespace emsesp