mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	AHT10: fix temperature-only operation; add warning/error messages (#6405)
This commit is contained in:
		| @@ -15,7 +15,6 @@ | ||||
| #include "aht10.h" | ||||
| #include "esphome/core/log.h" | ||||
| #include "esphome/core/hal.h" | ||||
| #include <cinttypes> | ||||
|  | ||||
| namespace esphome { | ||||
| namespace aht10 { | ||||
| @@ -27,7 +26,7 @@ static const uint8_t AHT10_MEASURE_CMD[] = {0xAC, 0x33, 0x00}; | ||||
| static const uint8_t AHT10_SOFTRESET_CMD[] = {0xBA}; | ||||
|  | ||||
| static const uint8_t AHT10_DEFAULT_DELAY = 5;     // ms, for initialization and temperature measurement | ||||
| static const uint8_t AHT10_HUMIDITY_DELAY = 30;   // ms | ||||
| static const uint8_t AHT10_READ_DELAY = 80;       // ms, time to wait for conversion result | ||||
| static const uint8_t AHT10_SOFTRESET_DELAY = 30;  // ms | ||||
|  | ||||
| static const uint8_t AHT10_ATTEMPTS = 3;  // safety margin, normally 3 attempts are enough: 3*30=90ms | ||||
| @@ -36,7 +35,6 @@ static const uint8_t AHT10_INIT_ATTEMPTS = 10; | ||||
| static const uint8_t AHT10_STATUS_BUSY = 0x80; | ||||
|  | ||||
| void AHT10Component::setup() { | ||||
|   this->read_delay_ = this->humidity_sensor_ != nullptr ? AHT10_HUMIDITY_DELAY : AHT10_DEFAULT_DELAY; | ||||
|   if (this->write(AHT10_SOFTRESET_CMD, sizeof(AHT10_SOFTRESET_CMD)) != i2c::ERROR_OK) { | ||||
|     ESP_LOGE(TAG, "Reset AHT10 failed!"); | ||||
|   } | ||||
| @@ -87,19 +85,19 @@ void AHT10Component::setup() { | ||||
| void AHT10Component::restart_read_() { | ||||
|   if (this->read_count_ == AHT10_ATTEMPTS) { | ||||
|     this->read_count_ = 0; | ||||
|     ESP_LOGE(TAG, "Measurements reading timed-out!"); | ||||
|     this->status_set_error(); | ||||
|     this->status_set_error("Measurements reading timed-out!"); | ||||
|     return; | ||||
|   } | ||||
|   this->read_count_++; | ||||
|   this->set_timeout(this->read_delay_, [this]() { this->read_data_(); }); | ||||
|   this->set_timeout(AHT10_READ_DELAY, [this]() { this->read_data_(); }); | ||||
| } | ||||
|  | ||||
| void AHT10Component::read_data_() { | ||||
|   uint8_t data[6]; | ||||
|   ESP_LOGD(TAG, "Read attempt %d at %ums", this->read_count_, (unsigned) (millis() - this->start_time_)); | ||||
|   if (this->read_count_ > 1) | ||||
|     ESP_LOGD(TAG, "Read attempt %d at %ums", this->read_count_, (unsigned) (millis() - this->start_time_)); | ||||
|   if (this->read(data, 6) != i2c::ERROR_OK) { | ||||
|     ESP_LOGD(TAG, "Communication with AHT10 failed, waiting..."); | ||||
|     this->status_set_warning("AHT10 read failed, retrying soon"); | ||||
|     this->restart_read_(); | ||||
|     return; | ||||
|   } | ||||
| @@ -116,14 +114,14 @@ void AHT10Component::read_data_() { | ||||
|     } else { | ||||
|       ESP_LOGD(TAG, "ATH10 Unrealistic humidity (0x0), retrying..."); | ||||
|       if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) { | ||||
|         ESP_LOGE(TAG, "Communication with AHT10 failed!"); | ||||
|         this->status_set_warning(); | ||||
|         this->status_set_warning("Communication with AHT10 failed!"); | ||||
|       } | ||||
|       this->restart_read_(); | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
|   ESP_LOGD(TAG, "Success at %ums", (unsigned) (millis() - this->start_time_)); | ||||
|   if (this->read_count_ > 1) | ||||
|     ESP_LOGD(TAG, "Success at %ums", (unsigned) (millis() - this->start_time_)); | ||||
|   uint32_t raw_temperature = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5]; | ||||
|   uint32_t raw_humidity = ((data[1] << 16) | (data[2] << 8) | data[3]) >> 4; | ||||
|  | ||||
| @@ -151,8 +149,7 @@ void AHT10Component::update() { | ||||
|     return; | ||||
|   this->start_time_ = millis(); | ||||
|   if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) { | ||||
|     ESP_LOGE(TAG, "Communication with AHT10 failed!"); | ||||
|     this->status_set_warning(); | ||||
|     this->status_set_warning("Communication with AHT10 failed!"); | ||||
|     return; | ||||
|   } | ||||
|   this->restart_read_(); | ||||
|   | ||||
| @@ -27,7 +27,6 @@ class AHT10Component : public PollingComponent, public i2c::I2CDevice { | ||||
|   sensor::Sensor *humidity_sensor_{nullptr}; | ||||
|   AHT10Variant variant_{}; | ||||
|   unsigned read_count_{}; | ||||
|   unsigned read_delay_{}; | ||||
|   void read_data_(); | ||||
|   void restart_read_(); | ||||
|   uint32_t start_time_{}; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user