7static const char *
const TAG =
"ags10";
10static const uint8_t REG_TVOC = 0x00;
12static const uint8_t REG_CALIBRATION = 0x01;
14static const uint8_t REG_VERSION = 0x11;
16static const uint8_t REG_RESISTANCE = 0x20;
18static const uint8_t REG_ADDRESS = 0x21;
21static const uint16_t ZP_CURRENT = 0x0000;
23static const uint16_t ZP_DEFAULT = 0xFFFF;
28 ESP_LOGD(TAG,
"AGS10 Sensor Version: 0x%02X", *version);
33 ESP_LOGE(TAG,
"AGS10 Sensor Version: unknown");
38 ESP_LOGD(TAG,
"AGS10 Sensor Resistance: 0x%08" PRIX32, *resistance);
43 ESP_LOGE(TAG,
"AGS10 Sensor Resistance: unknown");
58 ESP_LOGCONFIG(TAG,
"AGS10:");
60 switch (this->error_code_) {
64 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
67 ESP_LOGE(TAG,
"The crc check failed");
70 ESP_LOGE(TAG,
"AGS10 is not ready to return TVOC data or sensor in pre-heat stage.");
73 ESP_LOGE(TAG,
"AGS10 returns TVOC data in unsupported units.");
76 ESP_LOGE(TAG,
"Unknown error: %d", this->error_code_);
79 LOG_UPDATE_INTERVAL(
this);
80 LOG_SENSOR(
" ",
"TVOC Sensor", this->
tvoc_);
81 LOG_SENSOR(
" ",
"Firmware Version Sensor", this->
version_);
82 LOG_SENSOR(
" ",
"Resistance Sensor", this->
resistance_);
89 uint8_t rev_newaddress = ~newaddress;
90 std::array<uint8_t, 5> data{newaddress, rev_newaddress, newaddress, rev_newaddress, 0};
91 data[4] =
crc8(data.data(), 4, 0xFF, 0x31,
true);
95 ESP_LOGE(TAG,
"couldn't write the new I2C address 0x%02X", newaddress);
99 ESP_LOGW(TAG,
"changed I2C address to 0x%02X", newaddress);
100 this->error_code_ =
NONE;
110 std::array<uint8_t, 5> data{0x00, 0x0C, (uint8_t) ((value >> 8) & 0xFF), (uint8_t) (value & 0xFF), 0};
111 data[4] =
crc8(data.data(), 4, 0xFF, 0x31,
true);
115 ESP_LOGE(TAG,
"unable to set zero-point calibration with 0x%02X", value);
118 if (value == ZP_CURRENT) {
119 ESP_LOGI(TAG,
"zero-point calibration has been set with current resistance");
120 }
else if (value == ZP_DEFAULT) {
121 ESP_LOGI(TAG,
"zero-point calibration has been reset to the factory defaults");
123 ESP_LOGI(TAG,
"zero-point calibration has been set with 0x%02X", value);
125 this->error_code_ =
NONE;
137 auto status_byte = res[0];
139 int units = status_byte & 0x0e;
140 int status_bit = status_byte & 0x01;
142 if (status_bit != 0) {
144 ESP_LOGW(TAG,
"Reading AGS10 data failed: illegal status (not ready or sensor in pre-heat stage)!");
150 ESP_LOGE(TAG,
"Reading AGS10 data failed: unsupported units (%d)!", units);
177 if (!data.has_value()) {
179 ESP_LOGE(TAG,
"Reading AGS10 version failed!");
180 return optional<std::array<uint8_t, N>>();
184 auto crc_byte = res[
len];
186 if (crc_byte !=
crc8(res.data(),
len, 0xFF, 0x31,
true)) {
188 ESP_LOGE(TAG,
"Reading AGS10 version failed: crc error!");
189 return optional<std::array<uint8_t, N>>();
void status_set_warning()
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.
bool set_zero_point_with_factory_defaults()
Sets zero-point with factory defaults.
optional< uint8_t > read_version_()
Reads and returns a firmware version of AGS10.
enum esphome::ags10::AGS10Component::ErrorCode NONE
sensor::Sensor * tvoc_
TVOC.
bool set_zero_point_with_current_resistance()
Sets zero-point with current sensor resistance.
sensor::Sensor * resistance_
Resistance.
optional< uint32_t > read_resistance_()
Reads and returns the resistance of AGS10.
optional< uint32_t > read_tvoc_()
Reads and returns value of TVOC.
sensor::Sensor * version_
Firmvare version.
bool set_zero_point_with(uint16_t value)
Sets zero-point with the value.
bool new_i2c_address(uint8_t newaddress)
Modifies target address of AGS10.
void dump_config() override
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
void publish_state(float state)
Publish a new state to the front-end.
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.
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.
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.