ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
t6615.cpp
Go to the documentation of this file.
1#include "t6615.h"
3#include "esphome/core/log.h"
4
5namespace esphome::t6615 {
6
7ESPHOME_LOG_TAG(TAG, "t6615");
8
9static const uint32_t T6615_TIMEOUT = 1000;
10static const uint8_t T6615_MAGIC = 0xFF;
11static const uint8_t T6615_ADDR_HOST = 0xFA;
12static const uint8_t T6615_ADDR_SENSOR = 0xFE;
13static const uint8_t T6615_COMMAND_GET_PPM[] = {0x02, 0x03};
14static const uint8_t T6615_COMMAND_GET_SERIAL[] = {0x02, 0x01};
15static const uint8_t T6615_COMMAND_GET_VERSION[] = {0x02, 0x0D};
16static const uint8_t T6615_COMMAND_GET_ELEVATION[] = {0x02, 0x0F};
17static const uint8_t T6615_COMMAND_GET_ABC[] = {0xB7, 0x00};
18static const uint8_t T6615_COMMAND_ENABLE_ABC[] = {0xB7, 0x01};
19static const uint8_t T6615_COMMAND_DISABLE_ABC[] = {0xB7, 0x02};
20static const uint8_t T6615_COMMAND_SET_ELEVATION[] = {0x03, 0x0F};
21
23 this->command_time_ = millis();
25 this->write_byte(T6615_MAGIC);
26 this->write_byte(T6615_ADDR_SENSOR);
27 this->write_byte(sizeof(T6615_COMMAND_GET_PPM));
28 this->write_array(T6615_COMMAND_GET_PPM, sizeof(T6615_COMMAND_GET_PPM));
29}
30
32 if (this->available() < 5) {
33 if (this->command_ == T6615Command::GET_PPM && millis() - this->command_time_ > T6615_TIMEOUT) {
34 /* command got eaten, clear the buffer and fire another */
35 this->discard_available();
36 this->send_ppm_command_();
37 }
38 return;
39 }
40
41 uint8_t response_buffer[6];
42
43 /* by the time we get here, we know we have at least five bytes in the buffer */
44 this->read_array(response_buffer, 5);
45
46 // Read header
47 if (!this->status_comm_result(response_buffer[0] == T6615_MAGIC && response_buffer[1] == T6615_ADDR_HOST)) {
48 ESP_LOGW(TAG, "Got bad data from T6615! Magic was %02X and address was %02X", response_buffer[0],
49 response_buffer[1]);
50 this->discard_available();
51 /* try again to read the sensor */
52 this->send_ppm_command_();
53 return;
54 }
55
56 switch (this->command_) {
58 const uint16_t ppm = encode_uint16(response_buffer[3], response_buffer[4]);
59 ESP_LOGD(TAG, "T6615 Received CO₂=%uppm", ppm);
60 PUBLISH_STATE(this->co2_sensor_, ppm);
61 break;
62 }
63 default:
64 break;
65 }
66 this->command_time_ = 0;
68}
69
70void T6615Component::update() { this->query_ppm_(); }
71
73 if (this->co2_sensor_ == nullptr ||
74 (this->command_ != T6615Command::NONE && millis() - this->command_time_ < T6615_TIMEOUT)) {
75 return;
76 }
77
78 this->send_ppm_command_();
79}
80
81void T6615Component::dump_config() {
82 ESP_LOGCONFIG(TAG, "T6615:");
83 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
84}
85
86} // namespace esphome::t6615
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
virtual void loop()
This method will be called repeatedly.
Definition component.cpp:86
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_byte(uint8_t data)
Definition uart.h:22
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
ESPHOME_LOG_TAG(TAG, "t6615")
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
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t