ESPHome 2026.11.0b6
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
7ESPHOME_LOG_TAG(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#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
30 char hex_buf[format_hex_pretty_size(sizeof(response))];
31 ESP_LOGW(TAG, "Invalid preamble from SenseAir! %s", format_hex_pretty_to(hex_buf, response, sizeof(response), ' '));
32#endif
33
34 this->status_set_warning();
35 this->discard_available();
36 return;
37 }
38
39 uint16_t calc_checksum = crc16(response, 11);
40 uint16_t resp_checksum = encode_value_le<uint16_t>(&response[11]);
41 if (!this->status_comm_result(resp_checksum == calc_checksum)) {
42 ESP_LOGW(TAG, "SenseAir checksum doesn't match: 0x%02X!=0x%02X", resp_checksum, calc_checksum);
43 return;
44 }
45
46 const uint8_t length = response[2];
47 const uint16_t status = encode_uint16(response[3], response[4]);
48 const uint16_t ppm = encode_uint16(response[length + 1], response[length + 2]);
49
50 ESP_LOGD(TAG, "SenseAir Received CO₂=%uppm Status=0x%02X", ppm, status);
51 if (ppm == 0 && (status & SenseAirStatus::OUT_OF_RANGE_ERROR) != 0) {
52 ESP_LOGD(TAG, "Discarding 0 ppm reading with out-of-range status.");
53 return;
54 }
55 PUBLISH_STATE(this->co2_sensor_, ppm);
56}
57
58void SenseAirComponent::background_calibration() {
59 // Responses are just echoes but must be read to clear the buffer
60 ESP_LOGD(TAG, "SenseAir Starting background calibration");
61 uint8_t command_length = sizeof(SENSEAIR_COMMAND_CLEAR_ACK_REGISTER) / sizeof(SENSEAIR_COMMAND_CLEAR_ACK_REGISTER[0]);
62 uint8_t response[command_length];
63 this->senseair_write_command_(SENSEAIR_COMMAND_CLEAR_ACK_REGISTER, response, command_length);
64 this->senseair_write_command_(SENSEAIR_COMMAND_BACKGROUND_CAL, response, command_length);
65}
66
67void SenseAirComponent::background_calibration_result() {
68 ESP_LOGD(TAG, "SenseAir Requesting background calibration result");
69 uint8_t response[SENSEAIR_CAL_RESULT_RESPONSE_LENGTH];
70 if (!this->senseair_write_command_(SENSEAIR_COMMAND_BACKGROUND_CAL_RESULT, response,
71 SENSEAIR_CAL_RESULT_RESPONSE_LENGTH)) {
72 ESP_LOGE(TAG, "Requesting background calibration result from SenseAir failed!");
73 return;
74 }
75
76 if (response[0] != 0xFE || response[1] != 0x03) {
77#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_ERROR
78 char hex_buf[format_hex_pretty_size(sizeof(response))];
79 ESP_LOGE(TAG, "Invalid reply from SenseAir! %s", format_hex_pretty_to(hex_buf, response, sizeof(response), ' '));
80#endif
81 return;
82 }
83
84 // Check if 5th bit (register CI6) is set
85#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_INFO
86 char hex_buf[format_hex_pretty_size(sizeof(response))];
87 ESP_LOGI(TAG, "SenseAir Result=%s (%s)",
88 (response[4] & 0b100000) != 0 ? LOG_STR_LITERAL("OK") : LOG_STR_LITERAL("NOT_OK"),
89 format_hex_pretty_to(hex_buf, response, sizeof(response), ' '));
90#endif
91}
92
93void SenseAirComponent::abc_enable() {
94 // Response is just an echo but must be read to clear the buffer
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];
98 this->senseair_write_command_(SENSEAIR_COMMAND_ABC_ENABLE, response, command_length);
99}
100
101void SenseAirComponent::abc_disable() {
102 // Response is just an echo but must be read to clear the buffer
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];
106 this->senseair_write_command_(SENSEAIR_COMMAND_ABC_DISABLE, response, command_length);
107}
108
109void SenseAirComponent::abc_get_period() {
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!");
114 return;
115 }
116
117 if (response[0] != 0xFE || response[1] != 0x03) {
118#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_ERROR
119 char hex_buf[format_hex_pretty_size(sizeof(response))];
120 ESP_LOGE(TAG, "Invalid reply from SenseAir! %s", format_hex_pretty_to(hex_buf, response, sizeof(response), ' '));
121#endif
122 return;
123 }
124
125 const uint16_t hours = encode_uint16(response[3], response[4]);
126 ESP_LOGD(TAG, "SenseAir Read ABC Period: %u hours", hours);
127}
128
129bool SenseAirComponent::senseair_write_command_(const uint8_t *command, uint8_t *response, uint8_t response_length) {
130 // Verify we have somewhere to store the response
131 if (response == nullptr) {
132 return false;
133 }
134 // Write wake up byte required by some S8 sensor models
135 this->write_byte(0);
136 this->flush();
137 delay(5);
138 this->write_array(command, SENSEAIR_REQUEST_LENGTH);
139
140 bool ret = this->read_array(response, response_length);
141 this->flush();
142 return ret;
143}
144
145void SenseAirComponent::dump_config() {
146 ESP_LOGCONFIG(TAG, "SenseAir:");
147 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
148}
149
150} // namespace esphome::senseair
uint8_t status
Definition bl0942.h:8
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
virtual void update()=0
bool senseair_write_command_(const uint8_t *command, uint8_t *response, uint8_t response_length)
Definition senseair.cpp:129
UARTFlushResult flush()
Definition uart.h:96
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:43
void discard_available()
Read and drop every byte waiting in the receive buffer.
Definition uart.cpp:36
void write_byte(uint8_t data)
Definition uart.h:22
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
int ret
uint8_t calc_checksum(const uint8_t *state, size_t len)
ESPHOME_LOG_TAG(TAG, "senseair")
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:95
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:498
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1084
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:1687
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:1043
void HOT delay(uint32_t ms)
Definition hal.cpp:85