ESPHome 2026.11.0b6
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
8ESPHOME_LOG_TAG(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
28void IAQCore::setup() {
29 if (this->write(nullptr, 0) != i2c::ERROR_OK) {
30 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
31 return;
32 }
33}
34
35void IAQCore::update() {
36 uint8_t buffer[SENSOR_DATA_LENGTH];
37
38 if (this->read_register(0xB5, buffer, SENSOR_DATA_LENGTH) != i2c::ERROR_OK) {
39 ESP_LOGD(TAG, "Read failed");
40 this->status_set_warning();
41 this->publish_nans_();
42 return;
43 }
44
45 SensorData data(buffer);
46
47 switch (data.status) {
48 case ERROR_OK:
49 ESP_LOGD(TAG, "OK");
50 break;
51 case ERROR_RUNIN:
52 ESP_LOGI(TAG, "Warming up");
53 break;
54 case ERROR_BUSY:
55 ESP_LOGI(TAG, "Busy");
56 break;
57 case ERROR_ERROR:
58 ESP_LOGE(TAG, "Error");
59 break;
60 }
61
62 if (data.status != ERROR_OK) {
63 this->status_set_warning();
64 this->publish_nans_();
65 return;
66 }
67
68 PUBLISH_STATE(this->co2_sensor_, data.co2);
69 PUBLISH_STATE(this->tvoc_sensor_, data.tvoc);
70
72}
73
75 PUBLISH_STATE(this->co2_sensor_, NAN);
76 PUBLISH_STATE(this->tvoc_sensor_, NAN);
77}
78
79void IAQCore::dump_config() {
80 ESP_LOGCONFIG(TAG, "AMS iAQ Core:");
81 LOG_I2C_DEVICE(this);
82 LOG_UPDATE_INTERVAL(this);
83 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
84 LOG_SENSOR(" ", "TVOC", this->tvoc_sensor_);
85}
86
87} // namespace esphome::iaqcore
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void status_clear_warning()
Definition component.h:309
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
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:45
uint32_t buffer
Definition gif.cpp:50
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
ESPHOME_LOG_TAG(TAG, "iaqcore")
std::span< const uint8_t > data
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:1051
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:1043