ESPHome  2023.5.5
aht10.cpp
Go to the documentation of this file.
1 // Implementation based on:
2 // - AHT10: https://github.com/Thinary/AHT10
3 // - Official Datasheet (cn):
4 // http://www.aosong.com/userfiles/files/media/aht10%E8%A7%84%E6%A0%BC%E4%B9%A6v1_1%EF%BC%8820191015%EF%BC%89.pdf
5 // - Unofficial Translated Datasheet (en):
6 // https://wiki.liutyi.info/download/attachments/30507639/Aosong_AHT10_en_draft_0c.pdf
7 //
8 // When configured for humidity, the log 'Components should block for at most 20-30ms in loop().' will be generated in
9 // verbose mode. This is due to technical specs of the sensor and can not be avoided.
10 //
11 // According to the datasheet, the component is supposed to respond in more than 75ms. In fact, it can answer almost
12 // immediately for temperature. But for humidity, it takes >90ms to get a valid data. From experience, we have best
13 // results making successive requests; the current implementation makes 3 attempts with a delay of 30ms each time.
14 
15 #include "aht10.h"
16 #include "esphome/core/log.h"
17 #include "esphome/core/hal.h"
18 
19 namespace esphome {
20 namespace aht10 {
21 
22 static const char *const TAG = "aht10";
23 static const uint8_t AHT10_CALIBRATE_CMD[] = {0xE1};
24 static const uint8_t AHT10_MEASURE_CMD[] = {0xAC, 0x33, 0x00};
25 static const uint8_t AHT10_DEFAULT_DELAY = 5; // ms, for calibration and temperature measurement
26 static const uint8_t AHT10_HUMIDITY_DELAY = 30; // ms
27 static const uint8_t AHT10_ATTEMPTS = 3; // safety margin, normally 3 attempts are enough: 3*30=90ms
28 
30  ESP_LOGCONFIG(TAG, "Setting up AHT10...");
31 
32  if (!this->write_bytes(0, AHT10_CALIBRATE_CMD, sizeof(AHT10_CALIBRATE_CMD))) {
33  ESP_LOGE(TAG, "Communication with AHT10 failed!");
34  this->mark_failed();
35  return;
36  }
37  uint8_t data = 0;
38  if (this->write(&data, 1) != i2c::ERROR_OK) {
39  ESP_LOGD(TAG, "Communication with AHT10 failed!");
40  this->mark_failed();
41  return;
42  }
43  delay(AHT10_DEFAULT_DELAY);
44  if (this->read(&data, 1) != i2c::ERROR_OK) {
45  ESP_LOGD(TAG, "Communication with AHT10 failed!");
46  this->mark_failed();
47  return;
48  }
49  if (this->read(&data, 1) != i2c::ERROR_OK) {
50  ESP_LOGD(TAG, "Communication with AHT10 failed!");
51  this->mark_failed();
52  return;
53  }
54  if ((data & 0x68) != 0x08) { // Bit[6:5] = 0b00, NORMAL mode and Bit[3] = 0b1, CALIBRATED
55  ESP_LOGE(TAG, "AHT10 calibration failed!");
56  this->mark_failed();
57  return;
58  }
59 
60  ESP_LOGV(TAG, "AHT10 calibrated");
61 }
62 
64  if (!this->write_bytes(0, AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD))) {
65  ESP_LOGE(TAG, "Communication with AHT10 failed!");
66  this->status_set_warning();
67  return;
68  }
69  uint8_t data[6];
70  uint8_t delay_ms = AHT10_DEFAULT_DELAY;
71  if (this->humidity_sensor_ != nullptr)
72  delay_ms = AHT10_HUMIDITY_DELAY;
73  bool success = false;
74  for (int i = 0; i < AHT10_ATTEMPTS; ++i) {
75  ESP_LOGVV(TAG, "Attempt %d at %6u", i, millis());
76  delay(delay_ms);
77  if (this->read(data, 6) != i2c::ERROR_OK) {
78  ESP_LOGD(TAG, "Communication with AHT10 failed, waiting...");
79  continue;
80  }
81 
82  if ((data[0] & 0x80) == 0x80) { // Bit[7] = 0b1, device is busy
83  ESP_LOGD(TAG, "AHT10 is busy, waiting...");
84  } else if (data[1] == 0x0 && data[2] == 0x0 && (data[3] >> 4) == 0x0) {
85  // Unrealistic humidity (0x0)
86  if (this->humidity_sensor_ == nullptr) {
87  ESP_LOGVV(TAG, "ATH10 Unrealistic humidity (0x0), but humidity is not required");
88  break;
89  } else {
90  ESP_LOGD(TAG, "ATH10 Unrealistic humidity (0x0), retrying...");
91  if (!this->write_bytes(0, AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD))) {
92  ESP_LOGE(TAG, "Communication with AHT10 failed!");
93  this->status_set_warning();
94  return;
95  }
96  }
97  } else {
98  // data is valid, we can break the loop
99  ESP_LOGVV(TAG, "Answer at %6u", millis());
100  success = true;
101  break;
102  }
103  }
104  if (!success || (data[0] & 0x80) == 0x80) {
105  ESP_LOGE(TAG, "Measurements reading timed-out!");
106  this->status_set_warning();
107  return;
108  }
109 
110  uint32_t raw_temperature = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5];
111  uint32_t raw_humidity = ((data[1] << 16) | (data[2] << 8) | data[3]) >> 4;
112 
113  float temperature = ((200.0f * (float) raw_temperature) / 1048576.0f) - 50.0f;
114  float humidity;
115  if (raw_humidity == 0) { // unrealistic value
116  humidity = NAN;
117  } else {
118  humidity = (float) raw_humidity * 100.0f / 1048576.0f;
119  }
120 
121  if (this->temperature_sensor_ != nullptr) {
122  this->temperature_sensor_->publish_state(temperature);
123  }
124  if (this->humidity_sensor_ != nullptr) {
125  if (std::isnan(humidity)) {
126  ESP_LOGW(TAG, "Invalid humidity! Sensor reported 0%% Hum");
127  }
128  this->humidity_sensor_->publish_state(humidity);
129  }
130  this->status_clear_warning();
131 }
132 
134 
136  ESP_LOGCONFIG(TAG, "AHT10:");
137  LOG_I2C_DEVICE(this);
138  if (this->is_failed()) {
139  ESP_LOGE(TAG, "Communication with AHT10 failed!");
140  }
141  LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
142  LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
143 }
144 
145 } // namespace aht10
146 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
ErrorCode read(uint8_t *data, size_t len)
Definition: i2c.h:48
sensor::Sensor * humidity_sensor_
Definition: aht10.h:22
float temperature
Definition: qmp6988.h:71
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:27
void status_clear_warning()
Definition: component.cpp:153
void update() override
Definition: aht10.cpp:63
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:56
void status_set_warning()
Definition: component.cpp:145
void dump_config() override
Definition: aht10.cpp:135
sensor::Sensor * temperature_sensor_
Definition: aht10.h:21
float get_setup_priority() const override
Definition: aht10.cpp:133
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:112
Definition: a4988.cpp:4
void setup() override
Definition: aht10.cpp:29
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:28
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:109