ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
sht4x.cpp
Go to the documentation of this file.
1#include "sht4x.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace 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
13 uint8_t cmd[] = {MEASURECOMMANDS[this->heater_command_]};
14
15 ESP_LOGD(TAG, "Heater turning on");
16 if (this->write(cmd, 1) != i2c::ERROR_OK) {
17 this->status_set_error(LOG_STR("Failed to turn on heater"));
18 }
19}
20
22 uint16_t buffer[2];
23 if (!this->get_8bit_register(SERIAL_NUMBER_COMMAND, buffer, 2, 1)) {
24 ESP_LOGE(TAG, "Get serial number failed");
25 this->serial_number_ = 0;
26 return;
27 }
28 this->serial_number_ = (uint32_t(buffer[0]) << 16) | (uint32_t(buffer[1]));
29 ESP_LOGD(TAG, "Serial number: %08" PRIx32, this->serial_number_);
30}
31
33 auto err = this->write(nullptr, 0);
34 if (err != i2c::ERROR_OK) {
35 this->mark_failed();
36 return;
37 }
38
39 this->read_serial_number_();
40
41 if (std::isfinite(this->duty_cycle_) && this->duty_cycle_ > 0.0f) {
42 uint32_t heater_interval = static_cast<uint32_t>(static_cast<uint16_t>(this->heater_time_) / this->duty_cycle_);
43 ESP_LOGD(TAG, "Heater interval: %" PRIu32, heater_interval);
44
47 this->heater_command_ = 0x39;
48 } else {
49 this->heater_command_ = 0x32;
50 }
51 } else if (this->heater_power_ == SHT4X_HEATERPOWER_MED) {
53 this->heater_command_ = 0x2F;
54 } else {
55 this->heater_command_ = 0x24;
56 }
57 } else {
59 this->heater_command_ = 0x1E;
60 } else {
61 this->heater_command_ = 0x15;
62 }
63 }
64 ESP_LOGD(TAG, "Heater command: %x", this->heater_command_);
65
66 this->set_interval(heater_interval, std::bind(&SHT4XComponent::start_heater_, this));
67 }
68}
69
71 ESP_LOGCONFIG(TAG,
72 "SHT4x:\n"
73 " Serial number: %08" PRIx32,
74 this->serial_number_);
75
76 LOG_I2C_DEVICE(this);
77 if (this->is_failed()) {
78 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
79 }
80 if (this->serial_number_ == 0) {
81 ESP_LOGW(TAG, "Get serial number failed");
82 }
83}
84
86 // Send command
87 if (!this->write_command(MEASURECOMMANDS[this->precision_])) {
88 // Warning will be printed only if warning status is not set yet
89 this->status_set_warning(LOG_STR("Failed to send measurement command"));
90 return;
91 }
92
93 this->set_timeout(10, [this]() {
94 uint16_t buffer[2];
95
96 // Read measurement
97 if (!this->read_data(buffer, 2)) {
98 // Using ESP_LOGW to force the warning to be printed
99 ESP_LOGW(TAG, "Sensor read failed");
100 this->status_set_warning();
101 return;
102 }
103
104 this->status_clear_warning();
105
106 // Evaluate and publish measurements
107 if (this->temp_sensor_ != nullptr) {
108 // Temp is contained in the first result word
109 float sensor_value_temp = buffer[0];
110 float temp = -45 + 175 * sensor_value_temp / 65535;
111
112 this->temp_sensor_->publish_state(temp);
113 }
114
115 if (this->humidity_sensor_ != nullptr) {
116 // Relative humidity is in the second result word
117 float sensor_value_rh = buffer[1];
118 float rh = -6 + 125 * sensor_value_rh / 65535;
119
121 }
122 });
123}
124
125} // namespace sht4x
126} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:184
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:77
SHT4XPRECISION precision_
Definition sht4x.h:33
SHT4XHEATERPOWER heater_power_
Definition sht4x.h:34
sensor::Sensor * humidity_sensor_
Definition sht4x.h:44
sensor::Sensor * temp_sensor_
Definition sht4x.h:43
void dump_config() override
Definition sht4x.cpp:70
SHT4XHEATERTIME heater_time_
Definition sht4x.h:35
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:33
@ SHT4X_HEATERPOWER_HIGH
Definition sht4x.h:14
@ SHT4X_HEATERPOWER_MED
Definition sht4x.h:14
@ SHT4X_HEATERTIME_LONG
Definition sht4x.h:16
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7