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};
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};
18static constexpr uint8_t CM1106_ABC_FLAG_ENABLE = 0x0;
19static constexpr uint8_t CM1106_ABC_FLAG_DISABLE = 0x2;
21 return static_cast<uint8_t
>(-
sum_bytes(response,
len - 1));
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))) {
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!");
44 if (response[0] != 0x16 || response[1] != 0x05 || response[2] != 0x01) {
45#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
55 ESP_LOGW(TAG,
"Checksum doesn't match: 0x%02X!=0x%02X", response[7],
checksum);
61 const uint8_t
status = response[5];
63 ESP_LOGW(TAG,
"Preheating; CO₂=%uppm", ppm);
68 ESP_LOGW(TAG,
"Status: %02X", status);
72 ESP_LOGD(TAG,
"CO₂=%uppm DF4=%02X", ppm, response[6]);
73 PUBLISH_STATE(this->co2_sensor_, ppm);
76void CM1106Component::calibrate_zero(uint16_t ppm) {
78 memcpy(cmd, C_M1106_CMD_SET_CO2_CALIB,
sizeof(cmd));
82 ESP_LOGD(TAG,
"Successfully calibrated sensor to %uppm", ppm);
87 ESP_LOGD(TAG,
"%sabling automatic baseline calibration", enabled ? LOG_STR_LITERAL(
"En") : LOG_STR_LITERAL(
"Dis"));
89 memcpy(cmd, C_M1106_CMD_SET_ABC_STATUS,
sizeof(cmd));
90 cmd[4] = enabled ? CM1106_ABC_FLAG_ENABLE : CM1106_ABC_FLAG_DISABLE;
95 ESP_LOGD(TAG,
"Successfully set ABC status");
100 uint8_t response[4] = {0};
102 ESP_LOGW(TAG,
"Command %02X failed", command[2]);
107#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
117 size_t response_len) {
123 if (response ==
nullptr)
126 return this->
read_array(response, response_len);
129void CM1106Component::dump_config() {
130 ESP_LOGCONFIG(TAG,
"CM1106:");
131 LOG_SENSOR(
" ",
"CO2", this->co2_sensor_);
134 " Automatic baseline calibration on boot: %s\n"
135 " ABC calibration cycle: %u days\n"
136 " ABC baseline: %uppm",
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.
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...
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).