ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
sht4x.cpp
Go to the documentation of this file.
1#include "sht4x.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5namespace esphome::sht4x {
6
7static const char *const TAG = "sht4x";
8
9static const uint8_t MEASURECOMMANDS[] = {0xFD, 0xF6, 0xE0};
10static const uint8_t SERIAL_NUMBER_COMMAND = 0x89;
11
12// Conversion constants from SHT4x datasheet
13static constexpr float TEMPERATURE_OFFSET = -45.0f;
14static constexpr float TEMPERATURE_SPAN = 175.0f;
15static constexpr float HUMIDITY_OFFSET = -6.0f;
16static constexpr float HUMIDITY_SPAN = 125.0f;
17static constexpr float RAW_MAX = 65535.0f;
18
20 uint16_t buffer[2];
21 if (!this->get_8bit_register(SERIAL_NUMBER_COMMAND, buffer, 2, 1)) {
22 ESP_LOGE(TAG, "Get serial number failed");
23 this->serial_number_ = 0;
24 return;
25 }
26 this->serial_number_ = (uint32_t(buffer[0]) << 16) | (uint32_t(buffer[1]));
27 ESP_LOGD(TAG, "Serial number: %08" PRIx32, this->serial_number_);
28}
29
31 auto err = this->write(nullptr, 0);
32 if (err != i2c::ERROR_OK) {
33 this->mark_failed();
34 return;
35 }
36
37 this->read_serial_number_();
38
39 if (std::isfinite(this->duty_cycle_) && this->duty_cycle_ > 0.0f) {
40 this->heater_interval_ = static_cast<uint32_t>(static_cast<uint16_t>(this->heater_time_) / this->duty_cycle_);
41 ESP_LOGD(TAG, "Heater interval: %" PRIu32, this->heater_interval_);
42
45 this->heater_command_ = 0x39;
46 } else {
47 this->heater_command_ = 0x32;
48 }
49 } else if (this->heater_power_ == SHT4X_HEATERPOWER_MED) {
51 this->heater_command_ = 0x2F;
52 } else {
53 this->heater_command_ = 0x24;
54 }
55 } else {
57 this->heater_command_ = 0x1E;
58 } else {
59 this->heater_command_ = 0x15;
60 }
61 }
62 ESP_LOGD(TAG, "Heater command: %x", this->heater_command_);
63 }
64}
65
67 ESP_LOGCONFIG(TAG,
68 "SHT4x:\n"
69 " Serial number: %08" PRIx32,
70 this->serial_number_);
71
72 LOG_I2C_DEVICE(this);
73 if (this->is_failed()) {
74 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
75 }
76 if (this->serial_number_ == 0) {
77 ESP_LOGW(TAG, "Get serial number failed");
78 }
79}
80
82 // Send command
83 if (!this->write_command(MEASURECOMMANDS[this->precision_])) {
84 // Warning will be printed only if warning status is not set yet
85 this->status_set_warning(LOG_STR("Failed to send measurement command"));
86 return;
87 }
88
89 this->set_timeout(10, [this]() {
90 uint16_t buffer[2];
91
92 // Read measurement
93 if (!this->read_data(buffer, 2)) {
94 // Using ESP_LOGW to force the warning to be printed
95 ESP_LOGW(TAG, "Sensor read failed");
96 this->status_set_warning();
97 return;
98 }
99
100 this->status_clear_warning();
101
102 // Evaluate and publish measurements
103 if (this->temp_sensor_ != nullptr) {
104 // Temp is contained in the first result word
105 float temp = TEMPERATURE_OFFSET + TEMPERATURE_SPAN * static_cast<float>(buffer[0]) / RAW_MAX;
106 this->temp_sensor_->publish_state(temp);
107 }
108
109 if (this->humidity_sensor_ != nullptr) {
110 // Relative humidity is in the second result word
111 float rh = HUMIDITY_OFFSET + HUMIDITY_SPAN * static_cast<float>(buffer[1]) / RAW_MAX;
113 }
114
115 // Fire heater after measurement to maximize cooldown time before the next reading.
116 // The heater command produces a measurement that we don't need (datasheet 4.9).
117 if (this->heater_interval_ > 0) {
118 uint32_t now = millis();
119 if (now - this->last_heater_millis_ >= this->heater_interval_) {
120 ESP_LOGD(TAG, "Heater turning on");
121 if (this->write_command(this->heater_command_)) {
122 this->last_heater_millis_ = now;
123 }
124 }
125 }
126 });
127}
128
129} // namespace esphome::sht4x
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
void status_clear_warning()
Definition component.h:289
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
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.
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
SHT4XPRECISION precision_
Definition sht4x.h:32
SHT4XHEATERPOWER heater_power_
Definition sht4x.h:33
sensor::Sensor * humidity_sensor_
Definition sht4x.h:44
sensor::Sensor * temp_sensor_
Definition sht4x.h:43
void dump_config() override
Definition sht4x.cpp:66
SHT4XHEATERTIME heater_time_
Definition sht4x.h:34
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
@ SHT4X_HEATERPOWER_HIGH
Definition sht4x.h:13
@ SHT4X_HEATERPOWER_MED
Definition sht4x.h:13
@ SHT4X_HEATERTIME_LONG
Definition sht4x.h:15
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t