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_REGISTER_STATUS = 0xE7;
17 ESP_LOGCONFIG(TAG,
"Setting up HTU21D...");
19 if (!this->
write_bytes(HTU21D_REGISTER_RESET,
nullptr, 0)) {
28 ESP_LOGCONFIG(TAG,
"HTU21D:");
31 ESP_LOGE(TAG,
"Communication with HTU21D failed!");
33 LOG_UPDATE_INTERVAL(
this);
35 LOG_SENSOR(
" ",
"Humidity", this->
humidity_);
38 uint16_t raw_temperature;
44 if (this->
read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) !=
i2c::ERROR_OK) {
50 float temperature = (float(raw_temperature & 0xFFFC)) * 175.72f / 65536.0f - 46.85f;
52 uint16_t raw_humidity;
64 float humidity = (float(raw_humidity & 0xFFFC)) * 125.0f / 65536.0f - 6.0f;
65 ESP_LOGD(TAG,
"Got Temperature=%.1f°C Humidity=%.1f%%", temperature, humidity);
const float DATA
For components that import data from directly connected sensors like DHT.
uint16_t i2ctohs(uint16_t i2cshort)
sensor::Sensor * humidity_
ErrorCode read(uint8_t *data, size_t len)
void setup() override
Setup (reset) the sensor and check connection.
void update() override
Update the sensor values (temperature+humidity).
float get_setup_priority() const override
sensor::Sensor * temperature_
void dump_config() override
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
void status_set_warning()
virtual void mark_failed()
Mark this component as failed.
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)