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;
26 static const uint8_t AHT10_HUMIDITY_DELAY = 30;
27 static const uint8_t AHT10_ATTEMPTS = 3;
30 ESP_LOGCONFIG(TAG,
"Setting up AHT10...");
32 if (!this->
write_bytes(0, AHT10_CALIBRATE_CMD,
sizeof(AHT10_CALIBRATE_CMD))) {
33 ESP_LOGE(TAG,
"Communication with AHT10 failed!");
39 ESP_LOGD(TAG,
"Communication with AHT10 failed!");
43 delay(AHT10_DEFAULT_DELAY);
45 ESP_LOGD(TAG,
"Communication with AHT10 failed!");
50 ESP_LOGD(TAG,
"Communication with AHT10 failed!");
54 if ((data & 0x68) != 0x08) {
55 ESP_LOGE(TAG,
"AHT10 calibration failed!");
60 ESP_LOGV(TAG,
"AHT10 calibrated");
64 if (!this->
write_bytes(0, AHT10_MEASURE_CMD,
sizeof(AHT10_MEASURE_CMD))) {
65 ESP_LOGE(TAG,
"Communication with AHT10 failed!");
70 uint8_t delay_ms = AHT10_DEFAULT_DELAY;
72 delay_ms = AHT10_HUMIDITY_DELAY;
74 for (
int i = 0; i < AHT10_ATTEMPTS; ++i) {
75 ESP_LOGVV(TAG,
"Attempt %d at %6u", i,
millis());
78 ESP_LOGD(TAG,
"Communication with AHT10 failed, waiting...");
82 if ((data[0] & 0x80) == 0x80) {
83 ESP_LOGD(TAG,
"AHT10 is busy, waiting...");
84 }
else if (data[1] == 0x0 && data[2] == 0x0 && (data[3] >> 4) == 0x0) {
87 ESP_LOGVV(TAG,
"ATH10 Unrealistic humidity (0x0), but humidity is not required");
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!");
99 ESP_LOGVV(TAG,
"Answer at %6u",
millis());
104 if (!success || (data[0] & 0x80) == 0x80) {
105 ESP_LOGE(TAG,
"Measurements reading timed-out!");
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;
113 float temperature = ((200.0f * (float) raw_temperature) / 1048576.0f) - 50.0f;
115 if (raw_humidity == 0) {
118 humidity = (float) raw_humidity * 100.0f / 1048576.0f;
125 if (std::isnan(humidity)) {
126 ESP_LOGW(TAG,
"Invalid humidity! Sensor reported 0%% Hum");
136 ESP_LOGCONFIG(TAG,
"AHT10:");
137 LOG_I2C_DEVICE(
this);
139 ESP_LOGE(TAG,
"Communication with AHT10 failed!");
const float DATA
For components that import data from directly connected sensors like DHT.
ErrorCode read(uint8_t *data, size_t len)
sensor::Sensor * humidity_sensor_
uint32_t IRAM_ATTR HOT millis()
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
void status_set_warning()
void dump_config() override
sensor::Sensor * temperature_sensor_
float get_setup_priority() const override
virtual void mark_failed()
Mark this component as failed.
void IRAM_ATTR HOT delay(uint32_t ms)
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)