8 static const char *
const TAG =
"senseair";
9 static const uint8_t SENSEAIR_REQUEST_LENGTH = 8;
10 static const uint8_t SENSEAIR_PPM_STATUS_RESPONSE_LENGTH = 13;
11 static const uint8_t SENSEAIR_ABC_PERIOD_RESPONSE_LENGTH = 7;
12 static const uint8_t SENSEAIR_CAL_RESULT_RESPONSE_LENGTH = 7;
13 static const uint8_t SENSEAIR_COMMAND_GET_PPM_STATUS[] = {0xFE, 0x04, 0x00, 0x00, 0x00, 0x04, 0xE5, 0xC6};
14 static const uint8_t SENSEAIR_COMMAND_CLEAR_ACK_REGISTER[] = {0xFE, 0x06, 0x00, 0x00, 0x00, 0x00, 0x9D, 0xC5};
15 static const uint8_t SENSEAIR_COMMAND_BACKGROUND_CAL[] = {0xFE, 0x06, 0x00, 0x01, 0x7C, 0x06, 0x6C, 0xC7};
16 static const uint8_t SENSEAIR_COMMAND_BACKGROUND_CAL_RESULT[] = {0xFE, 0x03, 0x00, 0x00, 0x00, 0x01, 0x90, 0x05};
17 static const uint8_t SENSEAIR_COMMAND_ABC_ENABLE[] = {0xFE, 0x06, 0x00, 0x1F, 0x00, 0xB4, 0xAC, 0x74};
18 static const uint8_t SENSEAIR_COMMAND_ABC_DISABLE[] = {0xFE, 0x06, 0x00, 0x1F, 0x00, 0x00, 0xAC, 0x03};
19 static const uint8_t SENSEAIR_COMMAND_ABC_GET_PERIOD[] = {0xFE, 0x03, 0x00, 0x1F, 0x00, 0x01, 0xA1, 0xC3};
22 uint8_t response[SENSEAIR_PPM_STATUS_RESPONSE_LENGTH];
23 if (!this->
senseair_write_command_(SENSEAIR_COMMAND_GET_PPM_STATUS, response, SENSEAIR_PPM_STATUS_RESPONSE_LENGTH)) {
24 ESP_LOGW(TAG,
"Reading data from SenseAir failed!");
29 if (response[0] != 0xFE || response[1] != 0x04) {
30 ESP_LOGW(TAG,
"Invalid preamble from SenseAir! %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x %02x",
31 response[0], response[1], response[2], response[3], response[4], response[5], response[6], response[7],
32 response[8], response[9], response[10], response[11], response[12]);
38 ESP_LOGV(TAG,
" ... %02x", b);
40 ESP_LOGV(TAG,
" ... nothing read");
46 uint16_t calc_checksum =
crc16(response, 11);
47 uint16_t resp_checksum = (uint16_t(response[12]) << 8) | response[11];
48 if (resp_checksum != calc_checksum) {
49 ESP_LOGW(TAG,
"SenseAir checksum doesn't match: 0x%02X!=0x%02X", resp_checksum, calc_checksum);
55 const uint8_t
length = response[2];
56 const uint16_t
status = (uint16_t(response[3]) << 8) | response[4];
57 const int16_t ppm = int16_t((response[length + 1] << 8) | response[length + 2]);
59 ESP_LOGD(TAG,
"SenseAir Received CO₂=%dppm Status=0x%02X", ppm, status);
66 ESP_LOGD(TAG,
"SenseAir Starting background calibration");
67 uint8_t command_length =
sizeof(SENSEAIR_COMMAND_CLEAR_ACK_REGISTER) /
sizeof(SENSEAIR_COMMAND_CLEAR_ACK_REGISTER[0]);
68 uint8_t response[command_length];
74 ESP_LOGD(TAG,
"SenseAir Requesting background calibration result");
75 uint8_t response[SENSEAIR_CAL_RESULT_RESPONSE_LENGTH];
77 SENSEAIR_CAL_RESULT_RESPONSE_LENGTH)) {
78 ESP_LOGE(TAG,
"Requesting background calibration result from SenseAir failed!");
82 if (response[0] != 0xFE || response[1] != 0x03) {
83 ESP_LOGE(TAG,
"Invalid reply from SenseAir! %02x%02x%02x %02x%02x %02x%02x", response[0], response[1], response[2],
84 response[3], response[4], response[5], response[6]);
89 ESP_LOGI(TAG,
"SenseAir Result=%s (%02x%02x%02x %02x%02x %02x%02x)", (response[4] & 0b100000) != 0 ?
"OK" :
"NOT_OK",
90 response[0], response[1], response[2], response[3], response[4], response[5], response[6]);
95 ESP_LOGD(TAG,
"SenseAir Enabling automatic baseline calibration");
96 uint8_t command_length =
sizeof(SENSEAIR_COMMAND_ABC_ENABLE) /
sizeof(SENSEAIR_COMMAND_ABC_ENABLE[0]);
97 uint8_t response[command_length];
103 ESP_LOGD(TAG,
"SenseAir Disabling automatic baseline calibration");
104 uint8_t command_length =
sizeof(SENSEAIR_COMMAND_ABC_DISABLE) /
sizeof(SENSEAIR_COMMAND_ABC_DISABLE[0]);
105 uint8_t response[command_length];
110 ESP_LOGD(TAG,
"SenseAir Requesting ABC period");
111 uint8_t response[SENSEAIR_ABC_PERIOD_RESPONSE_LENGTH];
112 if (!this->
senseair_write_command_(SENSEAIR_COMMAND_ABC_GET_PERIOD, response, SENSEAIR_ABC_PERIOD_RESPONSE_LENGTH)) {
113 ESP_LOGE(TAG,
"Requesting ABC period from SenseAir failed!");
117 if (response[0] != 0xFE || response[1] != 0x03) {
118 ESP_LOGE(TAG,
"Invalid reply from SenseAir! %02x%02x%02x %02x%02x %02x%02x", response[0], response[1], response[2],
119 response[3], response[4], response[5], response[6]);
123 const uint16_t hours = (uint16_t(response[3]) << 8) | response[4];
124 ESP_LOGD(TAG,
"SenseAir Read ABC Period: %u hours", hours);
129 if (response ==
nullptr) {
136 this->
write_array(command, SENSEAIR_REQUEST_LENGTH);
138 bool ret = this->
read_array(response, response_length);
144 ESP_LOGCONFIG(TAG,
"SenseAir:");
optional< std::array< uint8_t, N > > read_array()
void write_array(const uint8_t *data, size_t len)
void write_byte(uint8_t data)
void status_set_warning(const char *message="unspecified")
void dump_config() override
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
bool read_byte(uint8_t *data)
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
void background_calibration()
bool senseair_write_command_(const uint8_t *command, uint8_t *response, uint8_t response_length)
sensor::Sensor * co2_sensor_
Implementation of SPI Controller mode.
void background_calibration_result()
void IRAM_ATTR HOT delay(uint32_t ms)