ESPHome 2026.3.0
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 {
7namespace iaqcore {
8
9static const char *const TAG = "iaqcore";
10
11enum IAQCoreErrorCode : uint8_t { ERROR_OK = 0, ERROR_RUNIN = 0x10, ERROR_BUSY = 0x01, ERROR_ERROR = 0x80 };
12
13static constexpr size_t SENSOR_DATA_LENGTH = 9;
14
15struct SensorData {
16 int32_t resistance;
17 uint16_t co2;
18 uint16_t tvoc;
19 IAQCoreErrorCode status;
20
21 SensorData(const uint8_t *buffer) {
22 this->co2 = encode_uint16(buffer[0], buffer[1]);
23 this->status = static_cast<IAQCoreErrorCode>(buffer[2]);
24 this->resistance = encode_uint32(buffer[3], buffer[4], buffer[5], buffer[6]);
25 this->tvoc = encode_uint16(buffer[7], buffer[8]);
26 }
27};
28
30 if (this->write(nullptr, 0) != i2c::ERROR_OK) {
31 ESP_LOGD(TAG, ESP_LOG_MSG_COMM_FAIL);
32 this->mark_failed();
33 return;
34 }
35}
36
38 uint8_t buffer[SENSOR_DATA_LENGTH];
39
40 if (this->read_register(0xB5, buffer, SENSOR_DATA_LENGTH) != i2c::ERROR_OK) {
41 ESP_LOGD(TAG, "Read failed");
42 this->status_set_warning();
43 this->publish_nans_();
44 return;
45 }
46
47 SensorData data(buffer);
48
49 switch (data.status) {
50 case ERROR_OK:
51 ESP_LOGD(TAG, "OK");
52 break;
53 case ERROR_RUNIN:
54 ESP_LOGI(TAG, "Warming up");
55 break;
56 case ERROR_BUSY:
57 ESP_LOGI(TAG, "Busy");
58 break;
59 case ERROR_ERROR:
60 ESP_LOGE(TAG, "Error");
61 break;
62 }
63
64 if (data.status != ERROR_OK) {
65 this->status_set_warning();
66 this->publish_nans_();
67 return;
68 }
69
70 if (this->co2_ != nullptr) {
71 this->co2_->publish_state(data.co2);
72 }
73 if (this->tvoc_ != nullptr) {
74 this->tvoc_->publish_state(data.tvoc);
75 }
76
78}
79
81 if (this->co2_ != nullptr) {
82 this->co2_->publish_state(NAN);
83 }
84 if (this->tvoc_ != nullptr) {
85 this->tvoc_->publish_state(NAN);
86 }
87}
88
90 ESP_LOGCONFIG(TAG, "AMS iAQ Core:");
91 LOG_I2C_DEVICE(this);
92 LOG_UPDATE_INTERVAL(this);
93 if (this->is_failed()) {
94 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
95 }
96 LOG_SENSOR(" ", "CO2", this->co2_);
97 LOG_SENSOR(" ", "TVOC", this->tvoc_);
98}
99
100} // namespace iaqcore
101} // namespace esphome
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:233
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
Definition component.h:254
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:89
sensor::Sensor * tvoc_
Definition iaqcore.h:21
void setup() override
Definition iaqcore.cpp:29
sensor::Sensor * co2_
Definition iaqcore.h:20
void update() override
Definition iaqcore.cpp:37
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:736
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:728