ESPHome 2025.5.0
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 ESP_LOGCONFIG(TAG, "Setting up sht4x...");
22
23 auto err = this->write(nullptr, 0);
24 if (err != i2c::ERROR_OK) {
25 this->mark_failed();
26 return;
27 }
28
29 if (std::isfinite(this->duty_cycle_) && this->duty_cycle_ > 0.0f) {
30 uint32_t heater_interval = static_cast<uint32_t>(static_cast<uint16_t>(this->heater_time_) / this->duty_cycle_);
31 ESP_LOGD(TAG, "Heater interval: %" PRIu32, heater_interval);
32
35 this->heater_command_ = 0x39;
36 } else {
37 this->heater_command_ = 0x32;
38 }
39 } else if (this->heater_power_ == SHT4X_HEATERPOWER_MED) {
41 this->heater_command_ = 0x2F;
42 } else {
43 this->heater_command_ = 0x24;
44 }
45 } else {
47 this->heater_command_ = 0x1E;
48 } else {
49 this->heater_command_ = 0x15;
50 }
51 }
52 ESP_LOGD(TAG, "Heater command: %x", this->heater_command_);
53
54 this->set_interval(heater_interval, std::bind(&SHT4XComponent::start_heater_, this));
55 }
56}
57
59 ESP_LOGCONFIG(TAG, "SHT4x:");
60 LOG_I2C_DEVICE(this);
61 if (this->is_failed()) {
62 ESP_LOGE(TAG, "Communication with SHT4x failed!");
63 }
64}
65
67 // Send command
68 if (!this->write_command(MEASURECOMMANDS[this->precision_])) {
69 // Warning will be printed only if warning status is not set yet
70 this->status_set_warning("Failed to send measurement command");
71 return;
72 }
73
74 this->set_timeout(10, [this]() {
75 uint16_t buffer[2];
76
77 // Read measurement
78 if (!this->read_data(buffer, 2)) {
79 // Using ESP_LOGW to force the warning to be printed
80 ESP_LOGW(TAG, "Sensor read failed");
81 this->status_set_warning();
82 return;
83 }
84
86
87 // Evaluate and publish measurements
88 if (this->temp_sensor_ != nullptr) {
89 // Temp is contained in the first result word
90 float sensor_value_temp = buffer[0];
91 float temp = -45 + 175 * sensor_value_temp / 65535;
92
93 this->temp_sensor_->publish_state(temp);
94 }
95
96 if (this->humidity_sensor_ != nullptr) {
97 // Relative humidity is in the second result word
98 float sensor_value_rh = buffer[1];
99 float rh = -6 + 125 * sensor_value_rh / 65535;
100
102 }
103 });
104}
105
106} // namespace sht4x
107} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
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:55
void status_set_warning(const char *message="unspecified")
void status_set_error(const char *message="unspecified")
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.
Definition component.cpp:72
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:39
SHT4XPRECISION precision_
Definition sht4x.h:34
SHT4XHEATERPOWER heater_power_
Definition sht4x.h:35
sensor::Sensor * humidity_sensor_
Definition sht4x.h:43
sensor::Sensor * temp_sensor_
Definition sht4x.h:42
void dump_config() override
Definition sht4x.cpp:58
SHT4XHEATERTIME heater_time_
Definition sht4x.h:36
@ 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