7 static const char *
const TAG =
"mhz19";
8 static const uint8_t MHZ19_REQUEST_LENGTH = 8;
9 static const uint8_t MHZ19_RESPONSE_LENGTH = 9;
10 static const uint8_t MHZ19_COMMAND_GET_PPM[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00};
11 static const uint8_t MHZ19_COMMAND_ABC_ENABLE[] = {0xFF, 0x01, 0x79, 0xA0, 0x00, 0x00, 0x00, 0x00};
12 static const uint8_t MHZ19_COMMAND_ABC_DISABLE[] = {0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00};
13 static const uint8_t MHZ19_COMMAND_CALIBRATE_ZERO[] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00};
17 for (uint8_t i = 1; i < MHZ19_REQUEST_LENGTH; i++) {
20 return 0xFF - sum + 0x01;
32 uint8_t response[MHZ19_RESPONSE_LENGTH];
34 ESP_LOGW(TAG,
"Reading data from MHZ19 failed!");
39 if (response[0] != 0xFF || response[1] != 0x86) {
40 ESP_LOGW(TAG,
"Invalid preamble from MHZ19!");
46 if (response[8] != checksum) {
47 ESP_LOGW(TAG,
"MHZ19 Checksum doesn't match: 0x%02X!=0x%02X", response[8], checksum);
53 const uint16_t ppm = (uint16_t(response[2]) << 8) | response[3];
54 const int temp = int(response[4]) - 40;
55 const uint8_t
status = response[5];
57 ESP_LOGD(TAG,
"MHZ19 Received CO₂=%uppm Temperature=%d°C Status=0x%02X", ppm, temp, status);
65 ESP_LOGD(TAG,
"MHZ19 Calibrating zero point");
70 ESP_LOGD(TAG,
"MHZ19 Enabling automatic baseline calibration");
75 ESP_LOGD(TAG,
"MHZ19 Disabling automatic baseline calibration");
87 if (response ==
nullptr)
90 return this->
read_array(response, MHZ19_RESPONSE_LENGTH);
94 ESP_LOGCONFIG(TAG,
"MH-Z19:");
100 ESP_LOGCONFIG(TAG,
" Automatic baseline calibration enabled on boot");
102 ESP_LOGCONFIG(TAG,
" Automatic baseline calibration disabled on boot");
const float DATA
For components that import data from directly connected sensors like DHT.
optional< std::array< uint8_t, N > > read_array()
void write_array(const uint8_t *data, size_t len)
void write_byte(uint8_t data)
sensor::Sensor * temperature_sensor_
sensor::Sensor * co2_sensor_
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 status_clear_warning()
void dump_config() override
void publish_state(float state)
Publish a new state to the front-end.
bool mhz19_write_command_(const uint8_t *command, uint8_t *response)
void status_set_warning()
MHZ19ABCLogic abc_boot_logic_
float get_setup_priority() const override
uint8_t mhz19_checksum(const uint8_t *command)