ESPHome 2026.11.0-dev
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 if (this->co2_sensor_ != nullptr)
61 this->co2_sensor_->publish_state(ppm);
62 break;
63 }
64 default:
65 break;
66 }
67 this->command_time_ = 0;
69}
70
72
74 if (this->co2_sensor_ == nullptr ||
75 (this->command_ != T6615Command::NONE && millis() - this->command_time_ < T6615_TIMEOUT)) {
76 return;
77 }
78
79 this->send_ppm_command_();
80}
81
83 ESP_LOGCONFIG(TAG, "T6615:");
84 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
85}
86
87} // namespace esphome::t6615
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
void dump_config() override
Definition t6615.cpp:82
sensor::Sensor * co2_sensor_
Definition t6615.h:37
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:998
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t