ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
sht4x.cpp
Go to the documentation of this file.
1#include "sht4x.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome::sht4x {
7
8ESPHOME_LOG_TAG(TAG, "sht4x");
9
10static const uint8_t MEASURECOMMANDS[] = {0xFD, 0xF6, 0xE0};
11static const uint8_t SERIAL_NUMBER_COMMAND = 0x89;
12
13// Conversion constants from SHT4x datasheet
14static constexpr float TEMPERATURE_OFFSET = -45.0f;
15static constexpr float TEMPERATURE_SPAN = 175.0f;
16static constexpr float HUMIDITY_OFFSET = -6.0f;
17static constexpr float HUMIDITY_SPAN = 125.0f;
18static constexpr float RAW_MAX = 65535.0f;
19
21 uint16_t buffer[2];
22 if (!this->get_8bit_register(SERIAL_NUMBER_COMMAND, buffer, 2, 10)) {
23 ESP_LOGE(TAG, "Get serial number failed");
24 this->serial_number_ = 0;
25 return;
26 }
27 this->serial_number_ = (uint32_t(buffer[0]) << 16) | (uint32_t(buffer[1]));
28 ESP_LOGD(TAG, "Serial number: %08" PRIx32, this->serial_number_);
29#ifdef USE_TEXT_SENSOR
30 if (this->serial_number_text_sensor_ != nullptr) {
31 char serial_number[format_hex_size(sizeof(this->serial_number_))];
32 format_hex_to(serial_number, this->serial_number_);
33 this->serial_number_text_sensor_->publish_state(serial_number);
34 }
35#endif
36}
37
39 auto err = this->write(nullptr, 0);
40 if (err != i2c::ERROR_OK) {
41 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
42 return;
43 }
44
45 this->read_serial_number_();
46
47 if (std::isfinite(this->duty_cycle_) && this->duty_cycle_ > 0.0f) {
48 this->heater_interval_ = static_cast<uint32_t>(static_cast<uint16_t>(this->heater_time_) / this->duty_cycle_);
49 ESP_LOGD(TAG, "Heater interval: %" PRIu32, this->heater_interval_);
50
53 this->heater_command_ = 0x39;
54 } else {
55 this->heater_command_ = 0x32;
56 }
57 } else if (this->heater_power_ == SHT4X_HEATERPOWER_MED) {
59 this->heater_command_ = 0x2F;
60 } else {
61 this->heater_command_ = 0x24;
62 }
63 } else {
65 this->heater_command_ = 0x1E;
66 } else {
67 this->heater_command_ = 0x15;
68 }
69 }
70 ESP_LOGD(TAG, "Heater command: %x", this->heater_command_);
71 }
72}
73
74void SHT4XComponent::dump_config() {
75 ESP_LOGCONFIG(TAG,
76 "SHT4x:\n"
77 " Serial number: %08" PRIx32,
78 this->serial_number_);
79
80 LOG_I2C_DEVICE(this);
81 if (this->serial_number_ == 0) {
82 ESP_LOGW(TAG, "Get serial number failed");
83 }
84}
85
86void SHT4XComponent::update() {
87 // Send command
88 if (!this->write_command(MEASURECOMMANDS[this->precision_])) {
89 // Warning will be printed only if warning status is not set yet
90 this->status_set_warning(LOG_STR("Failed to send measurement command"));
91 return;
92 }
93
94 this->set_timeout(10, [this]() {
95 uint16_t buffer[2];
96
97 // Read measurement
98 if (!this->status_comm_result(this->read_data(buffer, 2))) {
99 // Using ESP_LOGW to force the warning to be printed
100 ESP_LOGW(TAG, "Sensor read failed");
101 return;
102 }
103
104 // Evaluate and publish measurements
105 // Temperature is in the first result word, relative humidity in the second
106 PUBLISH_STATE(this->temp_sensor_, TEMPERATURE_OFFSET + TEMPERATURE_SPAN * static_cast<float>(buffer[0]) / RAW_MAX);
107 PUBLISH_STATE(this->humidity_sensor_, HUMIDITY_OFFSET + HUMIDITY_SPAN * static_cast<float>(buffer[1]) / RAW_MAX);
108
109 // Fire heater after measurement to maximize cooldown time before the next reading.
110 // The heater command produces a measurement that we don't need (datasheet 4.9).
111 if (this->heater_interval_ > 0) {
112 uint32_t now = millis();
113 if (now - this->last_heater_millis_ >= this->heater_interval_) {
114 ESP_LOGD(TAG, "Heater turning on");
115 if (this->write_command(this->heater_command_)) {
116 this->last_heater_millis_ = now;
117 }
118 }
119 }
120 });
121}
122
123} // namespace esphome::sht4x
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
bool get_8bit_register(uint8_t i2c_register, uint16_t *data, uint8_t len, uint8_t delay=0)
get data words from I2C register.
bool write_command(T i2c_register)
Write a command to the I2C device.
bool read_data(uint16_t *data, uint8_t len)
Read data words from I2C device.
SHT4XPRECISION precision_
Definition sht4x.h:38
SHT4XHEATERPOWER heater_power_
Definition sht4x.h:39
SHT4XHEATERTIME heater_time_
Definition sht4x.h:40
uint32_t buffer
Definition gif.cpp:50
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
@ SHT4X_HEATERPOWER_HIGH
Definition sht4x.h:16
@ SHT4X_HEATERPOWER_MED
Definition sht4x.h:16
ESPHOME_LOG_TAG(TAG, "sht4x")
@ SHT4X_HEATERTIME_LONG
Definition sht4x.h:18
constexpr size_t format_hex_size(size_t byte_count)
Calculate buffer size needed for format_hex_to: "XXXXXXXX...\0" = bytes * 2 + 1.
Definition helpers.h:1661
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
Definition helpers.cpp:428
static void uint32_t
char serial_number[10]
Definition sun_gtil2.cpp:15