ESPHome  2024.3.1
sht4x.cpp
Go to the documentation of this file.
1 #include "sht4x.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace sht4x {
6 
7 static const char *const TAG = "sht4x";
8 
9 static 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  this->write(cmd, 1);
16 }
17 
19  ESP_LOGCONFIG(TAG, "Setting up sht4x...");
20 
21  if (this->duty_cycle_ > 0.0) {
22  uint32_t heater_interval = (uint32_t) (this->heater_time_ / this->duty_cycle_);
23  ESP_LOGD(TAG, "Heater interval: %" PRIu32, heater_interval);
24 
26  if (this->heater_time_ == SHT4X_HEATERTIME_LONG) {
27  this->heater_command_ = 0x39;
28  } else {
29  this->heater_command_ = 0x32;
30  }
31  } else if (this->heater_power_ == SHT4X_HEATERPOWER_MED) {
32  if (this->heater_time_ == SHT4X_HEATERTIME_LONG) {
33  this->heater_command_ = 0x2F;
34  } else {
35  this->heater_command_ = 0x24;
36  }
37  } else {
38  if (this->heater_time_ == SHT4X_HEATERTIME_LONG) {
39  this->heater_command_ = 0x1E;
40  } else {
41  this->heater_command_ = 0x15;
42  }
43  }
44  ESP_LOGD(TAG, "Heater command: %x", this->heater_command_);
45 
46  this->set_interval(heater_interval, std::bind(&SHT4XComponent::start_heater_, this));
47  }
48 }
49 
50 void SHT4XComponent::dump_config() { LOG_I2C_DEVICE(this); }
51 
53  // Send command
54  this->write_command(MEASURECOMMANDS[this->precision_]);
55 
56  this->set_timeout(10, [this]() {
57  uint16_t buffer[2];
58 
59  // Read measurement
60  bool read_status = this->read_data(buffer, 2);
61 
62  if (read_status) {
63  // Evaluate and publish measurements
64  if (this->temp_sensor_ != nullptr) {
65  // Temp is contained in the first result word
66  float sensor_value_temp = buffer[0];
67  float temp = -45 + 175 * sensor_value_temp / 65535;
68 
69  this->temp_sensor_->publish_state(temp);
70  }
71 
72  if (this->humidity_sensor_ != nullptr) {
73  // Relative humidity is in the second result word
74  float sensor_value_rh = buffer[1];
75  float rh = -6 + 125 * sensor_value_rh / 65535;
76 
78  }
79  } else {
80  ESP_LOGD(TAG, "Sensor read failed");
81  }
82  });
83 }
84 
85 } // namespace sht4x
86 } // namespace esphome
SHT4XHEATERPOWER heater_power_
Definition: sht4x.h:35
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:52
bool write_command(T i2c_register)
Write a command to the i2c device.
Definition: i2c_sensirion.h:82
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:69
SHT4XPRECISION precision_
Definition: sht4x.h:34
bool read_data(uint16_t *data, uint8_t len)
Read data words from i2c device.
sensor::Sensor * temp_sensor_
Definition: sht4x.h:42
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:186
void update() override
Definition: sht4x.cpp:52
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
void setup() override
Definition: sht4x.cpp:18
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void dump_config() override
Definition: sht4x.cpp:50
sensor::Sensor * humidity_sensor_
Definition: sht4x.h:43
SHT4XHEATERTIME heater_time_
Definition: sht4x.h:36
stm32_cmd_t * cmd
Definition: stm32flash.h:96