esphome/esphome/components/mhz19/mhz19.cpp
Otto Winter 6682c43dfa
🏗 Merge C++ into python codebase (#504)
## Description:

Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97

Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍

Progress:
- Core support (file copy etc): 80%
- Base Abstractions (light, switch): ~50%
- Integrations: ~10%
- Working? Yes, (but only with ported components).

Other refactors:
- Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`)
- Rework coroutine syntax
- Move from `component/platform.py` to `domain/component.py` structure as with HA
- Move all defaults out of C++ and into config validation.
- Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration.
- Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit.

Future work:
- Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block
- Enable loading from `custom_components` folder.

**Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97

**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here>

## Checklist:
  - [ ] The code change is tested and works locally.
  - [ ] Tests have been added to verify that the new code works (under `tests/` folder).

If user exposed functionality or configuration variables are added/changed:
  - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
2019-04-17 12:06:00 +02:00

73 lines
2.2 KiB
C++

#include "mhz19.h"
#include "esphome/core/log.h"
namespace esphome {
namespace mhz19 {
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};
uint8_t mhz19_checksum(const uint8_t *command) {
uint8_t sum = 0;
for (uint8_t i = 1; i < MHZ19_REQUEST_LENGTH; i++) {
sum += command[i];
}
return 0xFF - sum + 0x01;
}
void MHZ19Component::update() {
uint8_t response[MHZ19_RESPONSE_LENGTH];
if (!this->mhz19_write_command_(MHZ19_COMMAND_GET_PPM, response)) {
ESP_LOGW(TAG, "Reading data from MHZ19 failed!");
this->status_set_warning();
return;
}
if (response[0] != 0xFF || response[1] != 0x86) {
ESP_LOGW(TAG, "Invalid preamble from MHZ19!");
this->status_set_warning();
return;
}
uint8_t checksum = mhz19_checksum(response);
if (response[8] != checksum) {
ESP_LOGW(TAG, "MHZ19 Checksum doesn't match: 0x%02X!=0x%02X", response[8], checksum);
this->status_set_warning();
return;
}
this->status_clear_warning();
const uint16_t ppm = (uint16_t(response[2]) << 8) | response[3];
const int temp = int(response[4]) - 40;
const uint8_t status = response[5];
ESP_LOGD(TAG, "MHZ19 Received CO₂=%uppm Temperature=%d°C Status=0x%02X", ppm, temp, status);
if (this->co2_sensor_ != nullptr)
this->co2_sensor_->publish_state(ppm);
if (this->temperature_sensor_ != nullptr)
this->temperature_sensor_->publish_state(temp);
}
bool MHZ19Component::mhz19_write_command_(const uint8_t *command, uint8_t *response) {
this->flush();
this->write_array(command, MHZ19_REQUEST_LENGTH);
this->write_byte(mhz19_checksum(command));
if (response == nullptr)
return true;
bool ret = this->read_array(response, MHZ19_RESPONSE_LENGTH);
this->flush();
return ret;
}
float MHZ19Component::get_setup_priority() const { return setup_priority::DATA; }
void MHZ19Component::dump_config() {
ESP_LOGCONFIG(TAG, "MH-Z19:");
LOG_SENSOR(" ", "CO2", this->co2_sensor_);
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
}
} // namespace mhz19
} // namespace esphome