ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
cm1106.cpp
Go to the documentation of this file.
1#include "cm1106.h"
3#include "esphome/core/log.h"
4
5#include <cinttypes>
6
7namespace esphome::cm1106 {
8
9ESPHOME_LOG_TAG(TAG, "cm1106");
10static constexpr uint8_t C_M1106_CMD_GET_CO2[4] = {0x11, 0x01, 0x01, 0xED};
11static constexpr uint8_t C_M1106_CMD_SET_CO2_CALIB[6] = {0x11, 0x03, 0x03, 0x00, 0x00, 0x00};
12static constexpr uint8_t C_M1106_CMD_SET_CO2_CALIB_RESPONSE[4] = {0x16, 0x01, 0x03, 0xE6};
13// The factory default ABC calibration cycle differs between CM1106 models, so
14// every ABC write applies the configured cycle and baseline explicitly.
15static constexpr uint8_t C_M1106_CMD_SET_ABC_STATUS[10] = {0x11, 0x07, 0x10, 0x64, 0x00, 0x0F, 0x01, 0x90, 0x64, 0x00};
16static constexpr uint8_t C_M1106_CMD_SET_ABC_STATUS_RESPONSE[4] = {0x16, 0x01, 0x10, 0xD9};
17
18static constexpr uint8_t CM1106_ABC_FLAG_ENABLE = 0x0;
19static constexpr uint8_t CM1106_ABC_FLAG_DISABLE = 0x2;
20uint8_t cm1106_checksum(const uint8_t *response, size_t len) {
21 return static_cast<uint8_t>(-sum_bytes(response, len - 1));
22}
23
25 uint8_t response[8] = {0};
26 if (!this->cm1106_write_command_(C_M1106_CMD_GET_CO2, sizeof(C_M1106_CMD_GET_CO2), response, sizeof(response))) {
27 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
28 return;
29 }
30
31 if (this->abc_boot_logic_ != CM1106_ABC_NONE) {
33 }
34}
35
36void CM1106Component::update() {
37 uint8_t response[8] = {0};
38 if (!this->cm1106_write_command_(C_M1106_CMD_GET_CO2, sizeof(C_M1106_CMD_GET_CO2), response, sizeof(response))) {
39 ESP_LOGW(TAG, "Reading data failed!");
40 this->status_set_warning();
41 return;
42 }
43
44 if (response[0] != 0x16 || response[1] != 0x05 || response[2] != 0x01) {
45#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
46 char hex_buf[format_hex_pretty_size(4)];
47 ESP_LOGW(TAG, "Unexpected response: %s", format_hex_pretty_to(hex_buf, response, 4, ' '));
48#endif
49 this->status_set_warning();
50 return;
51 }
52
53 uint8_t checksum = cm1106_checksum(response, sizeof(response));
54 if (response[7] != checksum) {
55 ESP_LOGW(TAG, "Checksum doesn't match: 0x%02X!=0x%02X", response[7], checksum);
56 this->status_set_warning();
57 return;
58 }
59
60 const uint16_t ppm = encode_uint16(response[3], response[4]);
61 const uint8_t status = response[5];
62 if (status & 0x1) {
63 ESP_LOGW(TAG, "Preheating; CO₂=%uppm", ppm);
64 this->status_set_warning();
65 return;
66 }
67 if (status) {
68 ESP_LOGW(TAG, "Status: %02X", status);
69 }
71
72 ESP_LOGD(TAG, "CO₂=%uppm DF4=%02X", ppm, response[6]);
73 PUBLISH_STATE(this->co2_sensor_, ppm);
74}
75
76void CM1106Component::calibrate_zero(uint16_t ppm) {
77 uint8_t cmd[6];
78 memcpy(cmd, C_M1106_CMD_SET_CO2_CALIB, sizeof(cmd));
79 cmd[3] = ppm >> 8;
80 cmd[4] = ppm & 0xFF;
81 if (this->send_command_expect_ack_(cmd, sizeof(cmd), C_M1106_CMD_SET_CO2_CALIB_RESPONSE)) {
82 ESP_LOGD(TAG, "Successfully calibrated sensor to %uppm", ppm);
83 }
84}
85
86void CM1106Component::abc_set_(bool enabled) {
87 ESP_LOGD(TAG, "%sabling automatic baseline calibration", enabled ? LOG_STR_LITERAL("En") : LOG_STR_LITERAL("Dis"));
88 uint8_t cmd[10];
89 memcpy(cmd, C_M1106_CMD_SET_ABC_STATUS, sizeof(cmd));
90 cmd[4] = enabled ? CM1106_ABC_FLAG_ENABLE : CM1106_ABC_FLAG_DISABLE;
91 cmd[5] = this->abc_cycle_;
92 cmd[6] = this->abc_baseline_ >> 8;
93 cmd[7] = this->abc_baseline_ & 0xFF;
94 if (this->send_command_expect_ack_(cmd, sizeof(cmd), C_M1106_CMD_SET_ABC_STATUS_RESPONSE)) {
95 ESP_LOGD(TAG, "Successfully set ABC status");
96 }
97}
98
99bool CM1106Component::send_command_expect_ack_(const uint8_t *command, size_t command_len, const uint8_t *ack) {
100 uint8_t response[4] = {0};
101 if (!this->cm1106_write_command_(command, command_len, response, sizeof(response))) {
102 ESP_LOGW(TAG, "Command %02X failed", command[2]);
103 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
104 return false;
105 }
106 if (!this->status_comm_result(memcmp(response, ack, sizeof(response)) == 0)) {
107#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
108 char hex_buf[format_hex_pretty_size(4)];
109 ESP_LOGW(TAG, "Unexpected response: %s", format_hex_pretty_to(hex_buf, response, 4, ' '));
110#endif
111 return false;
112 }
113 return true;
114}
115
116bool CM1106Component::cm1106_write_command_(const uint8_t *command, size_t command_len, uint8_t *response,
117 size_t response_len) {
118 this->discard_available();
119 this->write_array(command, command_len - 1);
120 this->write_byte(cm1106_checksum(command, command_len));
121 this->flush();
122
123 if (response == nullptr)
124 return true;
125
126 return this->read_array(response, response_len);
127}
128
129void CM1106Component::dump_config() {
130 ESP_LOGCONFIG(TAG, "CM1106:");
131 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
132 if (this->abc_boot_logic_ != CM1106_ABC_NONE) {
133 ESP_LOGCONFIG(TAG,
134 " Automatic baseline calibration on boot: %s\n"
135 " ABC calibration cycle: %u days\n"
136 " ABC baseline: %uppm",
137 ONOFF(this->abc_boot_logic_ == CM1106_ABC_ENABLED), this->abc_cycle_, this->abc_baseline_);
138 }
139}
140
141} // namespace esphome::cm1106
uint8_t checksum
Definition bl0906.h:3
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void status_clear_warning()
Definition component.h:309
bool send_command_expect_ack_(const uint8_t *command, size_t command_len, const uint8_t *ack)
Sends a command and checks the sensor's four byte acknowledgement, updating the warning status.
Definition cm1106.cpp:99
void abc_set_(bool enabled)
Definition cm1106.cpp:86
CM1106ABCLogic abc_boot_logic_
Definition cm1106.h:41
bool cm1106_write_command_(const uint8_t *command, size_t command_len, uint8_t *response, size_t response_len)
Definition cm1106.cpp:116
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
uint8_t cm1106_checksum(const uint8_t *response, size_t len)
Definition cm1106.cpp:20
@ CM1106_ABC_ENABLED
Definition cm1106.h:13
ESPHOME_LOG_TAG(TAG, "cm1106")
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
Definition helpers.h:939
const void size_t len
Definition hal.h:68
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 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
uint8_t ack