ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
senseair.cpp
Go to the documentation of this file.
1#include "senseair.h"
3#include "esphome/core/log.h"
4
6
7static const char *const TAG = "senseair";
8static const uint8_t SENSEAIR_REQUEST_LENGTH = 8;
9static const uint8_t SENSEAIR_PPM_STATUS_RESPONSE_LENGTH = 13;
10static const uint8_t SENSEAIR_ABC_PERIOD_RESPONSE_LENGTH = 7;
11static const uint8_t SENSEAIR_CAL_RESULT_RESPONSE_LENGTH = 7;
12static const uint8_t SENSEAIR_COMMAND_GET_PPM_STATUS[] = {0xFE, 0x04, 0x00, 0x00, 0x00, 0x04, 0xE5, 0xC6};
13static const uint8_t SENSEAIR_COMMAND_CLEAR_ACK_REGISTER[] = {0xFE, 0x06, 0x00, 0x00, 0x00, 0x00, 0x9D, 0xC5};
14static const uint8_t SENSEAIR_COMMAND_BACKGROUND_CAL[] = {0xFE, 0x06, 0x00, 0x01, 0x7C, 0x06, 0x6C, 0xC7};
15static const uint8_t SENSEAIR_COMMAND_BACKGROUND_CAL_RESULT[] = {0xFE, 0x03, 0x00, 0x00, 0x00, 0x01, 0x90, 0x05};
16static const uint8_t SENSEAIR_COMMAND_ABC_ENABLE[] = {0xFE, 0x06, 0x00, 0x1F, 0x00, 0xB4, 0xAC, 0x74}; // 180 hours
17static const uint8_t SENSEAIR_COMMAND_ABC_DISABLE[] = {0xFE, 0x06, 0x00, 0x1F, 0x00, 0x00, 0xAC, 0x03};
18static const uint8_t SENSEAIR_COMMAND_ABC_GET_PERIOD[] = {0xFE, 0x03, 0x00, 0x1F, 0x00, 0x01, 0xA1, 0xC3};
19
21 uint8_t response[SENSEAIR_PPM_STATUS_RESPONSE_LENGTH];
22 if (!this->senseair_write_command_(SENSEAIR_COMMAND_GET_PPM_STATUS, response, SENSEAIR_PPM_STATUS_RESPONSE_LENGTH)) {
23 ESP_LOGW(TAG, "Reading data from SenseAir failed!");
24 this->status_set_warning();
25 return;
26 }
27
28 if (response[0] != 0xFE || response[1] != 0x04) {
29 ESP_LOGW(TAG, "Invalid preamble from SenseAir! %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x %02x",
30 response[0], response[1], response[2], response[3], response[4], response[5], response[6], response[7],
31 response[8], response[9], response[10], response[11], response[12]);
32
33 this->status_set_warning();
34 while (this->available()) {
35 uint8_t b;
36 if (this->read_byte(&b)) {
37 ESP_LOGV(TAG, " ... %02x", b);
38 } else {
39 ESP_LOGV(TAG, " ... nothing read");
40 }
41 }
42 return;
43 }
44
45 uint16_t calc_checksum = crc16(response, 11);
46 uint16_t resp_checksum = (uint16_t(response[12]) << 8) | response[11];
47 if (resp_checksum != calc_checksum) {
48 ESP_LOGW(TAG, "SenseAir checksum doesn't match: 0x%02X!=0x%02X", resp_checksum, calc_checksum);
49 this->status_set_warning();
50 return;
51 }
52
54 const uint8_t length = response[2];
55 const uint16_t status = encode_uint16(response[3], response[4]);
56 const uint16_t ppm = encode_uint16(response[length + 1], response[length + 2]);
57
58 ESP_LOGD(TAG, "SenseAir Received CO₂=%uppm Status=0x%02X", ppm, status);
59 if (ppm == 0 && (status & SenseAirStatus::OUT_OF_RANGE_ERROR) != 0) {
60 ESP_LOGD(TAG, "Discarding 0 ppm reading with out-of-range status.");
61 return;
62 }
63 if (this->co2_sensor_ != nullptr)
64 this->co2_sensor_->publish_state(ppm);
65}
66
68 // Responses are just echoes but must be read to clear the buffer
69 ESP_LOGD(TAG, "SenseAir Starting background calibration");
70 uint8_t command_length = sizeof(SENSEAIR_COMMAND_CLEAR_ACK_REGISTER) / sizeof(SENSEAIR_COMMAND_CLEAR_ACK_REGISTER[0]);
71 uint8_t response[command_length];
72 this->senseair_write_command_(SENSEAIR_COMMAND_CLEAR_ACK_REGISTER, response, command_length);
73 this->senseair_write_command_(SENSEAIR_COMMAND_BACKGROUND_CAL, response, command_length);
74}
75
77 ESP_LOGD(TAG, "SenseAir Requesting background calibration result");
78 uint8_t response[SENSEAIR_CAL_RESULT_RESPONSE_LENGTH];
79 if (!this->senseair_write_command_(SENSEAIR_COMMAND_BACKGROUND_CAL_RESULT, response,
80 SENSEAIR_CAL_RESULT_RESPONSE_LENGTH)) {
81 ESP_LOGE(TAG, "Requesting background calibration result from SenseAir failed!");
82 return;
83 }
84
85 if (response[0] != 0xFE || response[1] != 0x03) {
86 ESP_LOGE(TAG, "Invalid reply from SenseAir! %02x%02x%02x %02x%02x %02x%02x", response[0], response[1], response[2],
87 response[3], response[4], response[5], response[6]);
88 return;
89 }
90
91 // Check if 5th bit (register CI6) is set
92 ESP_LOGI(TAG, "SenseAir Result=%s (%02x%02x%02x %02x%02x %02x%02x)", (response[4] & 0b100000) != 0 ? "OK" : "NOT_OK",
93 response[0], response[1], response[2], response[3], response[4], response[5], response[6]);
94}
95
97 // Response is just an echo but must be read to clear the buffer
98 ESP_LOGD(TAG, "SenseAir Enabling automatic baseline calibration");
99 uint8_t command_length = sizeof(SENSEAIR_COMMAND_ABC_ENABLE) / sizeof(SENSEAIR_COMMAND_ABC_ENABLE[0]);
100 uint8_t response[command_length];
101 this->senseair_write_command_(SENSEAIR_COMMAND_ABC_ENABLE, response, command_length);
102}
103
105 // Response is just an echo but must be read to clear the buffer
106 ESP_LOGD(TAG, "SenseAir Disabling automatic baseline calibration");
107 uint8_t command_length = sizeof(SENSEAIR_COMMAND_ABC_DISABLE) / sizeof(SENSEAIR_COMMAND_ABC_DISABLE[0]);
108 uint8_t response[command_length];
109 this->senseair_write_command_(SENSEAIR_COMMAND_ABC_DISABLE, response, command_length);
110}
111
113 ESP_LOGD(TAG, "SenseAir Requesting ABC period");
114 uint8_t response[SENSEAIR_ABC_PERIOD_RESPONSE_LENGTH];
115 if (!this->senseair_write_command_(SENSEAIR_COMMAND_ABC_GET_PERIOD, response, SENSEAIR_ABC_PERIOD_RESPONSE_LENGTH)) {
116 ESP_LOGE(TAG, "Requesting ABC period from SenseAir failed!");
117 return;
118 }
119
120 if (response[0] != 0xFE || response[1] != 0x03) {
121 ESP_LOGE(TAG, "Invalid reply from SenseAir! %02x%02x%02x %02x%02x %02x%02x", response[0], response[1], response[2],
122 response[3], response[4], response[5], response[6]);
123 return;
124 }
125
126 const uint16_t hours = (uint16_t(response[3]) << 8) | response[4];
127 ESP_LOGD(TAG, "SenseAir Read ABC Period: %u hours", hours);
128}
129
130bool SenseAirComponent::senseair_write_command_(const uint8_t *command, uint8_t *response, uint8_t response_length) {
131 // Verify we have somewhere to store the response
132 if (response == nullptr) {
133 return false;
134 }
135 // Write wake up byte required by some S8 sensor models
136 this->write_byte(0);
137 this->flush();
138 delay(5);
139 this->write_array(command, SENSEAIR_REQUEST_LENGTH);
140
141 bool ret = this->read_array(response, response_length);
142 this->flush();
143 return ret;
144}
145
147 ESP_LOGCONFIG(TAG, "SenseAir:");
148 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
149 this->check_uart_settings(9600);
150}
151
152} // namespace esphome::senseair
uint8_t status
Definition bl0942.h:8
void status_clear_warning()
Definition component.h:306
bool senseair_write_command_(const uint8_t *command, uint8_t *response, uint8_t response_length)
Definition senseair.cpp:130
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
UARTFlushResult flush()
Definition uart.h:48
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
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...
Definition uart.cpp:16
bool read_byte(uint8_t *data)
Definition uart.h:34
void write_byte(uint8_t data)
Definition uart.h:18
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
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.
Definition helpers.cpp:86
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:859
void HOT delay(uint32_t ms)
Definition hal.cpp:82
uint16_t length
Definition tt21100.cpp:0