5 static const char *
const TAG =
"ags10";
8 static const uint8_t REG_TVOC = 0x00;
10 static const uint8_t REG_CALIBRATION = 0x01;
12 static const uint8_t REG_VERSION = 0x11;
14 static const uint8_t REG_RESISTANCE = 0x20;
16 static const uint8_t REG_ADDRESS = 0x21;
19 static const uint16_t ZP_CURRENT = 0x0000;
21 static const uint16_t ZP_DEFAULT = 0xFFFF;
24 ESP_LOGCONFIG(TAG,
"Setting up ags10...");
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%08X", *resistance);
43 ESP_LOGE(TAG,
"AGS10 Sensor Resistance: unknown");
46 ESP_LOGD(TAG,
"Sensor initialized");
60 ESP_LOGCONFIG(TAG,
"AGS10:");
62 switch (this->error_code_) {
66 ESP_LOGE(TAG,
"Communication with AGS10 failed!");
69 ESP_LOGE(TAG,
"The crc check failed");
72 ESP_LOGE(TAG,
"AGS10 is not ready to return TVOC data or sensor in pre-heat stage.");
75 ESP_LOGE(TAG,
"AGS10 returns TVOC data in unsupported units.");
78 ESP_LOGE(TAG,
"Unknown error: %d", this->error_code_);
81 LOG_UPDATE_INTERVAL(
this);
82 LOG_SENSOR(
" ",
"TVOC Sensor", this->
tvoc_);
83 LOG_SENSOR(
" ",
"Firmware Version Sensor", this->
version_);
84 LOG_SENSOR(
" ",
"Resistance Sensor", this->
resistance_);
91 uint8_t rev_newaddress = ~newaddress;
92 std::array<uint8_t, 5> data{newaddress, rev_newaddress, newaddress, rev_newaddress, 0};
97 ESP_LOGE(TAG,
"couldn't write the new I2C address 0x%02X", newaddress);
101 ESP_LOGW(TAG,
"changed I2C address to 0x%02X", newaddress);
102 this->error_code_ =
NONE;
112 std::array<uint8_t, 5> data{0x00, 0x0C, (uint8_t) ((value >> 8) & 0xFF), (uint8_t) (value & 0xFF), 0};
117 ESP_LOGE(TAG,
"unable to set zero-point calibration with 0x%02X", value);
120 if (value == ZP_CURRENT) {
121 ESP_LOGI(TAG,
"zero-point calibration has been set with current resistance");
122 }
else if (value == ZP_DEFAULT) {
123 ESP_LOGI(TAG,
"zero-point calibration has been reset to the factory defaults");
125 ESP_LOGI(TAG,
"zero-point calibration has been set with 0x%02X", value);
127 this->error_code_ =
NONE;
133 auto data = this->read_and_check_<5>(REG_TVOC);
139 auto status_byte = res[0];
141 int units = status_byte & 0x0e;
142 int status_bit = status_byte & 0x01;
144 if (status_bit != 0) {
146 ESP_LOGW(TAG,
"Reading AGS10 data failed: illegal status (not ready or sensor in pre-heat stage)!");
152 ESP_LOGE(TAG,
"Reading AGS10 data failed: unsupported units (%d)!", units);
160 auto data = this->read_and_check_<5>(REG_VERSION);
169 auto data = this->read_and_check_<5>(REG_RESISTANCE);
178 auto data = this->read_bytes<N>(a_register);
179 if (!data.has_value()) {
181 ESP_LOGE(TAG,
"Reading AGS10 version failed!");
186 auto crc_byte = res[
len];
190 ESP_LOGE(TAG,
"Reading AGS10 version failed: crc error!");
198 uint8_t i, byte1,
crc = 0xFF;
199 for (byte1 = 0; byte1 < num; byte1++) {
201 for (i = 0; i < 8; i++) {
203 crc = (crc << 1) ^ 0x31;
optional< uint32_t > read_resistance_()
Reads and returns the resistance of AGS10.
optional< uint32_t > read_tvoc_()
Reads and returns value of TVOC.
bool new_i2c_address(uint8_t newaddress)
Modifies target address of AGS10.
optional< uint8_t > read_version_()
Reads and returns a firmware version of AGS10.
void status_set_warning(const char *message="unspecified")
bool set_zero_point_with_factory_defaults()
Sets zero-point with factory defaults.
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.
void dump_config() override
bool set_zero_point_with_current_resistance()
Sets zero-point with current sensor resistance.
const nullopt_t nullopt((nullopt_t::init()))
void status_clear_warning()
uint8_t calc_crc8_(std::array< uint8_t, N > dat, uint8_t num)
Calculates CRC8 value.
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.
sensor::Sensor * version_
Firmvare version.
sensor::Sensor * tvoc_
TVOC.
sensor::Sensor * resistance_
Resistance.
bool set_zero_point_with(uint16_t value)
Sets zero-point with the value.
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
optional< std::array< uint8_t, N > > read_and_check_(uint8_t a_register)
Read, checks and returns data from the sensor.
enum esphome::ags10::AGS10Component::ErrorCode NONE
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)