8static const char *
const TAG =
"ags10";
11static const uint8_t REG_TVOC = 0x00;
13static const uint8_t REG_CALIBRATION = 0x01;
15static const uint8_t REG_VERSION = 0x11;
17static const uint8_t REG_RESISTANCE = 0x20;
19static const uint8_t REG_ADDRESS = 0x21;
22static const uint16_t ZP_CURRENT = 0x0000;
24static const uint16_t ZP_DEFAULT = 0xFFFF;
27 ESP_LOGCONFIG(TAG,
"Running setup");
31 ESP_LOGD(TAG,
"AGS10 Sensor Version: 0x%02X", *version);
36 ESP_LOGE(TAG,
"AGS10 Sensor Version: unknown");
41 ESP_LOGD(TAG,
"AGS10 Sensor Resistance: 0x%08" PRIX32, *resistance);
46 ESP_LOGE(TAG,
"AGS10 Sensor Resistance: unknown");
49 ESP_LOGD(TAG,
"Sensor initialized");
63 ESP_LOGCONFIG(TAG,
"AGS10:");
65 switch (this->error_code_) {
69 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
72 ESP_LOGE(TAG,
"The crc check failed");
75 ESP_LOGE(TAG,
"AGS10 is not ready to return TVOC data or sensor in pre-heat stage.");
78 ESP_LOGE(TAG,
"AGS10 returns TVOC data in unsupported units.");
81 ESP_LOGE(TAG,
"Unknown error: %d", this->error_code_);
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_);
94 uint8_t rev_newaddress = ~newaddress;
95 std::array<uint8_t, 5> data{newaddress, rev_newaddress, newaddress, rev_newaddress, 0};
100 ESP_LOGE(TAG,
"couldn't write the new I2C address 0x%02X", newaddress);
104 ESP_LOGW(TAG,
"changed I2C address to 0x%02X", newaddress);
105 this->error_code_ =
NONE;
115 std::array<uint8_t, 5> data{0x00, 0x0C, (uint8_t) ((value >> 8) & 0xFF), (uint8_t) (value & 0xFF), 0};
120 ESP_LOGE(TAG,
"unable to set zero-point calibration with 0x%02X", value);
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");
128 ESP_LOGI(TAG,
"zero-point calibration has been set with 0x%02X", value);
130 this->error_code_ =
NONE;
142 auto status_byte = res[0];
144 int units = status_byte & 0x0e;
145 int status_bit = status_byte & 0x01;
147 if (status_bit != 0) {
149 ESP_LOGW(TAG,
"Reading AGS10 data failed: illegal status (not ready or sensor in pre-heat stage)!");
155 ESP_LOGE(TAG,
"Reading AGS10 data failed: unsupported units (%d)!", units);
182 if (!data.has_value()) {
184 ESP_LOGE(TAG,
"Reading AGS10 version failed!");
189 auto crc_byte = res[
len];
193 ESP_LOGE(TAG,
"Reading AGS10 version failed: crc error!");
201 uint8_t i, byte1, crc = 0xFF;
202 for (byte1 = 0; byte1 < num; byte1++) {
204 for (i = 0; i < 8; i++) {
206 crc = (crc << 1) ^ 0x31;
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.
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.
uint8_t calc_crc8_(std::array< uint8_t, N > dat, uint8_t num)
Calculates CRC8 value.
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
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
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.
Providing packet encoding functions for exchanging data with a remote host.
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.
const nullopt_t nullopt((nullopt_t::init()))