ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
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/hal.h"
18#include "esphome/core/log.h"
19
20namespace esphome::aht10 {
21
22static const char *const TAG = "aht10";
23static const uint8_t AHT10_INITIALIZE_CMD[] = {0xE1, 0x08, 0x00};
24static const uint8_t AHT20_INITIALIZE_CMD[] = {0xBE, 0x08, 0x00};
25static const uint8_t AHT10_MEASURE_CMD[] = {0xAC, 0x33, 0x00};
26static const uint8_t AHT10_SOFTRESET_CMD[] = {0xBA};
27
28static const uint8_t AHT10_DEFAULT_DELAY = 5; // ms, for initialization and temperature measurement
29static const uint8_t AHT10_READ_DELAY = 80; // ms, time to wait for conversion result
30static const uint8_t AHT10_SOFTRESET_DELAY = 30; // ms
31
32static const uint8_t AHT10_ATTEMPTS = 3; // safety margin, normally 3 attempts are enough: 3*30=90ms
33static const uint8_t AHT10_INIT_ATTEMPTS = 10;
34
35static const uint8_t AHT10_STATUS_BUSY = 0x80;
36
37static const float AHT10_DIVISOR = 1048576.0f; // 2^20, used for temperature and humidity calculations
38
40 if (this->write(AHT10_SOFTRESET_CMD, sizeof(AHT10_SOFTRESET_CMD)) != i2c::ERROR_OK) {
41 ESP_LOGE(TAG, "Reset failed");
42 }
43 delay(AHT10_SOFTRESET_DELAY);
44
46 switch (this->variant_) {
48 error_code = this->write(AHT20_INITIALIZE_CMD, sizeof(AHT20_INITIALIZE_CMD));
49 break;
51 error_code = this->write(AHT10_INITIALIZE_CMD, sizeof(AHT10_INITIALIZE_CMD));
52 break;
53 }
54 if (error_code != i2c::ERROR_OK) {
55 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
56 this->mark_failed();
57 return;
58 }
59 uint8_t cal_attempts = 0;
60 uint8_t data = AHT10_STATUS_BUSY;
61 while (data & AHT10_STATUS_BUSY) {
62 delay(AHT10_DEFAULT_DELAY);
63 if (this->read(&data, 1) != i2c::ERROR_OK) {
64 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
65 this->mark_failed();
66 return;
67 }
68 ++cal_attempts;
69 if (cal_attempts > AHT10_INIT_ATTEMPTS) {
70 ESP_LOGE(TAG, "Initialization timed out");
71 this->mark_failed();
72 return;
73 }
74 }
75 if ((data & 0x68) != 0x08) { // Bit[6:5] = 0b00, NORMAL mode and Bit[3] = 0b1, CALIBRATED
76 ESP_LOGE(TAG, "Initialization failed");
77 this->mark_failed();
78 return;
79 }
80}
81
83 if (this->read_count_ == AHT10_ATTEMPTS) {
84 this->read_count_ = 0;
85 this->status_set_error(LOG_STR("Reading timed out"));
86 return;
87 }
88 this->read_count_++;
89 this->set_timeout(AHT10_READ_DELAY, [this]() { this->read_data_(); });
90}
91
93 uint8_t data[6];
94 if (this->read_count_ > 1) {
95 ESP_LOGD(TAG, "Read attempt %d at %ums", this->read_count_, (unsigned) (millis() - this->start_time_));
96 }
97 if (this->read(data, 6) != i2c::ERROR_OK) {
98 this->status_set_warning(LOG_STR("Read failed, will retry"));
99 this->restart_read_();
100 return;
101 }
102
103 if ((data[0] & 0x80) == 0x80) { // Bit[7] = 0b1, device is busy
104 ESP_LOGD(TAG, "Device busy, will retry");
105 this->restart_read_();
106 return;
107 }
108 if (data[1] == 0x0 && data[2] == 0x0 && (data[3] >> 4) == 0x0) {
109 // Invalid humidity (0x0)
110 if (this->humidity_sensor_ == nullptr) {
111 ESP_LOGV(TAG, "Invalid humidity (reading not required)");
112 } else {
113 ESP_LOGD(TAG, "Invalid humidity, retrying");
114 if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) {
115 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
116 }
117 this->restart_read_();
118 return;
119 }
120 }
121 if (this->read_count_ > 1) {
122 ESP_LOGD(TAG, "Success at %ums", (unsigned) (millis() - this->start_time_));
123 }
124 uint32_t raw_temperature = encode_uint24(data[3] & 0xF, data[4], data[5]);
125 uint32_t raw_humidity = encode_uint24(data[1], data[2], data[3]) >> 4;
126
127 if (this->temperature_sensor_ != nullptr) {
128 float temperature = ((200.0f * static_cast<float>(raw_temperature)) / AHT10_DIVISOR) - 50.0f;
129 this->temperature_sensor_->publish_state(temperature);
130 }
131 if (this->humidity_sensor_ != nullptr) {
132 float humidity = raw_humidity == 0 ? NAN : static_cast<float>(raw_humidity) * 100.0f / AHT10_DIVISOR;
133 if (std::isnan(humidity)) {
134 ESP_LOGW(TAG, "Invalid humidity reading (0%%), ");
135 }
136 this->humidity_sensor_->publish_state(humidity);
137 }
138 this->status_clear_warning();
139 this->read_count_ = 0;
140}
142 if (this->read_count_ != 0)
143 return;
144 this->start_time_ = millis();
145 if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) {
146 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
147 return;
148 }
149 this->restart_read_();
150}
151
153 ESP_LOGCONFIG(TAG, "AHT10:");
154 LOG_I2C_DEVICE(this);
155 if (this->is_failed()) {
156 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
157 }
158 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
159 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
160}
161
162} // namespace esphome::aht10
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
void status_clear_warning()
Definition component.h:289
void dump_config() override
Definition aht10.cpp:152
sensor::Sensor * temperature_sensor_
Definition aht10.h:24
sensor::Sensor * humidity_sensor_
Definition aht10.h:25
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:12
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
@ ERROR_INVALID_ARGUMENT
method called invalid argument(s)
Definition i2c_bus.h:15
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition helpers.h:863
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12