ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
iaqcore.cpp
Go to the documentation of this file.
1#include "iaqcore.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome::iaqcore {
7
8static const char *const TAG = "iaqcore";
9
10enum IAQCoreErrorCode : uint8_t { ERROR_OK = 0, ERROR_RUNIN = 0x10, ERROR_BUSY = 0x01, ERROR_ERROR = 0x80 };
11
12static constexpr size_t SENSOR_DATA_LENGTH = 9;
13
14struct SensorData {
15 int32_t resistance;
16 uint16_t co2;
17 uint16_t tvoc;
18 IAQCoreErrorCode status;
19
20 SensorData(const uint8_t *buffer) {
21 this->co2 = encode_uint16(buffer[0], buffer[1]);
22 this->status = static_cast<IAQCoreErrorCode>(buffer[2]);
23 this->resistance = encode_uint32(buffer[3], buffer[4], buffer[5], buffer[6]);
24 this->tvoc = encode_uint16(buffer[7], buffer[8]);
25 }
26};
27
29 if (this->write(nullptr, 0) != i2c::ERROR_OK) {
30 ESP_LOGD(TAG, ESP_LOG_MSG_COMM_FAIL);
31 this->mark_failed();
32 return;
33 }
34}
35
37 uint8_t buffer[SENSOR_DATA_LENGTH];
38
39 if (this->read_register(0xB5, buffer, SENSOR_DATA_LENGTH) != i2c::ERROR_OK) {
40 ESP_LOGD(TAG, "Read failed");
41 this->status_set_warning();
42 this->publish_nans_();
43 return;
44 }
45
46 SensorData data(buffer);
47
48 switch (data.status) {
49 case ERROR_OK:
50 ESP_LOGD(TAG, "OK");
51 break;
52 case ERROR_RUNIN:
53 ESP_LOGI(TAG, "Warming up");
54 break;
55 case ERROR_BUSY:
56 ESP_LOGI(TAG, "Busy");
57 break;
58 case ERROR_ERROR:
59 ESP_LOGE(TAG, "Error");
60 break;
61 }
62
63 if (data.status != ERROR_OK) {
64 this->status_set_warning();
65 this->publish_nans_();
66 return;
67 }
68
69 if (this->co2_ != nullptr) {
70 this->co2_->publish_state(data.co2);
71 }
72 if (this->tvoc_ != nullptr) {
73 this->tvoc_->publish_state(data.tvoc);
74 }
75
77}
78
80 if (this->co2_ != nullptr) {
81 this->co2_->publish_state(NAN);
82 }
83 if (this->tvoc_ != nullptr) {
84 this->tvoc_->publish_state(NAN);
85 }
86}
87
89 ESP_LOGCONFIG(TAG, "AMS iAQ Core:");
90 LOG_I2C_DEVICE(this);
91 LOG_UPDATE_INTERVAL(this);
92 if (this->is_failed()) {
93 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
94 }
95 LOG_SENSOR(" ", "CO2", this->co2_);
96 LOG_SENSOR(" ", "TVOC", this->tvoc_);
97}
98
99} // namespace esphome::iaqcore
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
void status_clear_warning()
Definition component.h:289
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_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:25
void dump_config() override
Definition iaqcore.cpp:88
sensor::Sensor * tvoc_
Definition iaqcore.h:20
void setup() override
Definition iaqcore.cpp:28
sensor::Sensor * co2_
Definition iaqcore.h:19
void update() override
Definition iaqcore.cpp:36
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:867
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:859