ESPHome  2024.11.1
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"
4 #include "esphome/core/helpers.h"
5 
6 namespace esphome {
7 namespace iaqcore {
8 
9 static const char *const TAG = "iaqcore";
10 
11 enum IAQCoreErrorCode : uint8_t { ERROR_OK = 0, ERROR_RUNIN = 0x10, ERROR_BUSY = 0x01, ERROR_ERROR = 0x80 };
12 
13 struct SensorData {
14  uint16_t co2;
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 
75  this->status_clear_warning();
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
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:187
void setup() override
Definition: iaqcore.cpp:27
void dump_config() override
Definition: iaqcore.cpp:87
No error found during execution of method.
Definition: i2c_bus.h:13
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:183
uint8_t status
Definition: bl0942.h:74
void update() override
Definition: iaqcore.cpp:35
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7