7static const char *
const TAG =
"htu21d";
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)) {
29 ESP_LOGCONFIG(TAG,
"HTU21D:");
32 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
34 LOG_UPDATE_INTERVAL(
this);
36 LOG_SENSOR(
" ",
"Humidity", this->
humidity_);
46 uint16_t raw_temperature;
47 if (this->
read(
reinterpret_cast<uint8_t *
>(&raw_temperature), 2) !=
i2c::ERROR_OK) {
53 float temperature = (float(raw_temperature & 0xFFFC)) * 175.72f / 65536.0f - 46.85f;
55 ESP_LOGD(TAG,
"Got Temperature=%.1f°C",
temperature);
66 uint16_t raw_humidity;
73 float humidity = (float(raw_humidity & 0xFFFC)) * 125.0f / 65536.0f - 6.0f;
75 ESP_LOGD(TAG,
"Got Humidity=%.1f%%", humidity);
84 uint8_t heater_level = 0;
95 ESP_LOGD(TAG,
"Heater Level=%d", heater_level);
109 return (
bool) ((raw_heater >> HTU21D_REG_HTRE_BIT) & 0x01);
119 raw_heater |= (1 << HTU21D_REG_HTRE_BIT);
121 raw_heater &= ~(1 << HTU21D_REG_HTRE_BIT);
142 return raw_heater & 0xF;
void mark_failed()
Mark this component as failed.
void status_set_warning()
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void status_clear_warning()
sensor::Sensor * humidity_
sensor::Sensor * temperature_
void set_heater(sensor::Sensor *heater)
void dump_config() override
void update() override
Update the sensor values (temperature+humidity).
HTU21DSensorModels sensor_model_
uint8_t get_heater_level()
void setup() override
Setup (reset) the sensor and check connection.
void set_heater_level(uint8_t level)
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
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
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
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
void publish_state(float state)
Publish a new state to the front-end.
@ HTU21D_SENSOR_MODEL_HTU21D
uint16_t i2ctohs(uint16_t i2cshort)
@ ERROR_OK
No error found during execution of method.
void HOT delay(uint32_t ms)