diff --git a/esphome/components/shtcx/shtcx.cpp b/esphome/components/shtcx/shtcx.cpp index 867c26df1d..4112270c02 100644 --- a/esphome/components/shtcx/shtcx.cpp +++ b/esphome/components/shtcx/shtcx.cpp @@ -35,15 +35,17 @@ void SHTCXComponent::setup() { return; } - uint16_t device_id_register[1]; - if (!this->read_data_(device_id_register, 1)) { + uint16_t device_id_register; + if (!this->read_data_(&device_id_register, 1)) { ESP_LOGE(TAG, "Error reading Device ID"); this->mark_failed(); return; } - if (((device_id_register[0] << 2) & 0x1C) == 0x1C) { - if ((device_id_register[0] & 0x847) == 0x847) { + this->sensor_id_ = device_id_register; + + if ((device_id_register & 0x3F) == 0x07) { + if (device_id_register & 0x800) { this->type_ = SHTCX_TYPE_SHTC3; } else { this->type_ = SHTCX_TYPE_SHTC1; @@ -51,11 +53,11 @@ void SHTCXComponent::setup() { } else { this->type_ = SHTCX_TYPE_UNKNOWN; } - ESP_LOGCONFIG(TAG, " Device identified: %s", to_string(this->type_)); + ESP_LOGCONFIG(TAG, " Device identified: %s (%04x)", to_string(this->type_), device_id_register); } void SHTCXComponent::dump_config() { ESP_LOGCONFIG(TAG, "SHTCx:"); - ESP_LOGCONFIG(TAG, " Model: %s", to_string(this->type_)); + ESP_LOGCONFIG(TAG, " Model: %s (%04x)", to_string(this->type_), this->sensor_id_); LOG_I2C_DEVICE(this); if (this->is_failed()) { ESP_LOGE(TAG, "Communication with SHTCx failed!"); @@ -75,21 +77,28 @@ void SHTCXComponent::update() { this->wake_up(); } if (!this->write_command_(SHTCX_COMMAND_POLLING_H)) { + ESP_LOGE(TAG, "sensor polling failed"); + if (this->temperature_sensor_ != nullptr) + this->temperature_sensor_->publish_state(NAN); + if (this->humidity_sensor_ != nullptr) + this->humidity_sensor_->publish_state(NAN); this->status_set_warning(); return; } this->set_timeout(50, [this]() { + float temperature = NAN; + float humidity = NAN; uint16_t raw_data[2]; if (!this->read_data_(raw_data, 2)) { + ESP_LOGE(TAG, "sensor read failed"); this->status_set_warning(); - return; + } else { + temperature = 175.0f * float(raw_data[0]) / 65536.0f - 45.0f; + humidity = 100.0f * float(raw_data[1]) / 65536.0f; + + ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity); } - - float temperature = 175.0f * float(raw_data[0]) / 65536.0f - 45.0f; - float humidity = 100.0f * float(raw_data[1]) / 65536.0f; - - ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity); if (this->temperature_sensor_ != nullptr) this->temperature_sensor_->publish_state(temperature); if (this->humidity_sensor_ != nullptr) diff --git a/esphome/components/shtcx/shtcx.h b/esphome/components/shtcx/shtcx.h index ccc6533bfa..cb2b46d348 100644 --- a/esphome/components/shtcx/shtcx.h +++ b/esphome/components/shtcx/shtcx.h @@ -27,6 +27,7 @@ class SHTCXComponent : public PollingComponent, public i2c::I2CDevice { bool write_command_(uint16_t command); bool read_data_(uint16_t *data, uint8_t len); SHTCXType type_; + uint16_t sensor_id_; sensor::Sensor *temperature_sensor_; sensor::Sensor *humidity_sensor_; };