MH-Z19 calibration support (#683)

* Allow configuration to enable or disable automatic baseline calibration on boot
* Add actions to enable or disable automatic baseline calibration
* Add action to calibrate zero point
This commit is contained in:
Nikolay Vasilchuk
2019-08-27 20:33:25 +03:00
committed by Brandon Davidson
parent 15a7d2ef75
commit c5db457700
4 changed files with 97 additions and 0 deletions
+32
View File
@@ -8,6 +8,9 @@ static const char *TAG = "mhz19";
static const uint8_t MHZ19_REQUEST_LENGTH = 8;
static const uint8_t MHZ19_RESPONSE_LENGTH = 9;
static const uint8_t MHZ19_COMMAND_GET_PPM[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00};
static const uint8_t MHZ19_COMMAND_ABC_ENABLE[] = {0xFF, 0x01, 0x79, 0xA0, 0x00, 0x00, 0x00, 0x00};
static const uint8_t MHZ19_COMMAND_ABC_DISABLE[] = {0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00};
static const uint8_t MHZ19_COMMAND_CALIBRATE_ZERO[] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t mhz19_checksum(const uint8_t *command) {
uint8_t sum = 0;
@@ -17,6 +20,14 @@ uint8_t mhz19_checksum(const uint8_t *command) {
return 0xFF - sum + 0x01;
}
void MHZ19Component::setup() {
if (this->abc_boot_logic_ == MHZ19_ABC_ENABLED) {
this->abc_enable();
} else if (this->abc_boot_logic_ == MHZ19_ABC_DISABLED) {
this->abc_disable();
}
}
void MHZ19Component::update() {
uint8_t response[MHZ19_RESPONSE_LENGTH];
if (!this->mhz19_write_command_(MHZ19_COMMAND_GET_PPM, response)) {
@@ -50,6 +61,21 @@ void MHZ19Component::update() {
this->temperature_sensor_->publish_state(temp);
}
void MHZ19Component::calibrate_zero() {
ESP_LOGD(TAG, "MHZ19 Calibrating zero point");
this->mhz19_write_command_(MHZ19_COMMAND_CALIBRATE_ZERO, nullptr);
}
void MHZ19Component::abc_enable() {
ESP_LOGD(TAG, "MHZ19 Enabling automatic baseline calibration");
this->mhz19_write_command_(MHZ19_COMMAND_ABC_ENABLE, nullptr);
}
void MHZ19Component::abc_disable() {
ESP_LOGD(TAG, "MHZ19 Disabling automatic baseline calibration");
this->mhz19_write_command_(MHZ19_COMMAND_ABC_DISABLE, nullptr);
}
bool MHZ19Component::mhz19_write_command_(const uint8_t *command, uint8_t *response) {
this->flush();
this->write_array(command, MHZ19_REQUEST_LENGTH);
@@ -67,6 +93,12 @@ void MHZ19Component::dump_config() {
ESP_LOGCONFIG(TAG, "MH-Z19:");
LOG_SENSOR(" ", "CO2", this->co2_sensor_);
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
if (this->abc_boot_logic_ == MHZ19_ABC_ENABLED) {
ESP_LOGCONFIG(TAG, " Automatic baseline calibration enabled on boot");
} else if (this->abc_boot_logic_ == MHZ19_ABC_DISABLED) {
ESP_LOGCONFIG(TAG, " Automatic baseline calibration disabled on boot");
}
}
} // namespace mhz19