ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
hc8.cpp
Go to the documentation of this file.
1#include "hc8.h"
4#include "esphome/core/log.h"
5
6#include <array>
7
8namespace esphome::hc8 {
9
10ESPHOME_LOG_TAG(TAG, "hc8");
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};
13
15 // send an initial query to the device, this will
16 // get it out of "active output mode", where it
17 // generates data every second
18 this->write_array(HC8_COMMAND_GET_PPM);
19 this->flush();
20
21 this->discard_available();
22}
23
24void HC8Component::update() {
25 if (!this->warmup_complete_) {
27 uint32_t warmup_ms = this->warmup_seconds_ * 1000;
28 if (now_ms < warmup_ms) {
29 ESP_LOGW(TAG, "HC8 warming up, %" PRIu32 " s left", (warmup_ms - now_ms) / 1000);
30 this->status_set_warning();
31 return;
32 }
33 this->warmup_complete_ = true;
35 }
36
37 this->discard_available();
38
39 this->write_array(HC8_COMMAND_GET_PPM);
40 this->flush();
41
42 // the sensor is a bit slow in responding, so trying to
43 // read immediately after sending a query will timeout
44 this->set_timeout(50, [this]() {
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!");
48 this->status_set_warning();
49 return;
50 }
51
52 if (response[0] != 0x64 || response[1] != 0x69) {
53 ESP_LOGW(TAG, "Invalid preamble from HC8!");
54 this->status_set_warning();
55 return;
56 }
57
58 if (!this->status_comm_result(crc16(response.data(), 12) == encode_uint16(response[13], response[12]))) {
59 ESP_LOGW(TAG, "HC8 Checksum mismatch");
60 return;
61 }
62
63 const uint16_t ppm = encode_uint16(response[5], response[4]);
64 ESP_LOGD(TAG, "HC8 Received CO₂=%uppm", ppm);
65 PUBLISH_STATE(this->co2_sensor_, ppm);
66 });
67}
68
69void HC8Component::calibrate(uint16_t baseline) {
70 ESP_LOGD(TAG, "HC8 Calibrating baseline to %uppm", baseline);
71
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;
76 command[5] = 0;
77
78 // the last byte is a checksum over the data
79 for (uint8_t i = 0; i < 5; ++i)
80 command[5] -= command[i];
81
82 this->write_array(command);
83 this->flush();
84}
85
86void HC8Component::dump_config() {
87 ESP_LOGCONFIG(TAG,
88 "HC8:\n"
89 " Warmup time: %" PRIu32 " s",
90 this->warmup_seconds_);
91 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
92}
93
94} // namespace esphome::hc8
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:309
uint32_t warmup_seconds_
Definition hc8.h:24
UARTFlushResult flush()
Definition uart.h:96
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:43
void discard_available()
Read and drop every byte waiting in the receive buffer.
Definition uart.cpp:36
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
ESPHOME_LOG_TAG(TAG, "hc8")
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.
Definition helpers.cpp:95
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
uint8_t end[39]
Definition sun_gtil2.cpp:17