ESPHome 2025.9.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 = 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();
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);
61
62 if (this->write(&HTU21D_REGISTER_HUMIDITY, 1) != i2c::ERROR_OK) {
63 this->status_set_warning();
64 return;
65 }
66
67 this->set_timeout(50, [this]() {
68 uint16_t raw_humidity;
69 if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
70 this->status_set_warning();
71 return;
72 }
73 raw_humidity = i2c::i2ctohs(raw_humidity);
74
75 float humidity = (float(raw_humidity & 0xFFFC)) * 125.0f / 65536.0f - 6.0f;
76
77 ESP_LOGD(TAG, "Got Humidity=%.1f%%", humidity);
78
79 if (this->humidity_ != nullptr)
80 this->humidity_->publish_state(humidity);
81
82 int8_t heater_level;
83
84 // HTU21D does have a heater module but does not have heater level
85 // Setting heater level to 1 in case the heater is ON
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 this->status_clear_warning();
101 });
102 });
103}
104
106 uint8_t raw_heater;
107 if (this->read_register(HTU21D_REGISTER_STATUS, reinterpret_cast<uint8_t *>(&raw_heater), 2) != i2c::ERROR_OK) {
108 this->status_set_warning();
109 return false;
110 }
111 raw_heater = i2c::i2ctohs(raw_heater);
112 return (bool) (((raw_heater) >> (HTU21D_REG_HTRE_BIT)) & 0x01);
113}
114
116 uint8_t raw_heater;
117 if (this->read_register(HTU21D_REGISTER_STATUS, reinterpret_cast<uint8_t *>(&raw_heater), 2) != i2c::ERROR_OK) {
118 this->status_set_warning();
119 return;
120 }
121 raw_heater = i2c::i2ctohs(raw_heater);
122 if (status) {
123 raw_heater |= (1 << (HTU21D_REG_HTRE_BIT));
124 } else {
125 raw_heater &= ~(1 << (HTU21D_REG_HTRE_BIT));
126 }
127
128 if (this->write_register(HTU21D_WRITERHT_REG_CMD, &raw_heater, 1) != i2c::ERROR_OK) {
129 this->status_set_warning();
130 return;
131 }
132}
133
135 if (this->write_register(HTU21D_WRITEHEATER_REG_CMD, &level, 1) != i2c::ERROR_OK) {
136 this->status_set_warning();
137 return;
138 }
139}
140
142 int8_t raw_heater;
143 if (this->read_register(HTU21D_READHEATER_REG_CMD, reinterpret_cast<uint8_t *>(&raw_heater), 2) != i2c::ERROR_OK) {
144 this->status_set_warning();
145 return 0;
146 }
147 raw_heater = i2c::i2ctohs(raw_heater);
148 return raw_heater;
149}
150
152
153} // namespace htu21d
154} // namespace esphome
uint8_t status
Definition bl0942.h:8
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:151
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:134
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:25
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition i2c.h:252
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:10
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
@ 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:13
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
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