8 static const char *
const TAG =
"htu21d";
10 static const uint8_t HTU21D_ADDRESS = 0x40;
11 static const uint8_t HTU21D_REGISTER_RESET = 0xFE;
12 static const uint8_t HTU21D_REGISTER_TEMPERATURE = 0xF3;
13 static const uint8_t HTU21D_REGISTER_HUMIDITY = 0xF5;
14 static const uint8_t HTU21D_WRITERHT_REG_CMD = 0xE6;
15 static const uint8_t HTU21D_REGISTER_STATUS = 0xE7;
16 static const uint8_t HTU21D_WRITEHEATER_REG_CMD = 0x51;
17 static const uint8_t HTU21D_READHEATER_REG_CMD = 0x11;
18 static 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));
const float DATA
For components that import data from directly connected sensors like DHT.
uint16_t i2ctohs(uint16_t i2cshort)
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
sensor::Sensor * humidity_
void status_set_warning(const char *message="unspecified")
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void setup() override
Setup (reset) the sensor and check connection.
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
void update() override
Update the sensor values (temperature+humidity).
float get_setup_priority() const override
sensor::Sensor * temperature_
void dump_config() override
No error found during execution of method.
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
void set_heater(sensor::Sensor *heater)
int8_t get_heater_level()
virtual void mark_failed()
Mark this component as failed.
void set_heater_level(uint8_t level)
Implementation of SPI Controller mode.
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
HTU21DSensorModels sensor_model_
void IRAM_ATTR HOT delay(uint32_t ms)
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)