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