ESPHome 2026.11.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
7ESPHOME_LOG_TAG(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(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
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->serial_number_ == 0) {
74 ESP_LOGW(TAG, "Get serial number failed");
75 }
76}
77
79 // Send command
80 if (!this->write_command(MEASURECOMMANDS[this->precision_])) {
81 // Warning will be printed only if warning status is not set yet
82 this->status_set_warning(LOG_STR("Failed to send measurement command"));
83 return;
84 }
85
86 this->set_timeout(10, [this]() {
87 uint16_t buffer[2];
88
89 // Read measurement
90 if (!this->status_comm_result(this->read_data(buffer, 2))) {
91 // Using ESP_LOGW to force the warning to be printed
92 ESP_LOGW(TAG, "Sensor read failed");
93 return;
94 }
95
96 // Evaluate and publish measurements
97 if (this->temp_sensor_ != nullptr) {
98 // Temp is contained in the first result word
99 float temp = TEMPERATURE_OFFSET + TEMPERATURE_SPAN * static_cast<float>(buffer[0]) / RAW_MAX;
100 this->temp_sensor_->publish_state(temp);
101 }
102
103 if (this->humidity_sensor_ != nullptr) {
104 // Relative humidity is in the second result word
105 float rh = HUMIDITY_OFFSET + HUMIDITY_SPAN * static_cast<float>(buffer[1]) / RAW_MAX;
107 }
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.
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.
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
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
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:13
@ SHT4X_HEATERPOWER_MED
Definition sht4x.h:13
ESPHOME_LOG_TAG(TAG, "sht4x")
@ SHT4X_HEATERTIME_LONG
Definition sht4x.h:15
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t