9static const char *
const TAG =
"mhz19";
10static const uint8_t MHZ19_REQUEST_LENGTH = 8;
11static const uint8_t MHZ19_RESPONSE_LENGTH = 9;
12static const uint8_t MHZ19_COMMAND_GET_PPM[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00};
13static const uint8_t MHZ19_COMMAND_ABC_ENABLE[] = {0xFF, 0x01, 0x79, 0xA0, 0x00, 0x00, 0x00, 0x00};
14static const uint8_t MHZ19_COMMAND_ABC_DISABLE[] = {0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00};
15static const uint8_t MHZ19_COMMAND_CALIBRATE_ZERO[] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00};
16static const uint8_t MHZ19_COMMAND_DETECTION_RANGE_0_2000PPM[] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x07, 0xD0};
17static const uint8_t MHZ19_COMMAND_DETECTION_RANGE_0_5000PPM[] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88};
18static const uint8_t MHZ19_COMMAND_DETECTION_RANGE_0_10000PPM[] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x27, 0x10};
22 for (uint8_t i = 1; i < MHZ19_REQUEST_LENGTH; i++) {
25 return 0xFF - sum + 0x01;
39 uint32_t now_ms =
millis();
41 if (now_ms < warmup_ms) {
42 ESP_LOGW(TAG,
"MHZ19 warming up, %" PRIu32
" s left", (warmup_ms - now_ms) / 1000);
47 uint8_t response[MHZ19_RESPONSE_LENGTH];
49 ESP_LOGW(TAG,
"Reading data from MHZ19 failed!");
54 if (response[0] != 0xFF || response[1] != 0x86) {
55 ESP_LOGW(TAG,
"Invalid preamble from MHZ19!");
62 ESP_LOGW(TAG,
"MHZ19 Checksum doesn't match: 0x%02X!=0x%02X", response[8],
checksum);
68 const uint16_t ppm = (uint16_t(response[2]) << 8) | response[3];
69 const int temp = int(response[4]) - 40;
70 const uint8_t
status = response[5];
72 ESP_LOGD(TAG,
"MHZ19 Received CO₂=%uppm Temperature=%d°C Status=0x%02X", ppm, temp,
status);
80 ESP_LOGD(TAG,
"MHZ19 Calibrating zero point");
85 ESP_LOGD(TAG,
"MHZ19 Enabling automatic baseline calibration");
90 ESP_LOGD(TAG,
"MHZ19 Disabling automatic baseline calibration");
95 switch (detection_ppm) {
97 ESP_LOGV(TAG,
"Using previously set detection range (no change)");
100 ESP_LOGD(TAG,
"Setting detection range to 0 to 2000ppm");
104 ESP_LOGD(TAG,
"Setting detection range to 0 to 5000ppm");
108 ESP_LOGD(TAG,
"Setting detection range to 0 to 10000ppm");
122 if (response ==
nullptr)
125 return this->
read_array(response, MHZ19_RESPONSE_LENGTH);
131 ESP_LOGCONFIG(TAG,
"MH-Z19:");
137 ESP_LOGCONFIG(TAG,
" Automatic baseline calibration enabled on boot");
139 ESP_LOGCONFIG(TAG,
" Automatic baseline calibration disabled on boot");
142 ESP_LOGCONFIG(TAG,
" Warmup time: %" PRIu32
" s", this->
warmup_seconds_);
144 const char *range_str;
147 range_str =
"default";
150 range_str =
"0 to 2000ppm";
153 range_str =
"0 to 5000ppm";
156 range_str =
"0 to 10000ppm";
159 range_str =
"default";
162 ESP_LOGCONFIG(TAG,
" Detection range: %s", range_str);
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...