ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
mhz19.cpp
Go to the documentation of this file.
1#include "mhz19.h"
2#include "esphome/core/log.h"
3
4#include <cinttypes>
5
6namespace esphome::mhz19 {
7
8static const char *const TAG = "mhz19";
9static const uint8_t MHZ19_REQUEST_LENGTH = 8;
10static const uint8_t MHZ19_RESPONSE_LENGTH = 9;
11static const uint8_t MHZ19_COMMAND_GET_PPM[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00};
12static const uint8_t MHZ19_COMMAND_ABC_ENABLE[] = {0xFF, 0x01, 0x79, 0xA0, 0x00, 0x00, 0x00, 0x00};
13static const uint8_t MHZ19_COMMAND_ABC_DISABLE[] = {0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00};
14static const uint8_t MHZ19_COMMAND_CALIBRATE_ZERO[] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00};
15static const uint8_t MHZ19_COMMAND_DETECTION_RANGE_0_2000PPM[] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x07, 0xD0};
16static const uint8_t MHZ19_COMMAND_DETECTION_RANGE_0_5000PPM[] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88};
17static const uint8_t MHZ19_COMMAND_DETECTION_RANGE_0_10000PPM[] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x27, 0x10};
18
19#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
20static const LogString *detection_range_to_log_string(MHZ19DetectionRange range) {
21 switch (range) {
23 return LOG_STR("0-2000 ppm");
25 return LOG_STR("0-5000 ppm");
27 return LOG_STR("0-10000 ppm");
28 default:
29 return LOG_STR("default");
30 }
31}
32#endif
33
34uint8_t mhz19_checksum(const uint8_t *command) {
35 uint8_t sum = 0;
36 for (uint8_t i = 1; i < MHZ19_REQUEST_LENGTH; i++) {
37 sum += command[i];
38 }
39 return 0xFF - sum + 0x01;
40}
41
44 this->abc_enable();
45 } else if (this->abc_boot_logic_ == MHZ19_ABC_DISABLED) {
46 this->abc_disable();
47 }
48
49 this->range_set(this->detection_range_);
50}
51
53 uint32_t now_ms = millis();
54 uint32_t warmup_ms = this->warmup_seconds_ * 1000;
55 if (now_ms < warmup_ms) {
56 ESP_LOGW(TAG, "MHZ19 warming up, %" PRIu32 " s left", (warmup_ms - now_ms) / 1000);
57 this->status_set_warning();
58 return;
59 }
60
61 uint8_t response[MHZ19_RESPONSE_LENGTH];
62 if (!this->mhz19_write_command_(MHZ19_COMMAND_GET_PPM, response)) {
63 ESP_LOGW(TAG, "Reading data from MHZ19 failed!");
64 this->status_set_warning();
65 return;
66 }
67
68 if (response[0] != 0xFF || response[1] != 0x86) {
69 ESP_LOGW(TAG, "Invalid preamble from MHZ19!");
70 this->status_set_warning();
71 return;
72 }
73
74 uint8_t checksum = mhz19_checksum(response);
75 if (response[8] != checksum) {
76 ESP_LOGW(TAG, "MHZ19 Checksum doesn't match: 0x%02X!=0x%02X", response[8], checksum);
77 this->status_set_warning();
78 return;
79 }
80
82 const uint16_t ppm = (uint16_t(response[2]) << 8) | response[3];
83 const int temp = int(response[4]) - 40;
84 const uint8_t status = response[5];
85
86 ESP_LOGD(TAG, "MHZ19 Received CO₂=%uppm Temperature=%d°C Status=0x%02X", ppm, temp, status);
87 if (this->co2_sensor_ != nullptr)
88 this->co2_sensor_->publish_state(ppm);
89 if (this->temperature_sensor_ != nullptr)
91}
92
94 ESP_LOGD(TAG, "MHZ19 Calibrating zero point");
95 this->mhz19_write_command_(MHZ19_COMMAND_CALIBRATE_ZERO, nullptr);
96}
97
99 ESP_LOGD(TAG, "MHZ19 Enabling automatic baseline calibration");
100 this->mhz19_write_command_(MHZ19_COMMAND_ABC_ENABLE, nullptr);
101}
102
104 ESP_LOGD(TAG, "MHZ19 Disabling automatic baseline calibration");
105 this->mhz19_write_command_(MHZ19_COMMAND_ABC_DISABLE, nullptr);
106}
107
109 const uint8_t *command;
110 switch (detection_range) {
112 command = MHZ19_COMMAND_DETECTION_RANGE_0_2000PPM;
113 break;
115 command = MHZ19_COMMAND_DETECTION_RANGE_0_5000PPM;
116 break;
118 command = MHZ19_COMMAND_DETECTION_RANGE_0_10000PPM;
119 break;
120 default:
121 ESP_LOGV(TAG, "Using previously set detection range (no change)");
122 return;
123 }
124 ESP_LOGD(TAG, "Setting detection range to %s", LOG_STR_ARG(detection_range_to_log_string(detection_range)));
125 this->mhz19_write_command_(command, nullptr);
126}
127
128bool MHZ19Component::mhz19_write_command_(const uint8_t *command, uint8_t *response) {
129 // Empty RX Buffer
130 while (this->available())
131 this->read();
132 this->write_array(command, MHZ19_REQUEST_LENGTH);
133 this->write_byte(mhz19_checksum(command));
134 this->flush();
135
136 if (response == nullptr)
137 return true;
138
139 return this->read_array(response, MHZ19_RESPONSE_LENGTH);
140}
141
143 ESP_LOGCONFIG(TAG, "MH-Z19:");
144 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
145 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
146 this->check_uart_settings(9600);
147
148 if (this->abc_boot_logic_ == MHZ19_ABC_ENABLED) {
149 ESP_LOGCONFIG(TAG, " Automatic baseline calibration enabled on boot");
150 } else if (this->abc_boot_logic_ == MHZ19_ABC_DISABLED) {
151 ESP_LOGCONFIG(TAG, " Automatic baseline calibration disabled on boot");
152 }
153
154 ESP_LOGCONFIG(TAG, " Warmup time: %" PRIu32 " s", this->warmup_seconds_);
155 ESP_LOGCONFIG(TAG, " Detection range: %s", LOG_STR_ARG(detection_range_to_log_string(this->detection_range_)));
156}
157
158} // namespace esphome::mhz19
uint8_t checksum
Definition bl0906.h:3
uint8_t status
Definition bl0942.h:8
void status_clear_warning()
Definition component.h:306
void range_set(MHZ19DetectionRange detection_range)
Definition mhz19.cpp:108
void dump_config() override
Definition mhz19.cpp:142
MHZ19DetectionRange detection_range_
Definition mhz19.h:49
sensor::Sensor * temperature_sensor_
Definition mhz19.h:43
sensor::Sensor * co2_sensor_
Definition mhz19.h:44
bool mhz19_write_command_(const uint8_t *command, uint8_t *response)
Definition mhz19.cpp:128
MHZ19ABCLogic abc_boot_logic_
Definition mhz19.h:45
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
UARTFlushResult flush()
Definition uart.h:48
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
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:16
void write_byte(uint8_t data)
Definition uart.h:18
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
Range range
Definition msa3xx.h:0
@ MHZ19_ABC_DISABLED
Definition mhz19.h:13
@ MHZ19_ABC_ENABLED
Definition mhz19.h:12
uint8_t mhz19_checksum(const uint8_t *command)
Definition mhz19.cpp:34
MHZ19DetectionRange
Definition mhz19.h:16
@ MHZ19_DETECTION_RANGE_0_10000PPM
Definition mhz19.h:20
@ MHZ19_DETECTION_RANGE_0_5000PPM
Definition mhz19.h:19
@ MHZ19_DETECTION_RANGE_0_2000PPM
Definition mhz19.h:18
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
static void uint32_t