mirror of
https://github.com/esphome/esphome.git
synced 2024-12-27 07:51:43 +01:00
Improved sensor readings in htu21d component. (#5839)
This commit is contained in:
parent
46310ff223
commit
45f9f3d972
1 changed files with 38 additions and 29 deletions
|
@ -39,45 +39,54 @@ void HTU21DComponent::dump_config() {
|
|||
LOG_SENSOR(" ", "Humidity", this->humidity_);
|
||||
}
|
||||
void HTU21DComponent::update() {
|
||||
uint16_t raw_temperature;
|
||||
if (this->write(&HTU21D_REGISTER_TEMPERATURE, 1) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
delay(50); // NOLINT
|
||||
if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
raw_temperature = i2c::i2ctohs(raw_temperature);
|
||||
|
||||
float temperature = (float(raw_temperature & 0xFFFC)) * 175.72f / 65536.0f - 46.85f;
|
||||
// According to the datasheet sht21 temperature readings can take up to 85ms
|
||||
this->set_timeout(85, [this]() {
|
||||
uint16_t raw_temperature;
|
||||
if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
raw_temperature = i2c::i2ctohs(raw_temperature);
|
||||
|
||||
uint16_t raw_humidity;
|
||||
if (this->write(&HTU21D_REGISTER_HUMIDITY, 1) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
delay(50); // NOLINT
|
||||
if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
raw_humidity = i2c::i2ctohs(raw_humidity);
|
||||
float temperature = (float(raw_temperature & 0xFFFC)) * 175.72f / 65536.0f - 46.85f;
|
||||
|
||||
float humidity = (float(raw_humidity & 0xFFFC)) * 125.0f / 65536.0f - 6.0f;
|
||||
ESP_LOGD(TAG, "Got Temperature=%.1f°C", temperature);
|
||||
|
||||
int8_t heater_level = this->get_heater_level();
|
||||
if (this->temperature_ != nullptr)
|
||||
this->temperature_->publish_state(temperature);
|
||||
this->status_clear_warning();
|
||||
|
||||
ESP_LOGD(TAG, "Got Temperature=%.1f°C Humidity=%.1f%% Heater Level=%d", temperature, humidity, heater_level);
|
||||
if (this->write(&HTU21D_REGISTER_HUMIDITY, 1) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->temperature_ != nullptr)
|
||||
this->temperature_->publish_state(temperature);
|
||||
if (this->humidity_ != nullptr)
|
||||
this->humidity_->publish_state(humidity);
|
||||
if (this->heater_ != nullptr)
|
||||
this->heater_->publish_state(heater_level);
|
||||
this->status_clear_warning();
|
||||
this->set_timeout(50, [this]() {
|
||||
uint16_t raw_humidity;
|
||||
if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
raw_humidity = i2c::i2ctohs(raw_humidity);
|
||||
|
||||
float humidity = (float(raw_humidity & 0xFFFC)) * 125.0f / 65536.0f - 6.0f;
|
||||
|
||||
int8_t heater_level = this->get_heater_level();
|
||||
|
||||
ESP_LOGD(TAG, "Got Humidity=%.1f%% Heater Level=%d", humidity, heater_level);
|
||||
|
||||
if (this->humidity_ != nullptr)
|
||||
this->humidity_->publish_state(humidity);
|
||||
if (this->heater_ != nullptr)
|
||||
this->heater_->publish_state(heater_level);
|
||||
this->status_clear_warning();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
bool HTU21DComponent::is_heater_enabled() {
|
||||
|
|
Loading…
Reference in a new issue