Fix SHTC3 sensor detection (#3365)

Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
Samuel Sieb 2022-04-10 13:42:31 -07:00 committed by GitHub
parent a9e653724c
commit 3297267a16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 12 deletions

View File

@ -35,15 +35,17 @@ void SHTCXComponent::setup() {
return; return;
} }
uint16_t device_id_register[1]; uint16_t device_id_register;
if (!this->read_data_(device_id_register, 1)) { if (!this->read_data_(&device_id_register, 1)) {
ESP_LOGE(TAG, "Error reading Device ID"); ESP_LOGE(TAG, "Error reading Device ID");
this->mark_failed(); this->mark_failed();
return; return;
} }
if (((device_id_register[0] << 2) & 0x1C) == 0x1C) { this->sensor_id_ = device_id_register;
if ((device_id_register[0] & 0x847) == 0x847) {
if ((device_id_register & 0x3F) == 0x07) {
if (device_id_register & 0x800) {
this->type_ = SHTCX_TYPE_SHTC3; this->type_ = SHTCX_TYPE_SHTC3;
} else { } else {
this->type_ = SHTCX_TYPE_SHTC1; this->type_ = SHTCX_TYPE_SHTC1;
@ -51,11 +53,11 @@ void SHTCXComponent::setup() {
} else { } else {
this->type_ = SHTCX_TYPE_UNKNOWN; 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() { void SHTCXComponent::dump_config() {
ESP_LOGCONFIG(TAG, "SHTCx:"); 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); LOG_I2C_DEVICE(this);
if (this->is_failed()) { if (this->is_failed()) {
ESP_LOGE(TAG, "Communication with SHTCx failed!"); ESP_LOGE(TAG, "Communication with SHTCx failed!");
@ -75,21 +77,28 @@ void SHTCXComponent::update() {
this->wake_up(); this->wake_up();
} }
if (!this->write_command_(SHTCX_COMMAND_POLLING_H)) { 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(); this->status_set_warning();
return; return;
} }
this->set_timeout(50, [this]() { this->set_timeout(50, [this]() {
float temperature = NAN;
float humidity = NAN;
uint16_t raw_data[2]; uint16_t raw_data[2];
if (!this->read_data_(raw_data, 2)) { if (!this->read_data_(raw_data, 2)) {
ESP_LOGE(TAG, "sensor read failed");
this->status_set_warning(); 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;
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); ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity);
}
if (this->temperature_sensor_ != nullptr) if (this->temperature_sensor_ != nullptr)
this->temperature_sensor_->publish_state(temperature); this->temperature_sensor_->publish_state(temperature);
if (this->humidity_sensor_ != nullptr) if (this->humidity_sensor_ != nullptr)

View File

@ -27,6 +27,7 @@ class SHTCXComponent : public PollingComponent, public i2c::I2CDevice {
bool write_command_(uint16_t command); bool write_command_(uint16_t command);
bool read_data_(uint16_t *data, uint8_t len); bool read_data_(uint16_t *data, uint8_t len);
SHTCXType type_; SHTCXType type_;
uint16_t sensor_id_;
sensor::Sensor *temperature_sensor_; sensor::Sensor *temperature_sensor_;
sensor::Sensor *humidity_sensor_; sensor::Sensor *humidity_sensor_;
}; };