ESPHome  2022.12.8
htu21d.cpp
Go to the documentation of this file.
1 #include "htu21d.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/hal.h"
4 
5 namespace esphome {
6 namespace htu21d {
7 
8 static const char *const TAG = "htu21d";
9 
10 static const uint8_t HTU21D_ADDRESS = 0x40;
11 static const uint8_t HTU21D_REGISTER_RESET = 0xFE;
12 static const uint8_t HTU21D_REGISTER_TEMPERATURE = 0xF3;
13 static const uint8_t HTU21D_REGISTER_HUMIDITY = 0xF5;
14 static const uint8_t HTU21D_REGISTER_STATUS = 0xE7;
15 
17  ESP_LOGCONFIG(TAG, "Setting up HTU21D...");
18 
19  if (!this->write_bytes(HTU21D_REGISTER_RESET, nullptr, 0)) {
20  this->mark_failed();
21  return;
22  }
23 
24  // Wait for software reset to complete
25  delay(15);
26 }
28  ESP_LOGCONFIG(TAG, "HTU21D:");
29  LOG_I2C_DEVICE(this);
30  if (this->is_failed()) {
31  ESP_LOGE(TAG, "Communication with HTU21D failed!");
32  }
33  LOG_UPDATE_INTERVAL(this);
34  LOG_SENSOR(" ", "Temperature", this->temperature_);
35  LOG_SENSOR(" ", "Humidity", this->humidity_);
36 }
38  uint16_t raw_temperature;
39  if (this->write(&HTU21D_REGISTER_TEMPERATURE, 1) != i2c::ERROR_OK) {
40  this->status_set_warning();
41  return;
42  }
43  delay(50); // NOLINT
44  if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) {
45  this->status_set_warning();
46  return;
47  }
48  raw_temperature = i2c::i2ctohs(raw_temperature);
49 
50  float temperature = (float(raw_temperature & 0xFFFC)) * 175.72f / 65536.0f - 46.85f;
51 
52  uint16_t raw_humidity;
53  if (this->write(&HTU21D_REGISTER_HUMIDITY, 1) != i2c::ERROR_OK) {
54  this->status_set_warning();
55  return;
56  }
57  delay(50); // NOLINT
58  if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
59  this->status_set_warning();
60  return;
61  }
62  raw_humidity = i2c::i2ctohs(raw_humidity);
63 
64  float humidity = (float(raw_humidity & 0xFFFC)) * 125.0f / 65536.0f - 6.0f;
65  ESP_LOGD(TAG, "Got Temperature=%.1f°C Humidity=%.1f%%", temperature, humidity);
66 
67  if (this->temperature_ != nullptr)
68  this->temperature_->publish_state(temperature);
69  if (this->humidity_ != nullptr)
70  this->humidity_->publish_state(humidity);
71  this->status_clear_warning();
72 }
74 
75 } // namespace htu21d
76 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
uint16_t i2ctohs(uint16_t i2cshort)
Definition: i2c.h:36
sensor::Sensor * humidity_
Definition: htu21d.h:25
ErrorCode read(uint8_t *data, size_t len)
Definition: i2c.h:48
float temperature
Definition: qmp6988.h:71
void setup() override
Setup (reset) the sensor and check connection.
Definition: htu21d.cpp:16
void update() override
Update the sensor values (temperature+humidity).
Definition: htu21d.cpp:37
float get_setup_priority() const override
Definition: htu21d.cpp:73
sensor::Sensor * temperature_
Definition: htu21d.h:24
void dump_config() override
Definition: htu21d.cpp:27
void status_clear_warning()
Definition: component.cpp:149
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:72
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:56
void status_set_warning()
Definition: component.cpp:141
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:112
Definition: a4988.cpp:4
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:109