ESPHome 2025.5.0
Loading...
Searching...
No Matches
iaqcore.cpp
Go to the documentation of this file.
1#include "iaqcore.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.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
13struct SensorData {
14 uint16_t co2;
15 IAQCoreErrorCode status;
16 int32_t resistance;
17 uint16_t tvoc;
18
19 SensorData(const uint8_t *buffer) {
20 this->co2 = encode_uint16(buffer[0], buffer[1]);
21 this->status = static_cast<IAQCoreErrorCode>(buffer[2]);
22 this->resistance = encode_uint32(buffer[3], buffer[4], buffer[5], buffer[6]);
23 this->tvoc = encode_uint16(buffer[7], buffer[8]);
24 }
25};
26
28 if (this->write(nullptr, 0) != i2c::ERROR_OK) {
29 ESP_LOGD(TAG, "Communication failed!");
30 this->mark_failed();
31 return;
32 }
33}
34
36 uint8_t buffer[sizeof(SensorData)];
37
38 if (this->read_register(0xB5, buffer, sizeof(buffer), false) != 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 if (this->co2_ != nullptr) {
69 this->co2_->publish_state(data.co2);
70 }
71 if (this->tvoc_ != nullptr) {
72 this->tvoc_->publish_state(data.tvoc);
73 }
74
76}
77
79 if (this->co2_ != nullptr) {
80 this->co2_->publish_state(NAN);
81 }
82 if (this->tvoc_ != nullptr) {
83 this->tvoc_->publish_state(NAN);
84 }
85}
86
88 ESP_LOGCONFIG(TAG, "AMS iAQ Core:");
89 LOG_I2C_DEVICE(this);
90 LOG_UPDATE_INTERVAL(this);
91 if (this->is_failed()) {
92 ESP_LOGE(TAG, "Communication with AMS iAQ Core failed!");
93 }
94 LOG_SENSOR(" ", "CO2", this->co2_);
95 LOG_SENSOR(" ", "TVOC", this->tvoc_);
96}
97
98} // namespace iaqcore
99} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:10
void dump_config() override
Definition iaqcore.cpp:87
sensor::Sensor * tvoc_
Definition iaqcore.h:23
void setup() override
Definition iaqcore.cpp:27
sensor::Sensor * co2_
Definition iaqcore.h:22
void update() override
Definition iaqcore.cpp:35
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
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:195
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:191