11static const uint8_t MHZ19_REQUEST_LENGTH = 8;
12static const uint8_t MHZ19_RESPONSE_LENGTH = 9;
13static const uint8_t MHZ19_COMMAND_GET_PPM[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00};
14static const uint8_t MHZ19_COMMAND_ABC_ENABLE[] = {0xFF, 0x01, 0x79, 0xA0, 0x00, 0x00, 0x00, 0x00};
15static const uint8_t MHZ19_COMMAND_ABC_DISABLE[] = {0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00};
16static const uint8_t MHZ19_COMMAND_CALIBRATE_ZERO[] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00};
17static const uint8_t MHZ19_COMMAND_DETECTION_RANGE_0_2000PPM[] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x07, 0xD0};
18static const uint8_t MHZ19_COMMAND_DETECTION_RANGE_0_5000PPM[] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88};
19static const uint8_t MHZ19_COMMAND_DETECTION_RANGE_0_10000PPM[] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x27, 0x10};
21#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
25 return LOG_STR(
"0-2000 ppm");
27 return LOG_STR(
"0-5000 ppm");
29 return LOG_STR(
"0-10000 ppm");
31 return LOG_STR(
"default");
48void MHZ19Component::update() {
52 if (now_ms < warmup_ms) {
53 ESP_LOGW(TAG,
"MHZ19 warming up, %" PRIu32
" s left", (warmup_ms - now_ms) / 1000);
60 uint8_t response[MHZ19_RESPONSE_LENGTH];
62 ESP_LOGW(TAG,
"Reading data from MHZ19 failed!");
67 if (response[0] != 0xFF || response[1] != 0x86) {
68 ESP_LOGW(TAG,
"Invalid preamble from MHZ19!");
75 ESP_LOGW(TAG,
"MHZ19 Checksum doesn't match: 0x%02X!=0x%02X", response[8],
checksum);
80 const int temp = int(response[4]) - 40;
81 const uint8_t
status = response[5];
83 ESP_LOGD(TAG,
"MHZ19 Received CO₂=%uppm Temperature=%d°C Status=0x%02X", ppm, temp, status);
84 PUBLISH_STATE(this->co2_sensor_, ppm);
85 PUBLISH_STATE(this->temperature_sensor_, temp);
88void MHZ19Component::calibrate_zero() {
89 ESP_LOGD(TAG,
"MHZ19 Calibrating zero point");
93void MHZ19Component::abc_enable() {
94 ESP_LOGD(TAG,
"MHZ19 Enabling automatic baseline calibration");
98void MHZ19Component::abc_disable() {
99 ESP_LOGD(TAG,
"MHZ19 Disabling automatic baseline calibration");
104 const uint8_t *command;
105 switch (detection_range) {
107 command = MHZ19_COMMAND_DETECTION_RANGE_0_2000PPM;
110 command = MHZ19_COMMAND_DETECTION_RANGE_0_5000PPM;
113 command = MHZ19_COMMAND_DETECTION_RANGE_0_10000PPM;
116 ESP_LOGV(TAG,
"Using previously set detection range (no change)");
119 ESP_LOGD(TAG,
"Setting detection range to %s", LOG_STR_ARG(detection_range_to_log_string(detection_range)));
129 if (response ==
nullptr)
132 return this->
read_array(response, MHZ19_RESPONSE_LENGTH);
135void MHZ19Component::dump_config() {
136 ESP_LOGCONFIG(TAG,
"MH-Z19:");
137 LOG_SENSOR(
" ",
"CO2", this->co2_sensor_);
138 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
140 ESP_LOGCONFIG(TAG,
" Automatic baseline calibration enabled on boot");
142 ESP_LOGCONFIG(TAG,
" Automatic baseline calibration disabled on boot");
145 ESP_LOGCONFIG(TAG,
" Warmup time: %" PRIu32
" s", this->
warmup_seconds_);
146 ESP_LOGCONFIG(TAG,
" Detection range: %s", LOG_STR_ARG(detection_range_to_log_string(this->
detection_range_)));