Skip to content

Instantly share code, notes, and snippets.

@robbi5
Created August 5, 2018 09:55
Show Gist options
  • Save robbi5/1019297126d101018fa12a7bca9f326a to your computer and use it in GitHub Desktop.
Save robbi5/1019297126d101018fa12a7bca9f326a to your computer and use it in GitHub Desktop.
esphomeyaml mh-z19
// Auto generated code by esphomeyaml
#include "esphomelib/application.h"
#include <MHZ19_uart.h>
using namespace esphomelib;
class MHZ19Sensor : public sensor::PollingSensorComponent {
protected:
GPIOPin *pin_rx_;
GPIOPin *pin_tx_;
MHZ19_uart mhz19;
public:
MHZ19Sensor(const std::string &name, uint32_t update_interval, GPIOPin *pin_rx, GPIOPin *pin_tx)
: sensor::PollingSensorComponent(name, update_interval), pin_rx_(pin_rx), pin_tx_(pin_tx) {}
void setup() override {
this->pin_rx_->setup();
this->pin_rx_->digital_write(false);
this->pin_tx_->setup();
this->pin_tx_->digital_write(true);
mhz19.begin(this->pin_rx_->get_pin(), this->pin_tx_->get_pin());
mhz19.setAutoCalibration(false);
}
void update() override {
int co2ppm = mhz19.getPPM();
push_new_value(co2ppm);
}
std::string icon() override { return "mdi:cloud"; }
std::string unit_of_measurement() override { return "ppm"; }
int8_t accuracy_decimals() override { return 0; }
};
// should live in Application.h when merged into core
struct MakeMHZ19Sensor {
MHZ19Sensor *mhz19;
sensor::MQTTSensorComponent *mqtt;
};
// should live in Application.cpp when merged into core
MakeMHZ19Sensor make_mhz19_sensor(const std::string &name,
const GPIOInputPin &rx,
const GPIOOutputPin &tx,
uint32_t update_interval) {
auto *sensor = App.register_component(
new MHZ19Sensor(name, update_interval, rx.copy(), tx.copy())
);
return MakeMHZ19Sensor {
.mhz19 = sensor,
.mqtt = App.register_sensor(sensor),
};
}
void setup() {
// ===== DO NOT EDIT ANYTHING BELOW THIS LINE =====
// ========== AUTO GENERATED CODE BEGIN ===========
// ...
// =========== AUTO GENERATED CODE END ============
// ========= YOU CAN EDIT AFTER THIS LINE =========
auto application_makemhz19sensor = make_mhz19_sensor("CO2", D1, D2, 15000);
App.setup();
}
void loop() {
App.loop();
delay(16);
}
[common]
lib_deps = MHZ19_uart@0.3
build_flags =
upload_flags =
; ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment