mirror of
https://github.com/esphome/esphome.git
synced 2026-06-02 03:02:19 +08:00
[sht3xd] Allow sensors that don't support serial number read (#14224)
Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com>
This commit is contained in:
@@ -12,6 +12,8 @@ static const char *const TAG = "sht3xd";
|
|||||||
// To ensure compatibility, reading serial number using the register with clock stretching register enabled
|
// 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
|
// (used originally in this component) is tried first and if that fails the alternate register address
|
||||||
// with clock stretching disabled is read.
|
// with clock stretching disabled is read.
|
||||||
|
// If both fail (e.g. some clones don't support the command), we continue so temp/humidity still work.
|
||||||
|
// Second attempt uses 10ms delay for boards that need more time before read (max permitted by ESPHome guidelines).
|
||||||
|
|
||||||
static const uint16_t SHT3XD_COMMAND_READ_SERIAL_NUMBER_CLOCK_STRETCHING = 0x3780;
|
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_SERIAL_NUMBER = 0x3682;
|
||||||
@@ -25,49 +27,28 @@ static const uint16_t SHT3XD_COMMAND_POLLING_H = 0x2400;
|
|||||||
static const uint16_t SHT3XD_COMMAND_FETCH_DATA = 0xE000;
|
static const uint16_t SHT3XD_COMMAND_FETCH_DATA = 0xE000;
|
||||||
|
|
||||||
void SHT3XDComponent::setup() {
|
void SHT3XDComponent::setup() {
|
||||||
uint16_t raw_serial_number[2];
|
uint16_t raw_serial_number[2]{0};
|
||||||
if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER_CLOCK_STRETCHING, raw_serial_number, 2)) {
|
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, 10)) {
|
||||||
if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER, raw_serial_number, 2)) {
|
ESP_LOGW(TAG, "Serial number read failed, continuing without it (clone or non-standard sensor)");
|
||||||
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]);
|
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)) {
|
if (!this->write_command(this->heater_enabled_ ? SHT3XD_COMMAND_HEATER_ENABLE : SHT3XD_COMMAND_HEATER_DISABLE)) {
|
||||||
this->error_code_ = WRITE_HEATER_MODE_FAILED;
|
this->mark_failed(LOG_STR("Failed to set heater mode"));
|
||||||
this->mark_failed();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHT3XDComponent::dump_config() {
|
void SHT3XDComponent::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, "SHT3xD:");
|
ESP_LOGCONFIG(TAG,
|
||||||
switch (this->error_code_) {
|
"SHT3xD:\n"
|
||||||
case READ_SERIAL_FAILED:
|
" Serial Number: 0x%08" PRIX32 "\n"
|
||||||
ESP_LOGD(TAG, " Error reading serial number");
|
" Heater Enabled: %s",
|
||||||
break;
|
this->serial_number_, TRUEFALSE(this->heater_enabled_));
|
||||||
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,
|
|
||||||
" Serial Number: 0x%08" PRIX32 "\n"
|
|
||||||
" Heater Enabled: %s",
|
|
||||||
this->serial_number_, TRUEFALSE(this->heater_enabled_));
|
|
||||||
|
|
||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
|
|
||||||
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
|
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
|
||||||
LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
|
LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,6 @@
|
|||||||
#include "esphome/components/sensor/sensor.h"
|
#include "esphome/components/sensor/sensor.h"
|
||||||
#include "esphome/components/sensirion_common/i2c_sensirion.h"
|
#include "esphome/components/sensirion_common/i2c_sensirion.h"
|
||||||
|
|
||||||
#include <cinttypes>
|
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace sht3xd {
|
namespace sht3xd {
|
||||||
|
|
||||||
@@ -21,13 +19,6 @@ class SHT3XDComponent : public PollingComponent, public sensirion_common::Sensir
|
|||||||
void set_heater_enabled(bool heater_enabled) { heater_enabled_ = heater_enabled; }
|
void set_heater_enabled(bool heater_enabled) { heater_enabled_ = heater_enabled; }
|
||||||
|
|
||||||
protected:
|
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 *temperature_sensor_{nullptr};
|
||||||
sensor::Sensor *humidity_sensor_{nullptr};
|
sensor::Sensor *humidity_sensor_{nullptr};
|
||||||
bool heater_enabled_{true};
|
bool heater_enabled_{true};
|
||||||
|
|||||||
Reference in New Issue
Block a user