Fix SHT3xd fails sometimes in 2024.4.0 (#6592)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
mrtoy-me 2024-04-23 09:16:55 +10:00 committed by Jesse Hills
parent fcd9e3cb5d
commit 33e9881830
No known key found for this signature in database
GPG Key ID: BEAAE804EFD8E83A
2 changed files with 35 additions and 6 deletions

View File

@ -6,9 +6,14 @@ namespace sht3xd {
static const char *const TAG = "sht3xd";
// use read serial number register with clock stretching disabled as per other SHT3XD_COMMAND registers
// which provides support for SHT85 sensor
// SHT85 does not support clock stretching and uses same registers as SHT3xd with clock stretching disabled
// https://sensirion.com/media/documents/E5762713/63D103C2/Sensirion_electronic_identification_code_SHT3x.pdf
// indicates two possible read serial number registers either with clock stretching enabled or disabled.
// Other SHT3XD_COMMAND registers use the clock stretching disabled register.
// To ensure compatibility, reading serial number using the register with clock stretching register enabled
// (used originally in this component) is tried first and if that fails the alternate register address
// with clock stretching disabled is read.
static const uint16_t SHT3XD_COMMAND_READ_SERIAL_NUMBER_CLOCK_STRETCHING = 0x3780;
static const uint16_t SHT3XD_COMMAND_READ_SERIAL_NUMBER = 0x3682;
static const uint16_t SHT3XD_COMMAND_READ_STATUS = 0xF32D;
@ -22,13 +27,19 @@ static const uint16_t SHT3XD_COMMAND_FETCH_DATA = 0xE000;
void SHT3XDComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up SHT3xD...");
uint16_t raw_serial_number[2];
if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER, raw_serial_number, 2)) {
this->mark_failed();
return;
if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER_CLOCK_STRETCHING, raw_serial_number, 2)) {
this->error_code_ = READ_SERIAL_STRETCHED_FAILED;
if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER, raw_serial_number, 2)) {
this->error_code_ = READ_SERIAL_FAILED;
this->mark_failed();
return;
}
}
this->serial_number_ = (uint32_t(raw_serial_number[0]) << 16) | uint32_t(raw_serial_number[1]);
if (!this->write_command(heater_enabled_ ? SHT3XD_COMMAND_HEATER_ENABLE : SHT3XD_COMMAND_HEATER_DISABLE)) {
this->error_code_ = WRITE_HEATER_MODE_FAILED;
this->mark_failed();
return;
}
@ -36,10 +47,21 @@ void SHT3XDComponent::setup() {
void SHT3XDComponent::dump_config() {
ESP_LOGCONFIG(TAG, "SHT3xD:");
switch (this->error_code_) {
case READ_SERIAL_FAILED:
ESP_LOGD(TAG, " Error reading serial number");
break;
case WRITE_HEATER_MODE_FAILED:
ESP_LOGD(TAG, " Error writing heater mode");
break;
default:
break;
}
if (this->is_failed()) {
ESP_LOGE(TAG, " Communication with SHT3xD failed!");
return;
}
ESP_LOGD(TAG, " Setup successful");
ESP_LOGD(TAG, " Serial Number: 0x%08" PRIX32, this->serial_number_);
ESP_LOGD(TAG, " Heater Enabled: %s", this->heater_enabled_ ? "true" : "false");

View File

@ -22,6 +22,13 @@ class SHT3XDComponent : public PollingComponent, public sensirion_common::Sensir
void set_heater_enabled(bool heater_enabled) { heater_enabled_ = heater_enabled; }
protected:
enum ErrorCode {
NONE = 0,
READ_SERIAL_STRETCHED_FAILED,
READ_SERIAL_FAILED,
WRITE_HEATER_MODE_FAILED,
} error_code_{NONE};
sensor::Sensor *temperature_sensor_{nullptr};
sensor::Sensor *humidity_sensor_{nullptr};
bool heater_enabled_{true};