1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-15 07:08:20 +00:00

First try

This commit is contained in:
Nikolay Vasilchuk 2019-07-31 18:35:13 +03:00
parent 11e88019c2
commit 21d1eff6fc
3 changed files with 27 additions and 0 deletions

View File

@ -8,6 +8,8 @@ static const char *TAG = "mhz19";
static const uint8_t MHZ19_REQUEST_LENGTH = 8; static const uint8_t MHZ19_REQUEST_LENGTH = 8;
static const uint8_t MHZ19_RESPONSE_LENGTH = 9; 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_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};
uint8_t mhz19_checksum(const uint8_t *command) { uint8_t mhz19_checksum(const uint8_t *command) {
uint8_t sum = 0; uint8_t sum = 0;
@ -17,6 +19,16 @@ uint8_t mhz19_checksum(const uint8_t *command) {
return 0xFF - sum + 0x01; return 0xFF - sum + 0x01;
} }
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);
}
}
void MHZ19Component::update() { void MHZ19Component::update() {
uint8_t response[MHZ19_RESPONSE_LENGTH]; uint8_t response[MHZ19_RESPONSE_LENGTH];
if (!this->mhz19_write_command_(MHZ19_COMMAND_GET_PPM, response)) { if (!this->mhz19_write_command_(MHZ19_COMMAND_GET_PPM, response)) {

View File

@ -7,21 +7,30 @@
namespace esphome { namespace esphome {
namespace mhz19 { namespace mhz19 {
enum MHZ19ABCLogic {
MHZ19_ABC_NONE = 0,
MHZ19_ABC_ENABLED,
MHZ19_ABC_DISABLED
};
class MHZ19Component : public PollingComponent, public uart::UARTDevice { class MHZ19Component : public PollingComponent, public uart::UARTDevice {
public: public:
float get_setup_priority() const override; float get_setup_priority() const override;
void setup() override;
void update() override; void update() override;
void dump_config() override; void dump_config() override;
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; } 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_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; }
protected: protected:
bool mhz19_write_command_(const uint8_t *command, uint8_t *response); bool mhz19_write_command_(const uint8_t *command, uint8_t *response);
sensor::Sensor *temperature_sensor_{nullptr}; sensor::Sensor *temperature_sensor_{nullptr};
sensor::Sensor *co2_sensor_{nullptr}; sensor::Sensor *co2_sensor_{nullptr};
MHZ19ABCLogic abc_logic_{MHZ19_ABC_NONE}
}; };
} // namespace mhz19 } // namespace mhz19

View File

@ -6,6 +6,8 @@ from esphome.const import CONF_CO2, CONF_ID, CONF_TEMPERATURE, ICON_PERIODIC_TAB
DEPENDENCIES = ['uart'] DEPENDENCIES = ['uart']
CONF_AUTOMATIC_BASELINE_CALIBRATION = 'automatic_baseline_calibration'
mhz19_ns = cg.esphome_ns.namespace('mhz19') mhz19_ns = cg.esphome_ns.namespace('mhz19')
MHZ19Component = mhz19_ns.class_('MHZ19Component', cg.PollingComponent, uart.UARTDevice) MHZ19Component = mhz19_ns.class_('MHZ19Component', cg.PollingComponent, uart.UARTDevice)
@ -13,6 +15,7 @@ CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(MHZ19Component), cv.GenerateID(): cv.declare_id(MHZ19Component),
cv.Required(CONF_CO2): sensor.sensor_schema(UNIT_PARTS_PER_MILLION, ICON_PERIODIC_TABLE_CO2, 0), cv.Required(CONF_CO2): sensor.sensor_schema(UNIT_PARTS_PER_MILLION, ICON_PERIODIC_TABLE_CO2, 0),
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 0), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 0),
cv.Optional(CONF_AUTOMATIC_BASELINE_CALIBRATION): cv.boolean,
}).extend(cv.polling_component_schema('60s')).extend(uart.UART_DEVICE_SCHEMA) }).extend(cv.polling_component_schema('60s')).extend(uart.UART_DEVICE_SCHEMA)
@ -28,3 +31,6 @@ def to_code(config):
if CONF_TEMPERATURE in config: if CONF_TEMPERATURE in config:
sens = yield sensor.new_sensor(config[CONF_TEMPERATURE]) sens = yield sensor.new_sensor(config[CONF_TEMPERATURE])
cg.add(var.set_temperature_sensor(sens)) cg.add(var.set_temperature_sensor(sens))
if CONF_AUTOMATIC_BASELINE_CALIBRATION in config:
cg.add(var.set_abc_enabled(config[CONF_AUTOMATIC_BASELINE_CALIBRATION]))