mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Improved sensor readings in htu21d component. (#5839)
This commit is contained in:
		| @@ -39,12 +39,14 @@ void HTU21DComponent::dump_config() { | |||||||
|   LOG_SENSOR("  ", "Humidity", this->humidity_); |   LOG_SENSOR("  ", "Humidity", this->humidity_); | ||||||
| } | } | ||||||
| void HTU21DComponent::update() { | void HTU21DComponent::update() { | ||||||
|   uint16_t raw_temperature; |  | ||||||
|   if (this->write(&HTU21D_REGISTER_TEMPERATURE, 1) != i2c::ERROR_OK) { |   if (this->write(&HTU21D_REGISTER_TEMPERATURE, 1) != i2c::ERROR_OK) { | ||||||
|     this->status_set_warning(); |     this->status_set_warning(); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|   delay(50);  // NOLINT |  | ||||||
|  |   // 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) { |     if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) { | ||||||
|       this->status_set_warning(); |       this->status_set_warning(); | ||||||
|       return; |       return; | ||||||
| @@ -53,12 +55,19 @@ void HTU21DComponent::update() { | |||||||
|  |  | ||||||
|     float temperature = (float(raw_temperature & 0xFFFC)) * 175.72f / 65536.0f - 46.85f; |     float temperature = (float(raw_temperature & 0xFFFC)) * 175.72f / 65536.0f - 46.85f; | ||||||
|  |  | ||||||
|   uint16_t raw_humidity; |     ESP_LOGD(TAG, "Got Temperature=%.1f°C", temperature); | ||||||
|  |  | ||||||
|  |     if (this->temperature_ != nullptr) | ||||||
|  |       this->temperature_->publish_state(temperature); | ||||||
|  |     this->status_clear_warning(); | ||||||
|  |  | ||||||
|     if (this->write(&HTU21D_REGISTER_HUMIDITY, 1) != i2c::ERROR_OK) { |     if (this->write(&HTU21D_REGISTER_HUMIDITY, 1) != i2c::ERROR_OK) { | ||||||
|       this->status_set_warning(); |       this->status_set_warning(); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|   delay(50);  // NOLINT |  | ||||||
|  |     this->set_timeout(50, [this]() { | ||||||
|  |       uint16_t raw_humidity; | ||||||
|       if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) { |       if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) { | ||||||
|         this->status_set_warning(); |         this->status_set_warning(); | ||||||
|         return; |         return; | ||||||
| @@ -69,15 +78,15 @@ void HTU21DComponent::update() { | |||||||
|  |  | ||||||
|       int8_t heater_level = this->get_heater_level(); |       int8_t heater_level = this->get_heater_level(); | ||||||
|  |  | ||||||
|   ESP_LOGD(TAG, "Got Temperature=%.1f°C Humidity=%.1f%% Heater Level=%d", temperature, humidity, heater_level); |       ESP_LOGD(TAG, "Got Humidity=%.1f%% Heater Level=%d", humidity, heater_level); | ||||||
|  |  | ||||||
|   if (this->temperature_ != nullptr) |  | ||||||
|     this->temperature_->publish_state(temperature); |  | ||||||
|       if (this->humidity_ != nullptr) |       if (this->humidity_ != nullptr) | ||||||
|         this->humidity_->publish_state(humidity); |         this->humidity_->publish_state(humidity); | ||||||
|       if (this->heater_ != nullptr) |       if (this->heater_ != nullptr) | ||||||
|         this->heater_->publish_state(heater_level); |         this->heater_->publish_state(heater_level); | ||||||
|       this->status_clear_warning(); |       this->status_clear_warning(); | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
| } | } | ||||||
|  |  | ||||||
| bool HTU21DComponent::is_heater_enabled() { | bool HTU21DComponent::is_heater_enabled() { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user