11static const std::array<uint8_t, 5> HC8_COMMAND_GET_PPM{0x64, 0x69, 0x03, 0x5E, 0x4E};
12static const std::array<uint8_t, 3> HC8_COMMAND_CALIBRATE_PREAMBLE{0x11, 0x03, 0x03};
24void HC8Component::update() {
28 if (now_ms < warmup_ms) {
29 ESP_LOGW(TAG,
"HC8 warming up, %" PRIu32
" s left", (warmup_ms - now_ms) / 1000);
45 std::array<uint8_t, 14> response;
46 if (!this->
read_array(response.data(), response.size())) {
47 ESP_LOGW(TAG,
"Reading data from HC8 failed!");
52 if (response[0] != 0x64 || response[1] != 0x69) {
53 ESP_LOGW(TAG,
"Invalid preamble from HC8!");
59 ESP_LOGW(TAG,
"HC8 Checksum mismatch");
64 ESP_LOGD(TAG,
"HC8 Received CO₂=%uppm", ppm);
65 PUBLISH_STATE(this->co2_sensor_, ppm);
69void HC8Component::calibrate(uint16_t baseline) {
70 ESP_LOGD(TAG,
"HC8 Calibrating baseline to %uppm", baseline);
72 std::array<uint8_t, 6> command{};
73 std::copy(begin(HC8_COMMAND_CALIBRATE_PREAMBLE),
end(HC8_COMMAND_CALIBRATE_PREAMBLE), begin(command));
74 command[3] = baseline >> 8;
75 command[4] = baseline;
79 for (uint8_t i = 0; i < 5; ++i)
80 command[5] -= command[i];
86void HC8Component::dump_config() {
89 " Warmup time: %" PRIu32
" s",
91 LOG_SENSOR(
" ",
"CO2", this->co2_sensor_);
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.