9 static const char *
const TAG =
"mhz19";
10 static const uint8_t MHZ19_REQUEST_LENGTH = 8;
11 static const uint8_t MHZ19_RESPONSE_LENGTH = 9;
12 static const uint8_t MHZ19_COMMAND_GET_PPM[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00};
13 static const uint8_t MHZ19_COMMAND_ABC_ENABLE[] = {0xFF, 0x01, 0x79, 0xA0, 0x00, 0x00, 0x00, 0x00};
14 static const uint8_t MHZ19_COMMAND_ABC_DISABLE[] = {0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00};
15 static const uint8_t MHZ19_COMMAND_CALIBRATE_ZERO[] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00};
19 for (uint8_t i = 1; i < MHZ19_REQUEST_LENGTH; i++) {
22 return 0xFF - sum + 0x01;
34 uint32_t now_ms =
millis();
36 if (now_ms < warmup_ms) {
37 ESP_LOGW(TAG,
"MHZ19 warming up, %" PRIu32
" s left", (warmup_ms - now_ms) / 1000);
42 uint8_t response[MHZ19_RESPONSE_LENGTH];
44 ESP_LOGW(TAG,
"Reading data from MHZ19 failed!");
49 if (response[0] != 0xFF || response[1] != 0x86) {
50 ESP_LOGW(TAG,
"Invalid preamble from MHZ19!");
56 if (response[8] != checksum) {
57 ESP_LOGW(TAG,
"MHZ19 Checksum doesn't match: 0x%02X!=0x%02X", response[8], checksum);
63 const uint16_t ppm = (uint16_t(response[2]) << 8) | response[3];
64 const int temp = int(response[4]) - 40;
65 const uint8_t
status = response[5];
67 ESP_LOGD(TAG,
"MHZ19 Received CO₂=%uppm Temperature=%d°C Status=0x%02X", ppm, temp, status);
75 ESP_LOGD(TAG,
"MHZ19 Calibrating zero point");
80 ESP_LOGD(TAG,
"MHZ19 Enabling automatic baseline calibration");
85 ESP_LOGD(TAG,
"MHZ19 Disabling automatic baseline calibration");
97 if (response ==
nullptr)
100 return this->
read_array(response, MHZ19_RESPONSE_LENGTH);
104 ESP_LOGCONFIG(TAG,
"MH-Z19:");
110 ESP_LOGCONFIG(TAG,
" Automatic baseline calibration enabled on boot");
112 ESP_LOGCONFIG(TAG,
" Automatic baseline calibration disabled on boot");
115 ESP_LOGCONFIG(TAG,
" Warmup time: %" PRIu32
" s", this->
warmup_seconds_);
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)
void status_set_warning(const char *message="unspecified")
sensor::Sensor * temperature_sensor_
sensor::Sensor * co2_sensor_
uint32_t IRAM_ATTR HOT millis()
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)
Implementation of SPI Controller mode.
MHZ19ABCLogic abc_boot_logic_
float get_setup_priority() const override
uint8_t mhz19_checksum(const uint8_t *command)