ESPHome 2025.9.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};
10
12 uint8_t cmd[] = {MEASURECOMMANDS[this->heater_command_]};
13
14 ESP_LOGD(TAG, "Heater turning on");
15 if (this->write(cmd, 1) != i2c::ERROR_OK) {
16 this->status_set_error("Failed to turn on heater");
17 }
18}
19
21 auto err = this->write(nullptr, 0);
22 if (err != i2c::ERROR_OK) {
23 this->mark_failed();
24 return;
25 }
26
27 if (std::isfinite(this->duty_cycle_) && this->duty_cycle_ > 0.0f) {
28 uint32_t heater_interval = static_cast<uint32_t>(static_cast<uint16_t>(this->heater_time_) / this->duty_cycle_);
29 ESP_LOGD(TAG, "Heater interval: %" PRIu32, heater_interval);
30
33 this->heater_command_ = 0x39;
34 } else {
35 this->heater_command_ = 0x32;
36 }
37 } else if (this->heater_power_ == SHT4X_HEATERPOWER_MED) {
39 this->heater_command_ = 0x2F;
40 } else {
41 this->heater_command_ = 0x24;
42 }
43 } else {
45 this->heater_command_ = 0x1E;
46 } else {
47 this->heater_command_ = 0x15;
48 }
49 }
50 ESP_LOGD(TAG, "Heater command: %x", this->heater_command_);
51
52 this->set_interval(heater_interval, std::bind(&SHT4XComponent::start_heater_, this));
53 }
54}
55
57 ESP_LOGCONFIG(TAG, "SHT4x:");
58 LOG_I2C_DEVICE(this);
59 if (this->is_failed()) {
60 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
61 }
62}
63
65 // Send command
66 if (!this->write_command(MEASURECOMMANDS[this->precision_])) {
67 // Warning will be printed only if warning status is not set yet
68 this->status_set_warning("Failed to send measurement command");
69 return;
70 }
71
72 this->set_timeout(10, [this]() {
73 uint16_t buffer[2];
74
75 // Read measurement
76 if (!this->read_data(buffer, 2)) {
77 // Using ESP_LOGW to force the warning to be printed
78 ESP_LOGW(TAG, "Sensor read failed");
79 this->status_set_warning();
80 return;
81 }
82
84
85 // Evaluate and publish measurements
86 if (this->temp_sensor_ != nullptr) {
87 // Temp is contained in the first result word
88 float sensor_value_temp = buffer[0];
89 float temp = -45 + 175 * sensor_value_temp / 65535;
90
91 this->temp_sensor_->publish_state(temp);
92 }
93
94 if (this->humidity_sensor_ != nullptr) {
95 // Relative humidity is in the second result word
96 float sensor_value_rh = buffer[1];
97 float rh = -6 + 125 * sensor_value_rh / 65535;
98
100 }
101 });
102}
103
104} // namespace sht4x
105} // 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.
Definition component.cpp:89
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.
void status_set_error(const char *message=nullptr)
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
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:45
SHT4XPRECISION precision_
Definition sht4x.h:33
SHT4XHEATERPOWER heater_power_
Definition sht4x.h:34
sensor::Sensor * humidity_sensor_
Definition sht4x.h:42
sensor::Sensor * temp_sensor_
Definition sht4x.h:41
void dump_config() override
Definition sht4x.cpp:56
SHT4XHEATERTIME heater_time_
Definition sht4x.h:35
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
@ 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