[sht4x] Read and store a serial number of SHT4x sensors (#12089)

Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com>
Co-authored-by: J. Nick Koston <nick+github@koston.org>
This commit is contained in:
Nikolai Ryzhkov
2025-11-25 19:27:35 +01:00
committed by GitHub
parent 6ca0cd1e8b
commit dec323e786
2 changed files with 24 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ namespace sht4x {
static const char *const TAG = "sht4x";
static const uint8_t MEASURECOMMANDS[] = {0xFD, 0xF6, 0xE0};
static const uint8_t SERIAL_NUMBER_COMMAND = 0x89;
void SHT4XComponent::start_heater_() {
uint8_t cmd[] = {MEASURECOMMANDS[this->heater_command_]};
@@ -17,6 +18,17 @@ void SHT4XComponent::start_heater_() {
}
}
void SHT4XComponent::read_serial_number_() {
uint16_t buffer[2];
if (!this->get_8bit_register(SERIAL_NUMBER_COMMAND, buffer, 2, 1)) {
ESP_LOGE(TAG, "Get serial number failed");
this->serial_number_ = 0;
return;
}
this->serial_number_ = (uint32_t(buffer[0]) << 16) | (uint32_t(buffer[1]));
ESP_LOGD(TAG, "Serial number: %08" PRIx32, this->serial_number_);
}
void SHT4XComponent::setup() {
auto err = this->write(nullptr, 0);
if (err != i2c::ERROR_OK) {
@@ -24,6 +36,8 @@ void SHT4XComponent::setup() {
return;
}
this->read_serial_number_();
if (std::isfinite(this->duty_cycle_) && this->duty_cycle_ > 0.0f) {
uint32_t heater_interval = static_cast<uint32_t>(static_cast<uint16_t>(this->heater_time_) / this->duty_cycle_);
ESP_LOGD(TAG, "Heater interval: %" PRIu32, heater_interval);
@@ -54,11 +68,18 @@ void SHT4XComponent::setup() {
}
void SHT4XComponent::dump_config() {
ESP_LOGCONFIG(TAG, "SHT4x:");
ESP_LOGCONFIG(TAG,
"SHT4x:\n"
" Serial number: %08" PRIx32,
this->serial_number_);
LOG_I2C_DEVICE(this);
if (this->is_failed()) {
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
}
if (this->serial_number_ == 0) {
ESP_LOGW(TAG, "Get serial number failed");
}
}
void SHT4XComponent::update() {

View File

@@ -36,7 +36,9 @@ class SHT4XComponent : public PollingComponent, public sensirion_common::Sensiri
float duty_cycle_;
void start_heater_();
void read_serial_number_();
uint8_t heater_command_;
uint32_t serial_number_;
sensor::Sensor *temp_sensor_{nullptr};
sensor::Sensor *humidity_sensor_{nullptr};