ESPHome 2025.10.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 {
6namespace htu21d {
7
8static const char *const 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 = 0xE3;
13static const uint8_t HTU21D_REGISTER_HUMIDITY = 0xE5;
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();
23 return;
24 }
25
26 // Wait for software reset to complete
27 delay(15);
28}
30 ESP_LOGCONFIG(TAG, "HTU21D:");
31 LOG_I2C_DEVICE(this);
32 if (this->is_failed()) {
33 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
34 }
35 LOG_UPDATE_INTERVAL(this);
36 LOG_SENSOR(" ", "Temperature", this->temperature_);
37 LOG_SENSOR(" ", "Humidity", this->humidity_);
38}
40 if (this->write(&HTU21D_REGISTER_TEMPERATURE, 1) != i2c::ERROR_OK) {
41 this->status_set_warning();
42 return;
43 }
44
45 // According to the datasheet sht21 temperature readings can take up to 85ms
46 this->set_timeout(85, [this]() {
47 uint16_t raw_temperature;
48 if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) {
49 this->status_set_warning();
50 return;
51 }
52 raw_temperature = i2c::i2ctohs(raw_temperature);
53
54 float temperature = (float(raw_temperature & 0xFFFC)) * 175.72f / 65536.0f - 46.85f;
55
56 ESP_LOGD(TAG, "Got Temperature=%.1f°C", temperature);
57
58 if (this->temperature_ != nullptr)
59 this->temperature_->publish_state(temperature);
60
61 if (this->write(&HTU21D_REGISTER_HUMIDITY, 1) != i2c::ERROR_OK) {
62 this->status_set_warning();
63 return;
64 }
65
66 this->set_timeout(50, [this]() {
67 uint16_t raw_humidity;
68 if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
69 this->status_set_warning();
70 return;
71 }
72 raw_humidity = i2c::i2ctohs(raw_humidity);
73
74 float humidity = (float(raw_humidity & 0xFFFC)) * 125.0f / 65536.0f - 6.0f;
75
76 ESP_LOGD(TAG, "Got Humidity=%.1f%%", humidity);
77
78 if (this->humidity_ != nullptr)
79 this->humidity_->publish_state(humidity);
80
82
83 // HTU21D does have a heater module but does not have heater level
84 // Setting heater level to 1 in case the heater is ON
85 uint8_t heater_level = 0;
87 if (this->is_heater_enabled()) {
88 heater_level = 1;
89 } else {
90 heater_level = 0;
91 }
92 } else {
93 heater_level = this->get_heater_level();
94 }
95
96 ESP_LOGD(TAG, "Heater Level=%d", heater_level);
97
98 if (this->heater_ != nullptr)
99 this->heater_->publish_state(heater_level);
100 });
101 });
102}
103
105 uint8_t raw_heater;
106 if (this->read_register(HTU21D_REGISTER_STATUS, &raw_heater, 1) != i2c::ERROR_OK) {
107 this->status_set_warning();
108 return false;
109 }
110 return (bool) ((raw_heater >> HTU21D_REG_HTRE_BIT) & 0x01);
111}
112
114 uint8_t raw_heater;
115 if (this->read_register(HTU21D_REGISTER_STATUS, &raw_heater, 1) != i2c::ERROR_OK) {
116 this->status_set_warning();
117 return;
118 }
119 if (status) {
120 raw_heater |= (1 << HTU21D_REG_HTRE_BIT);
121 } else {
122 raw_heater &= ~(1 << HTU21D_REG_HTRE_BIT);
123 }
124 if (this->write_register(HTU21D_WRITERHT_REG_CMD, &raw_heater, 1) != i2c::ERROR_OK) {
125 this->status_set_warning();
126 return;
127 }
128}
129
131 if (this->write_register(HTU21D_WRITEHEATER_REG_CMD, &level, 1) != i2c::ERROR_OK) {
132 this->status_set_warning();
133 return;
134 }
135}
136
138 uint8_t raw_heater;
139 if (this->read_register(HTU21D_READHEATER_REG_CMD, &raw_heater, 1) != i2c::ERROR_OK) {
140 this->status_set_warning();
141 return 0;
142 }
143 return raw_heater & 0xF;
144}
145
147
148} // namespace htu21d
149} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
float get_setup_priority() const override
Definition htu21d.cpp:146
sensor::Sensor * humidity_
Definition htu21d.h:35
sensor::Sensor * temperature_
Definition htu21d.h:34
void set_heater(sensor::Sensor *heater)
Definition htu21d.h:17
void update() override
Update the sensor values (temperature+humidity).
Definition htu21d.cpp:39
HTU21DSensorModels sensor_model_
Definition htu21d.h:37
sensor::Sensor * heater_
Definition htu21d.h:36
void setup() override
Setup (reset) the sensor and check connection.
Definition htu21d.cpp:20
void set_heater_level(uint8_t level)
Definition htu21d.cpp:130
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:41
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:184
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
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:32
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:252
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:73
@ HTU21D_SENSOR_MODEL_HTU21D
Definition htu21d.h:11
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:128
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:33
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:49
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint16_t temperature
Definition sun_gtil2.cpp:12