ESPHome 2025.6.3
Loading...
Searching...
No Matches
ags10.cpp
Go to the documentation of this file.
1#include "ags10.h"
3
4#include <cinttypes>
5
6namespace esphome {
7namespace ags10 {
8static const char *const TAG = "ags10";
9
10// Data acquisition.
11static const uint8_t REG_TVOC = 0x00;
12// Zero-point calibration.
13static const uint8_t REG_CALIBRATION = 0x01;
14// Read version.
15static const uint8_t REG_VERSION = 0x11;
16// Read current resistance.
17static const uint8_t REG_RESISTANCE = 0x20;
18// Modify target address.
19static const uint8_t REG_ADDRESS = 0x21;
20
21// Zero-point calibration with current resistance.
22static const uint16_t ZP_CURRENT = 0x0000;
23// Zero-point reset.
24static const uint16_t ZP_DEFAULT = 0xFFFF;
25
27 ESP_LOGCONFIG(TAG, "Running setup");
28
29 auto version = this->read_version_();
30 if (version) {
31 ESP_LOGD(TAG, "AGS10 Sensor Version: 0x%02X", *version);
32 if (this->version_ != nullptr) {
33 this->version_->publish_state(*version);
34 }
35 } else {
36 ESP_LOGE(TAG, "AGS10 Sensor Version: unknown");
37 }
38
39 auto resistance = this->read_resistance_();
40 if (resistance) {
41 ESP_LOGD(TAG, "AGS10 Sensor Resistance: 0x%08" PRIX32, *resistance);
42 if (this->resistance_ != nullptr) {
43 this->resistance_->publish_state(*resistance);
44 }
45 } else {
46 ESP_LOGE(TAG, "AGS10 Sensor Resistance: unknown");
47 }
48
49 ESP_LOGD(TAG, "Sensor initialized");
50}
51
53 auto tvoc = this->read_tvoc_();
54 if (tvoc) {
55 this->tvoc_->publish_state(*tvoc);
57 } else {
58 this->status_set_warning();
59 }
60}
61
63 ESP_LOGCONFIG(TAG, "AGS10:");
64 LOG_I2C_DEVICE(this);
65 switch (this->error_code_) {
66 case NONE:
67 break;
69 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
70 break;
72 ESP_LOGE(TAG, "The crc check failed");
73 break;
74 case ILLEGAL_STATUS:
75 ESP_LOGE(TAG, "AGS10 is not ready to return TVOC data or sensor in pre-heat stage.");
76 break;
78 ESP_LOGE(TAG, "AGS10 returns TVOC data in unsupported units.");
79 break;
80 default:
81 ESP_LOGE(TAG, "Unknown error: %d", this->error_code_);
82 break;
83 }
84 LOG_UPDATE_INTERVAL(this);
85 LOG_SENSOR(" ", "TVOC Sensor", this->tvoc_);
86 LOG_SENSOR(" ", "Firmware Version Sensor", this->version_);
87 LOG_SENSOR(" ", "Resistance Sensor", this->resistance_);
88}
89
93bool AGS10Component::new_i2c_address(uint8_t newaddress) {
94 uint8_t rev_newaddress = ~newaddress;
95 std::array<uint8_t, 5> data{newaddress, rev_newaddress, newaddress, rev_newaddress, 0};
96 data[4] = calc_crc8_(data, 4);
97 if (!this->write_bytes(REG_ADDRESS, data)) {
98 this->error_code_ = COMMUNICATION_FAILED;
99 this->status_set_warning();
100 ESP_LOGE(TAG, "couldn't write the new I2C address 0x%02X", newaddress);
101 return false;
102 }
103 this->set_i2c_address(newaddress);
104 ESP_LOGW(TAG, "changed I2C address to 0x%02X", newaddress);
105 this->error_code_ = NONE;
106 this->status_clear_warning();
107 return true;
108}
109
111
113
115 std::array<uint8_t, 5> data{0x00, 0x0C, (uint8_t) ((value >> 8) & 0xFF), (uint8_t) (value & 0xFF), 0};
116 data[4] = calc_crc8_(data, 4);
117 if (!this->write_bytes(REG_CALIBRATION, data)) {
118 this->error_code_ = COMMUNICATION_FAILED;
119 this->status_set_warning();
120 ESP_LOGE(TAG, "unable to set zero-point calibration with 0x%02X", value);
121 return false;
122 }
123 if (value == ZP_CURRENT) {
124 ESP_LOGI(TAG, "zero-point calibration has been set with current resistance");
125 } else if (value == ZP_DEFAULT) {
126 ESP_LOGI(TAG, "zero-point calibration has been reset to the factory defaults");
127 } else {
128 ESP_LOGI(TAG, "zero-point calibration has been set with 0x%02X", value);
129 }
130 this->error_code_ = NONE;
131 this->status_clear_warning();
132 return true;
133}
134
136 auto data = this->read_and_check_<5>(REG_TVOC);
137 if (!data) {
138 return nullopt;
139 }
140
141 auto res = *data;
142 auto status_byte = res[0];
143
144 int units = status_byte & 0x0e;
145 int status_bit = status_byte & 0x01;
146
147 if (status_bit != 0) {
148 this->error_code_ = ILLEGAL_STATUS;
149 ESP_LOGW(TAG, "Reading AGS10 data failed: illegal status (not ready or sensor in pre-heat stage)!");
150 return nullopt;
151 }
152
153 if (units != 0) {
154 this->error_code_ = UNSUPPORTED_UNITS;
155 ESP_LOGE(TAG, "Reading AGS10 data failed: unsupported units (%d)!", units);
156 return nullopt;
157 }
158
159 return encode_uint24(res[1], res[2], res[3]);
160}
161
163 auto data = this->read_and_check_<5>(REG_VERSION);
164 if (data) {
165 auto res = *data;
166 return res[3];
167 }
168 return nullopt;
169}
170
172 auto data = this->read_and_check_<5>(REG_RESISTANCE);
173 if (data) {
174 auto res = *data;
175 return encode_uint32(res[0], res[1], res[2], res[3]);
176 }
177 return nullopt;
178}
179
181 auto data = this->read_bytes<N>(a_register);
182 if (!data.has_value()) {
183 this->error_code_ = COMMUNICATION_FAILED;
184 ESP_LOGE(TAG, "Reading AGS10 version failed!");
186 }
187 auto len = N - 1;
188 auto res = *data;
189 auto crc_byte = res[len];
190
191 if (crc_byte != calc_crc8_(res, len)) {
192 this->error_code_ = CRC_CHECK_FAILED;
193 ESP_LOGE(TAG, "Reading AGS10 version failed: crc error!");
195 }
196
197 return data;
198}
199
200template<size_t N> uint8_t AGS10Component::calc_crc8_(std::array<uint8_t, N> dat, uint8_t num) {
201 uint8_t i, byte1, crc = 0xFF;
202 for (byte1 = 0; byte1 < num; byte1++) {
203 crc ^= (dat[byte1]);
204 for (i = 0; i < 8; i++) {
205 if (crc & 0x80) {
206 crc = (crc << 1) ^ 0x31;
207 } else {
208 crc = (crc << 1);
209 }
210 }
211 }
212 return crc;
213}
214} // namespace ags10
215} // namespace esphome
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
optional< std::array< uint8_t, N > > read_and_check_(uint8_t a_register)
Read, checks and returns data from the sensor.
Definition ags10.cpp:180
bool set_zero_point_with_factory_defaults()
Sets zero-point with factory defaults.
Definition ags10.cpp:110
optional< uint8_t > read_version_()
Reads and returns a firmware version of AGS10.
Definition ags10.cpp:162
enum esphome::ags10::AGS10Component::ErrorCode NONE
sensor::Sensor * tvoc_
TVOC.
Definition ags10.h:62
bool set_zero_point_with_current_resistance()
Sets zero-point with current sensor resistance.
Definition ags10.cpp:112
sensor::Sensor * resistance_
Resistance.
Definition ags10.h:72
uint8_t calc_crc8_(std::array< uint8_t, N > dat, uint8_t num)
Calculates CRC8 value.
Definition ags10.cpp:200
optional< uint32_t > read_resistance_()
Reads and returns the resistance of AGS10.
Definition ags10.cpp:171
optional< uint32_t > read_tvoc_()
Reads and returns value of TVOC.
Definition ags10.cpp:135
sensor::Sensor * version_
Firmvare version.
Definition ags10.h:67
bool set_zero_point_with(uint16_t value)
Sets zero-point with the value.
Definition ags10.cpp:114
bool new_i2c_address(uint8_t newaddress)
Modifies target address of AGS10.
Definition ags10.cpp:93
void dump_config() override
Definition ags10.cpp:62
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition i2c.h:252
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:140
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:216
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:302
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition helpers.h:196
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:200
const nullopt_t nullopt((nullopt_t::init()))