ESPHome  2021.11.3
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: %i", 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  uint8_t cmd[] = {MEASURECOMMANDS[this->precision_]};
54 
55  // Send command
56  this->write(cmd, 1);
57 
58  this->set_timeout(10, [this]() {
59  const uint8_t num_bytes = 6;
60  uint8_t buffer[num_bytes];
61 
62  // Read measurement
63  bool read_status = this->read_bytes_raw(buffer, num_bytes);
64 
65  if (read_status) {
66  // Evaluate and publish measurements
67  if (this->temp_sensor_ != nullptr) {
68  // Temp is contained in the first 16 bits
69  float sensor_value_temp = (buffer[0] << 8) + buffer[1];
70  float temp = -45 + 175 * sensor_value_temp / 65535;
71 
72  this->temp_sensor_->publish_state(temp);
73  }
74 
75  if (this->humidity_sensor_ != nullptr) {
76  // Relative humidity is in the last 16 bits
77  float sensor_value_rh = (buffer[3] << 8) + buffer[4];
78  float rh = -6 + 125 * sensor_value_rh / 65535;
79 
81  }
82  } else {
83  ESP_LOGD(TAG, "Sensor read failed");
84  }
85  });
86 }
87 
88 } // namespace sht4x
89 } // namespace esphome
SHT4XHEATERPOWER heater_power_
Definition: sht4x.h:33
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:50
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition: i2c.h:80
SHT4XPRECISION precision_
Definition: sht4x.h:32
ErrorCode write(const uint8_t *data, uint8_t len)
Definition: i2c.h:56
sensor::Sensor * temp_sensor_
Definition: sht4x.h:40
void update() override
Definition: sht4x.cpp:52
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:58
void set_timeout(uint32_t timeout, std::function< void()> &&f)
Definition: component.cpp:117
void setup() override
Definition: sht4x.cpp:18
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
void dump_config() override
Definition: sht4x.cpp:50
sensor::Sensor * humidity_sensor_
Definition: sht4x.h:41
SHT4XHEATERTIME heater_time_
Definition: sht4x.h:34