ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
htu21d.cpp
Go to the documentation of this file.
1#include "htu21d.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome::htu21d {
7
8ESPHOME_LOG_TAG(TAG, "htu21d");
9
10static const uint8_t HTU21D_ADDRESS = 0x40;
11static const uint8_t HTU21D_REGISTER_RESET = 0xFE;
12static const uint8_t HTU21D_REGISTER_TEMPERATURE = 0xF3;
13static const uint8_t HTU21D_REGISTER_HUMIDITY = 0xF5;
14static const uint8_t HTU21D_WRITERHT_REG_CMD = 0xE6;
15static const uint8_t HTU21D_REGISTER_STATUS = 0xE7;
16static const uint8_t HTU21D_WRITEHEATER_REG_CMD = 0x51;
17static const uint8_t HTU21D_READHEATER_REG_CMD = 0x11;
18static const uint8_t HTU21D_REG_HTRE_BIT = 0x02;
21 if (!this->write_bytes(HTU21D_REGISTER_RESET, nullptr, 0)) {
22 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
23 return;
24 }
25
26 // Wait for software reset to complete
27 delay(15);
28}
29void HTU21DComponent::dump_config() {
30 ESP_LOGCONFIG(TAG, "HTU21D:");
31 LOG_I2C_DEVICE(this);
32 LOG_UPDATE_INTERVAL(this);
33 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
34 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
35}
36void HTU21DComponent::update() {
37 if (this->write(&HTU21D_REGISTER_TEMPERATURE, 1) != i2c::ERROR_OK) {
38 this->status_set_warning();
39 return;
40 }
41
42 // According to the datasheet sht21 temperature readings can take up to 85ms
43 this->set_timeout(85, [this]() {
44 uint16_t raw_temperature;
45 if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) {
46 this->status_set_warning();
47 return;
48 }
49 raw_temperature = i2c::i2ctohs(raw_temperature);
50
51 float temperature = (float(raw_temperature & 0xFFFC)) * 175.72f / 65536.0f - 46.85f;
52
53 ESP_LOGD(TAG, "Got Temperature=%.1f°C", temperature);
54
55 PUBLISH_STATE(this->temperature_sensor_, 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 PUBLISH_STATE(this->humidity_sensor_, humidity);
75
77
78 // HTU21D does have a heater module but does not have heater level
79 // Setting heater level to 1 in case the heater is ON
80 uint8_t heater_level = 0;
82 if (this->is_heater_enabled()) {
83 heater_level = 1;
84 } else {
85 heater_level = 0;
86 }
87 } else {
88 heater_level = this->get_heater_level();
89 }
90
91 ESP_LOGD(TAG, "Heater Level=%d", heater_level);
92
93 PUBLISH_STATE(this->heater_sensor_, heater_level);
94 });
95 });
96}
97
98bool HTU21DComponent::is_heater_enabled() {
99 uint8_t raw_heater;
100 if (this->read_register(HTU21D_REGISTER_STATUS, &raw_heater, 1) != i2c::ERROR_OK) {
101 this->status_set_warning();
102 return false;
103 }
104 return (bool) ((raw_heater >> HTU21D_REG_HTRE_BIT) & 0x01);
105}
106
107void HTU21DComponent::set_heater(bool status) {
108 uint8_t raw_heater;
109 if (this->read_register(HTU21D_REGISTER_STATUS, &raw_heater, 1) != i2c::ERROR_OK) {
110 this->status_set_warning();
111 return;
112 }
113 assign_bits(raw_heater, 1 << HTU21D_REG_HTRE_BIT, status);
114 if (this->write_register(HTU21D_WRITERHT_REG_CMD, &raw_heater, 1) != i2c::ERROR_OK) {
115 this->status_set_warning();
116 return;
117 }
118}
119
120void HTU21DComponent::set_heater_level(uint8_t level) {
121 if (this->write_register(HTU21D_WRITEHEATER_REG_CMD, &level, 1) != i2c::ERROR_OK) {
122 this->status_set_warning();
123 return;
124 }
125}
126
127uint8_t HTU21DComponent::get_heater_level() {
128 uint8_t raw_heater;
129 if (this->read_register(HTU21D_READHEATER_REG_CMD, &raw_heater, 1) != i2c::ERROR_OK) {
130 this->status_set_warning();
131 return 0;
132 }
133 return raw_heater & 0xF;
134}
135
136} // namespace esphome::htu21d
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
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:309
HTU21DSensorModels sensor_model_
Definition htu21d.h:30
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:54
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:45
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
@ 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
FLAG_HAS_TRANSITION float
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1150
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint16_t temperature
Definition sun_gtil2.cpp:12