ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
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
5namespace esphome::htu21d {
6
7ESPHOME_LOG_TAG(TAG, "htu21d");
8
9static const uint8_t HTU21D_ADDRESS = 0x40;
10static const uint8_t HTU21D_REGISTER_RESET = 0xFE;
11static const uint8_t HTU21D_REGISTER_TEMPERATURE = 0xF3;
12static const uint8_t HTU21D_REGISTER_HUMIDITY = 0xF5;
13static const uint8_t HTU21D_WRITERHT_REG_CMD = 0xE6;
14static const uint8_t HTU21D_REGISTER_STATUS = 0xE7;
15static const uint8_t HTU21D_WRITEHEATER_REG_CMD = 0x51;
16static const uint8_t HTU21D_READHEATER_REG_CMD = 0x11;
17static const uint8_t HTU21D_REG_HTRE_BIT = 0x02;
20 if (!this->write_bytes(HTU21D_REGISTER_RESET, nullptr, 0)) {
21 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
22 return;
23 }
24
25 // Wait for software reset to complete
26 delay(15);
27}
29 ESP_LOGCONFIG(TAG, "HTU21D:");
30 LOG_I2C_DEVICE(this);
31 LOG_UPDATE_INTERVAL(this);
32 LOG_SENSOR(" ", "Temperature", this->temperature_);
33 LOG_SENSOR(" ", "Humidity", this->humidity_);
34}
36 if (this->write(&HTU21D_REGISTER_TEMPERATURE, 1) != i2c::ERROR_OK) {
37 this->status_set_warning();
38 return;
39 }
40
41 // According to the datasheet sht21 temperature readings can take up to 85ms
42 this->set_timeout(85, [this]() {
43 uint16_t raw_temperature;
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 ESP_LOGD(TAG, "Got Temperature=%.1f°C", temperature);
53
54 if (this->temperature_ != nullptr)
55 this->temperature_->publish_state(temperature);
56
57 if (this->write(&HTU21D_REGISTER_HUMIDITY, 1) != i2c::ERROR_OK) {
58 this->status_set_warning();
59 return;
60 }
61
62 this->set_timeout(50, [this]() {
63 uint16_t raw_humidity;
64 if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
65 this->status_set_warning();
66 return;
67 }
68 raw_humidity = i2c::i2ctohs(raw_humidity);
69
70 float humidity = (float(raw_humidity & 0xFFFC)) * 125.0f / 65536.0f - 6.0f;
71
72 ESP_LOGD(TAG, "Got Humidity=%.1f%%", humidity);
73
74 if (this->humidity_ != nullptr)
75 this->humidity_->publish_state(humidity);
76
78
79 // HTU21D does have a heater module but does not have heater level
80 // Setting heater level to 1 in case the heater is ON
81 uint8_t heater_level = 0;
83 if (this->is_heater_enabled()) {
84 heater_level = 1;
85 } else {
86 heater_level = 0;
87 }
88 } else {
89 heater_level = this->get_heater_level();
90 }
91
92 ESP_LOGD(TAG, "Heater Level=%d", heater_level);
93
94 if (this->heater_ != nullptr)
95 this->heater_->publish_state(heater_level);
96 });
97 });
98}
99
101 uint8_t raw_heater;
102 if (this->read_register(HTU21D_REGISTER_STATUS, &raw_heater, 1) != i2c::ERROR_OK) {
103 this->status_set_warning();
104 return false;
105 }
106 return (bool) ((raw_heater >> HTU21D_REG_HTRE_BIT) & 0x01);
107}
108
110 uint8_t raw_heater;
111 if (this->read_register(HTU21D_REGISTER_STATUS, &raw_heater, 1) != i2c::ERROR_OK) {
112 this->status_set_warning();
113 return;
114 }
115 if (status) {
116 raw_heater |= (1 << HTU21D_REG_HTRE_BIT);
117 } else {
118 raw_heater &= ~(1 << HTU21D_REG_HTRE_BIT);
119 }
120 if (this->write_register(HTU21D_WRITERHT_REG_CMD, &raw_heater, 1) != i2c::ERROR_OK) {
121 this->status_set_warning();
122 return;
123 }
124}
125
127 if (this->write_register(HTU21D_WRITEHEATER_REG_CMD, &level, 1) != i2c::ERROR_OK) {
128 this->status_set_warning();
129 return;
130 }
131}
132
134 uint8_t raw_heater;
135 if (this->read_register(HTU21D_READHEATER_REG_CMD, &raw_heater, 1) != i2c::ERROR_OK) {
136 this->status_set_warning();
137 return 0;
138 }
139 return raw_heater & 0xF;
140}
141
142} // namespace esphome::htu21d
void mark_failed()
Mark this component as failed.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:296
sensor::Sensor * humidity_
Definition htu21d.h:31
sensor::Sensor * temperature_
Definition htu21d.h:30
void set_heater(sensor::Sensor *heater)
Definition htu21d.h:15
void update() override
Update the sensor values (temperature+humidity).
Definition htu21d.cpp:35
HTU21DSensorModels sensor_model_
Definition htu21d.h:33
sensor::Sensor * heater_
Definition htu21d.h:32
void setup() override
Setup (reset) the sensor and check connection.
Definition htu21d.cpp:19
void set_heater_level(uint8_t level)
Definition htu21d.cpp:126
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:37
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:28
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
@ HTU21D_SENSOR_MODEL_HTU21D
Definition htu21d.h:9
ESPHOME_LOG_TAG(TAG, "htu21d")
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:139
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint16_t temperature
Definition sun_gtil2.cpp:12