ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
cm1106.cpp
Go to the documentation of this file.
1#include "cm1106.h"
2#include "esphome/core/log.h"
3
4#include <cinttypes>
5
6namespace esphome {
7namespace cm1106 {
8
9static const char *const TAG = "cm1106";
10static const uint8_t C_M1106_CMD_GET_CO2[4] = {0x11, 0x01, 0x01, 0xED};
11static const uint8_t C_M1106_CMD_SET_CO2_CALIB[6] = {0x11, 0x03, 0x03, 0x00, 0x00, 0x00};
12static const uint8_t C_M1106_CMD_SET_CO2_CALIB_RESPONSE[4] = {0x16, 0x01, 0x03, 0xE6};
13
14uint8_t cm1106_checksum(const uint8_t *response, size_t len) {
15 uint8_t crc = 0;
16 for (int i = 0; i < len - 1; i++) {
17 crc -= response[i];
18 }
19 return crc;
20}
21
23 uint8_t response[8] = {0};
24 if (!this->cm1106_write_command_(C_M1106_CMD_GET_CO2, sizeof(C_M1106_CMD_GET_CO2), response, sizeof(response))) {
25 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
26 this->mark_failed();
27 return;
28 }
29}
30
32 uint8_t response[8] = {0};
33 if (!this->cm1106_write_command_(C_M1106_CMD_GET_CO2, sizeof(C_M1106_CMD_GET_CO2), response, sizeof(response))) {
34 ESP_LOGW(TAG, "Reading data from CM1106 failed!");
35 this->status_set_warning();
36 return;
37 }
38
39 if (response[0] != 0x16 || response[1] != 0x05 || response[2] != 0x01) {
40 ESP_LOGW(TAG, "Got wrong UART response from CM1106: %02X %02X %02X %02X", response[0], response[1], response[2],
41 response[3]);
42 this->status_set_warning();
43 return;
44 }
45
46 uint8_t checksum = cm1106_checksum(response, sizeof(response));
47 if (response[7] != checksum) {
48 ESP_LOGW(TAG, "CM1106 Checksum doesn't match: 0x%02X!=0x%02X", response[7], checksum);
49 this->status_set_warning();
50 return;
51 }
52
54
55 uint16_t ppm = response[3] << 8 | response[4];
56 ESP_LOGD(TAG, "CM1106 Received CO₂=%uppm DF3=%02X DF4=%02X", ppm, response[5], response[6]);
57 if (this->co2_sensor_ != nullptr)
58 this->co2_sensor_->publish_state(ppm);
59}
60
62 uint8_t cmd[6];
63 memcpy(cmd, C_M1106_CMD_SET_CO2_CALIB, sizeof(cmd));
64 cmd[3] = ppm >> 8;
65 cmd[4] = ppm & 0xFF;
66 uint8_t response[4] = {0};
67
68 if (!this->cm1106_write_command_(cmd, sizeof(cmd), response, sizeof(response))) {
69 ESP_LOGW(TAG, "Reading data from CM1106 failed!");
70 this->status_set_warning();
71 return;
72 }
73
74 // check if correct response received
75 if (memcmp(response, C_M1106_CMD_SET_CO2_CALIB_RESPONSE, sizeof(response)) != 0) {
76 ESP_LOGW(TAG, "Got wrong UART response from CM1106: %02X %02X %02X %02X", response[0], response[1], response[2],
77 response[3]);
78 this->status_set_warning();
79 return;
80 }
81
83 ESP_LOGD(TAG, "CM1106 Successfully calibrated sensor to %uppm", ppm);
84}
85
86bool CM1106Component::cm1106_write_command_(const uint8_t *command, size_t command_len, uint8_t *response,
87 size_t response_len) {
88 // Empty RX Buffer
89 while (this->available())
90 this->read();
91 this->write_array(command, command_len - 1);
92 this->write_byte(cm1106_checksum(command, command_len));
93 this->flush();
94
95 if (response == nullptr)
96 return true;
97
98 return this->read_array(response, response_len);
99}
100
102 ESP_LOGCONFIG(TAG, "CM1106:");
103 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
104 this->check_uart_settings(9600);
105 if (this->is_failed()) {
106 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
107 }
108}
109
110} // namespace cm1106
111} // namespace esphome
uint8_t checksum
Definition bl0906.h:3
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
void calibrate_zero(uint16_t ppm)
Definition cm1106.cpp:61
bool cm1106_write_command_(const uint8_t *command, size_t command_len, uint8_t *response, size_t response_len)
Definition cm1106.cpp:86
sensor::Sensor * co2_sensor_
Definition cm1106.h:24
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:33
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:13
void write_byte(uint8_t data)
Definition uart.h:19
void write_array(const uint8_t *data, size_t len)
Definition uart.h:21
uint8_t cm1106_checksum(const uint8_t *response, size_t len)
Definition cm1106.cpp:14
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:279