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};
29static const uint8_t AHT10_DEFAULT_DELAY = 5;
30static const uint8_t AHT10_READ_DELAY = 80;
31static const uint8_t AHT10_SOFTRESET_DELAY = 30;
33static const uint8_t AHT10_ATTEMPTS = 3;
34static const uint8_t AHT10_INIT_ATTEMPTS = 10;
36static const uint8_t AHT10_STATUS_BUSY = 0x80;
38static const float AHT10_DIVISOR = 1048576.0f;
41 ESP_LOGCONFIG(TAG,
"Running setup");
44 ESP_LOGE(TAG,
"Reset failed");
46 delay(AHT10_SOFTRESET_DELAY);
51 error_code = this->
write(AHT20_INITIALIZE_CMD,
sizeof(AHT20_INITIALIZE_CMD));
54 error_code = this->
write(AHT10_INITIALIZE_CMD,
sizeof(AHT10_INITIALIZE_CMD));
58 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
62 uint8_t cal_attempts = 0;
63 uint8_t data = AHT10_STATUS_BUSY;
64 while (data & AHT10_STATUS_BUSY) {
65 delay(AHT10_DEFAULT_DELAY);
67 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
72 if (cal_attempts > AHT10_INIT_ATTEMPTS) {
73 ESP_LOGE(TAG,
"Initialization timed out");
78 if ((data & 0x68) != 0x08) {
79 ESP_LOGE(TAG,
"Initialization failed");
84 ESP_LOGV(TAG,
"Initialization complete");
108 if ((data[0] & 0x80) == 0x80) {
109 ESP_LOGD(TAG,
"Device busy, will retry");
113 if (data[1] == 0x0 && data[2] == 0x0 && (data[3] >> 4) == 0x0) {
116 ESP_LOGV(TAG,
"Invalid humidity (reading not required)");
118 ESP_LOGD(TAG,
"Invalid humidity, retrying");
129 uint32_t raw_temperature =
encode_uint24(data[3] & 0xF, data[4], data[5]);
130 uint32_t raw_humidity =
encode_uint24(data[1], data[2], data[3]) >> 4;
133 float temperature = ((200.0f *
static_cast<float>(raw_temperature)) / AHT10_DIVISOR) - 50.0f;
137 float humidity = raw_humidity == 0 ? NAN :
static_cast<float>(raw_humidity) * 100.0f / AHT10_DIVISOR;
138 if (std::isnan(humidity)) {
139 ESP_LOGW(TAG,
"Invalid humidity reading (0%%), ");
160 ESP_LOGCONFIG(TAG,
"AHT10:");
161 LOG_I2C_DEVICE(
this);
163 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message="unspecified")
void status_set_error(const char *message="unspecified")
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 dump_config() override
sensor::Sensor * temperature_sensor_
sensor::Sensor * humidity_sensor_
float get_setup_priority() const override
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
void publish_state(float state)
Publish a new state to the front-end.
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
@ ERROR_OK
No error found during execution of method.
@ ERROR_INVALID_ARGUMENT
method called invalid argument(s)
const float DATA
For components that import data from directly connected sensors like DHT.
Providing packet encoding functions for exchanging data with a remote host.
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.
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()