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};
16 for (
int i = 0; i <
len - 1; i++) {
23 ESP_LOGCONFIG(TAG,
"Running setup");
24 uint8_t response[8] = {0};
25 if (!this->
cm1106_write_command_(C_M1106_CMD_GET_CO2,
sizeof(C_M1106_CMD_GET_CO2), response,
sizeof(response))) {
26 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
33 uint8_t response[8] = {0};
34 if (!this->
cm1106_write_command_(C_M1106_CMD_GET_CO2,
sizeof(C_M1106_CMD_GET_CO2), response,
sizeof(response))) {
35 ESP_LOGW(TAG,
"Reading data from CM1106 failed!");
40 if (response[0] != 0x16 || response[1] != 0x05 || response[2] != 0x01) {
41 ESP_LOGW(TAG,
"Got wrong UART response from CM1106: %02X %02X %02X %02X", response[0], response[1], response[2],
49 ESP_LOGW(TAG,
"CM1106 Checksum doesn't match: 0x%02X!=0x%02X", response[7],
checksum);
56 uint16_t ppm = response[3] << 8 | response[4];
57 ESP_LOGD(TAG,
"CM1106 Received CO₂=%uppm DF3=%02X DF4=%02X", ppm, response[5], response[6]);
64 memcpy(cmd, C_M1106_CMD_SET_CO2_CALIB,
sizeof(cmd));
67 uint8_t response[4] = {0};
70 ESP_LOGW(TAG,
"Reading data from CM1106 failed!");
76 if (memcmp(response, C_M1106_CMD_SET_CO2_CALIB_RESPONSE,
sizeof(response)) != 0) {
77 ESP_LOGW(TAG,
"Got wrong UART response from CM1106: %02X %02X %02X %02X", response[0], response[1], response[2],
84 ESP_LOGD(TAG,
"CM1106 Successfully calibrated sensor to %uppm", ppm);
88 size_t response_len) {
96 if (response ==
nullptr)
99 return this->
read_array(response, response_len);
103 ESP_LOGCONFIG(TAG,
"CM1106:");
107 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
void calibrate_zero(uint16_t ppm)
void dump_config() override
bool cm1106_write_command_(const uint8_t *command, size_t command_len, uint8_t *response, size_t response_len)
sensor::Sensor * co2_sensor_
void publish_state(float state)
Publish a new state to the front-end.
optional< std::array< uint8_t, N > > read_array()
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...
void write_byte(uint8_t data)
void write_array(const uint8_t *data, size_t len)
uint8_t cm1106_checksum(const uint8_t *response, size_t len)
Providing packet encoding functions for exchanging data with a remote host.