ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
mhz19.cpp
Go to the documentation of this file.
1#include "mhz19.h"
3#include "esphome/core/log.h"
5
6#include <cinttypes>
7
8namespace esphome::mhz19 {
9
10ESPHOME_LOG_TAG(TAG, "mhz19");
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};
20
21#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
22static const LogString *detection_range_to_log_string(MHZ19DetectionRange range) {
23 switch (range) {
25 return LOG_STR("0-2000 ppm");
27 return LOG_STR("0-5000 ppm");
29 return LOG_STR("0-10000 ppm");
30 default:
31 return LOG_STR("default");
32 }
33}
34#endif
35
36uint8_t mhz19_checksum(const uint8_t *command) { return -sum_bytes(command + 1, MHZ19_REQUEST_LENGTH - 1); }
37
40 this->abc_enable();
41 } else if (this->abc_boot_logic_ == MHZ19_ABC_DISABLED) {
42 this->abc_disable();
43 }
44
45 this->range_set(this->detection_range_);
46}
47
48void MHZ19Component::update() {
49 if (!this->warmup_complete_) {
51 uint32_t warmup_ms = this->warmup_seconds_ * 1000;
52 if (now_ms < warmup_ms) {
53 ESP_LOGW(TAG, "MHZ19 warming up, %" PRIu32 " s left", (warmup_ms - now_ms) / 1000);
54 this->status_set_warning();
55 return;
56 }
57 this->warmup_complete_ = true;
58 }
59
60 uint8_t response[MHZ19_RESPONSE_LENGTH];
61 if (!this->mhz19_write_command_(MHZ19_COMMAND_GET_PPM, response)) {
62 ESP_LOGW(TAG, "Reading data from MHZ19 failed!");
63 this->status_set_warning();
64 return;
65 }
66
67 if (response[0] != 0xFF || response[1] != 0x86) {
68 ESP_LOGW(TAG, "Invalid preamble from MHZ19!");
69 this->status_set_warning();
70 return;
71 }
72
73 uint8_t checksum = mhz19_checksum(response);
74 if (!this->status_comm_result(response[8] == checksum)) {
75 ESP_LOGW(TAG, "MHZ19 Checksum doesn't match: 0x%02X!=0x%02X", response[8], checksum);
76 return;
77 }
78
79 const uint16_t ppm = encode_uint16(response[2], response[3]);
80 const int temp = int(response[4]) - 40;
81 const uint8_t status = response[5];
82
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);
86}
87
88void MHZ19Component::calibrate_zero() {
89 ESP_LOGD(TAG, "MHZ19 Calibrating zero point");
90 this->mhz19_write_command_(MHZ19_COMMAND_CALIBRATE_ZERO, nullptr);
91}
92
93void MHZ19Component::abc_enable() {
94 ESP_LOGD(TAG, "MHZ19 Enabling automatic baseline calibration");
95 this->mhz19_write_command_(MHZ19_COMMAND_ABC_ENABLE, nullptr);
96}
97
98void MHZ19Component::abc_disable() {
99 ESP_LOGD(TAG, "MHZ19 Disabling automatic baseline calibration");
100 this->mhz19_write_command_(MHZ19_COMMAND_ABC_DISABLE, nullptr);
101}
102
103void MHZ19Component::range_set(MHZ19DetectionRange detection_range) {
104 const uint8_t *command;
105 switch (detection_range) {
107 command = MHZ19_COMMAND_DETECTION_RANGE_0_2000PPM;
108 break;
110 command = MHZ19_COMMAND_DETECTION_RANGE_0_5000PPM;
111 break;
113 command = MHZ19_COMMAND_DETECTION_RANGE_0_10000PPM;
114 break;
115 default:
116 ESP_LOGV(TAG, "Using previously set detection range (no change)");
117 return;
118 }
119 ESP_LOGD(TAG, "Setting detection range to %s", LOG_STR_ARG(detection_range_to_log_string(detection_range)));
120 this->mhz19_write_command_(command, nullptr);
121}
122
123bool MHZ19Component::mhz19_write_command_(const uint8_t *command, uint8_t *response) {
124 this->discard_available();
125 this->write_array(command, MHZ19_REQUEST_LENGTH);
126 this->write_byte(mhz19_checksum(command));
127 this->flush();
128
129 if (response == nullptr)
130 return true;
131
132 return this->read_array(response, MHZ19_RESPONSE_LENGTH);
133}
134
135void MHZ19Component::dump_config() {
136 ESP_LOGCONFIG(TAG, "MH-Z19:");
137 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
138 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
139 if (this->abc_boot_logic_ == MHZ19_ABC_ENABLED) {
140 ESP_LOGCONFIG(TAG, " Automatic baseline calibration enabled on boot");
141 } else if (this->abc_boot_logic_ == MHZ19_ABC_DISABLED) {
142 ESP_LOGCONFIG(TAG, " Automatic baseline calibration disabled on boot");
143 }
144
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_)));
147}
148
149} // namespace esphome::mhz19
uint8_t checksum
Definition bl0906.h:3
uint8_t status
Definition bl0942.h:8
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
MHZ19DetectionRange detection_range_
Definition mhz19.h:45
bool mhz19_write_command_(const uint8_t *command, uint8_t *response)
Definition mhz19.cpp:123
MHZ19ABCLogic abc_boot_logic_
Definition mhz19.h:44
UARTFlushResult flush()
Definition uart.h:96
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:43
void discard_available()
Read and drop every byte waiting in the receive buffer.
Definition uart.cpp:36
void write_byte(uint8_t data)
Definition uart.h:22
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
Range range
Definition msa3xx.h:0
ESPHOME_LOG_TAG(TAG, "mhz19")
MHZ19DetectionRange
Definition mhz19.h:15
@ MHZ19_DETECTION_RANGE_0_10000PPM
Definition mhz19.h:19
@ MHZ19_DETECTION_RANGE_0_5000PPM
Definition mhz19.h:18
@ MHZ19_DETECTION_RANGE_0_2000PPM
Definition mhz19.h:17
uint8_t mhz19_checksum(const uint8_t *command)
Definition mhz19.cpp:36
@ MHZ19_ABC_DISABLED
Definition mhz19.h:12
@ MHZ19_ABC_ENABLED
Definition mhz19.h:11
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...
Definition helpers.h:939
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t