8static const char *
const TAG =
"htu21d";
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 ESP_LOGCONFIG(TAG,
"Setting up HTU21D...");
23 if (!this->
write_bytes(HTU21D_REGISTER_RESET,
nullptr, 0)) {
32 ESP_LOGCONFIG(TAG,
"HTU21D:");
35 ESP_LOGE(TAG,
"Communication with HTU21D failed!");
37 LOG_UPDATE_INTERVAL(
this);
39 LOG_SENSOR(
" ",
"Humidity", this->
humidity_);
49 uint16_t raw_temperature;
50 if (this->
read(
reinterpret_cast<uint8_t *
>(&raw_temperature), 2) !=
i2c::ERROR_OK) {
56 float temperature = (float(raw_temperature & 0xFFFC)) * 175.72f / 65536.0f - 46.85f;
58 ESP_LOGD(TAG,
"Got Temperature=%.1f°C",
temperature);
70 uint16_t raw_humidity;
77 float humidity = (float(raw_humidity & 0xFFFC)) * 125.0f / 65536.0f - 6.0f;
79 ESP_LOGD(TAG,
"Got Humidity=%.1f%%", humidity);
98 ESP_LOGD(TAG,
"Heater Level=%d", heater_level);
114 return (
bool) (((raw_heater) >> (HTU21D_REG_HTRE_BIT)) & 0x01);
125 raw_heater |= (1 << (HTU21D_REG_HTRE_BIT));
127 raw_heater &= ~(1 << (HTU21D_REG_HTRE_BIT));
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message="unspecified")
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
sensor::Sensor * humidity_
sensor::Sensor * temperature_
void set_heater(sensor::Sensor *heater)
void dump_config() override
int8_t get_heater_level()
void update() override
Update the sensor values (temperature+humidity).
HTU21DSensorModels sensor_model_
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, bool stop=true)
writes an array of bytes to a specific register in the I²C device
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
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
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
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.
const float DATA
For components that import data from directly connected sensors like DHT.
Providing packet encoding functions for exchanging data with a remote host.
void IRAM_ATTR HOT delay(uint32_t ms)