1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-15 15:18:16 +00:00
This commit is contained in:
Nikolay Vasilchuk 2019-08-05 18:35:31 +03:00
parent 625cdbc5e3
commit fee7ce8396
3 changed files with 96 additions and 8 deletions

View File

@ -10,6 +10,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};
static const uint8_t MHZ19_COMMAND_CALIBRATE_SPAN[] = {0xff, 0x01, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t mhz19_checksum(const uint8_t *command) {
uint8_t sum = 0;
@ -20,12 +22,10 @@ uint8_t mhz19_checksum(const uint8_t *command) {
}
void MHZ19Component::setup() {
if (this->abc_logic_ == MHZ19_ABC_ENABLED) {
ESP_LOGV(TAG, "Enabling ABC on boot");
this->mhz19_write_command_(MHZ19_COMMAND_ABC_ENABLE, nullptr);
} else if (this->abc_logic_ == MHZ19_ABC_DISABLED) {
ESP_LOGV(TAG, "Disabling ABC on boot");
this->mhz19_write_command_(MHZ19_COMMAND_ABC_DISABLE, nullptr);
if (this->abc_boot_logic_ == MHZ19_ABC_ENABLED) {
this->abc_enable();
} else if (this->abc_boot_logic_ == MHZ19_ABC_DISABLED) {
this->abc_disable();
}
}
@ -62,6 +62,26 @@ void MHZ19Component::update() {
this->temperature_sensor_->publish_state(temp);
}
void MHZ19Component::calibrate_zero() {
ESP_LOGI(TAG, "MHZ19 Calibrating zero point");
this->mhz19_write_command_(MHZ19_COMMAND_CALIBRATE_ZERO, nullptr);
}
void MHZ19Component::calibrate_span() {
ESP_LOGI(TAG, "MHZ19 Calibrating span point");
this->mhz19_write_command_(MHZ19_COMMAND_CALIBRATE_SPAN, nullptr);
}
void MHZ19Component::abc_enable() {
ESP_LOGI(TAG, "MHZ19 Enabling ABC");
this->mhz19_write_command_(MHZ19_COMMAND_ABC_ENABLE, nullptr);
}
void MHZ19Component::abc_disable() {
ESP_LOGI(TAG, "MHZ19 Disabling ABC");
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);
@ -79,6 +99,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

View File

@ -1,6 +1,7 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
@ -21,16 +22,57 @@ class MHZ19Component : public PollingComponent, public uart::UARTDevice {
void update() override;
void dump_config() override;
void calibrate_zero();
void calibrate_span();
void abc_enable();
void abc_disable();
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
void set_co2_sensor(sensor::Sensor *co2_sensor) { co2_sensor_ = co2_sensor; }
void set_abc_enabled(bool abc_enabled) { abc_logic_ = abc_enabled ? MHZ19_ABC_ENABLED : MHZ19_ABC_DISABLED; }
void set_abc_enabled(bool abc_enabled) { abc_boot_logic_ = abc_enabled ? MHZ19_ABC_ENABLED : MHZ19_ABC_DISABLED; }
protected:
bool mhz19_write_command_(const uint8_t *command, uint8_t *response);
sensor::Sensor *temperature_sensor_{nullptr};
sensor::Sensor *co2_sensor_{nullptr};
MHZ19ABCLogic abc_logic_{MHZ19_ABC_NONE};
MHZ19ABCLogic abc_boot_logic_{MHZ19_ABC_NONE};
};
template<typename... Ts> class MHZ19CalibrateZeroAction : public Action<Ts...> {
public:
MHZ19CalibrateZeroAction(MHZ19Component *mhz19) : mhz19_(mhz19) {}
void play(Ts... x) override { this->mhz19_->calibrate_zero(); }
protected:
MHZ19Component *mhz19_;
};
template<typename... Ts> class MHZ19CalibrateSpanAction : public Action<Ts...> {
public:
MHZ19CalibrateSpanAction(MHZ19Component *mhz19) : mhz19_(mhz19) {}
void play(Ts... x) override { this->mhz19_->calibrate_span(); }
protected:
MHZ19Component *mhz19_;
};
template<typename... Ts> class MHZ19ABCEnableAction : public Action<Ts...> {
public:
MHZ19ABCEnableAction(MHZ19Component *mhz19) : mhz19_(mhz19) {}
void play(Ts... x) override { this->mhz19_->abc_enable(); }
protected:
MHZ19Component *mhz19_;
};
template<typename... Ts> class MHZ19ABCDisableAction : public Action<Ts...> {
public:
MHZ19ABCDisableAction(MHZ19Component *mhz19) : mhz19_(mhz19) {}
void play(Ts... x) override { this->mhz19_->abc_disable(); }
protected:
MHZ19Component *mhz19_;
};
} // namespace mhz19

View File

@ -1,5 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.automation import maybe_simple_id
from esphome.components import sensor, uart
from esphome.const import CONF_CO2, CONF_ID, CONF_TEMPERATURE, ICON_PERIODIC_TABLE_CO2, \
UNIT_PARTS_PER_MILLION, UNIT_CELSIUS, ICON_THERMOMETER
@ -10,6 +12,10 @@ CONF_AUTOMATIC_BASELINE_CALIBRATION = 'automatic_baseline_calibration'
mhz19_ns = cg.esphome_ns.namespace('mhz19')
MHZ19Component = mhz19_ns.class_('MHZ19Component', cg.PollingComponent, uart.UARTDevice)
MHZ19CalibrateZeroAction = mhz19_ns.class_('MHZ19CalibrateZeroAction', automation.Action)
MHZ19CalibrateSpanAction = mhz19_ns.class_('MHZ19CalibrateSpanAction', automation.Action)
MHZ19ABCEnableAction = mhz19_ns.class_('MHZ19ABCEnableAction', automation.Action)
MHZ19ABCDisableAction = mhz19_ns.class_('MHZ19ABCDisableAction', automation.Action)
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(MHZ19Component),
@ -34,3 +40,17 @@ def to_code(config):
if CONF_AUTOMATIC_BASELINE_CALIBRATION in config:
cg.add(var.set_abc_enabled(config[CONF_AUTOMATIC_BASELINE_CALIBRATION]))
CALIBRATION_ACTION_SCHEMA = maybe_simple_id({
cv.Required(CONF_ID): cv.use_id(MHZ19Component),
})
@automation.register_action('mhz19.calibrate_zero', MHZ19CalibrateZeroAction, CALIBRATION_ACTION_SCHEMA)
@automation.register_action('mhz19.calibrate_span', MHZ19CalibrateSpanAction, CALIBRATION_ACTION_SCHEMA)
@automation.register_action('mhz19.abc_enable', MHZ19ABCEnableAction, CALIBRATION_ACTION_SCHEMA)
@automation.register_action('mhz19.abc_disable', MHZ19ABCDisableAction, CALIBRATION_ACTION_SCHEMA)
def mhz19_calibration_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(action_id, template_arg, paren)