ESPHome 2025.5.0
Loading...
Searching...
No Matches
t6615.cpp
Go to the documentation of this file.
1#include "t6615.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace t6615 {
6
7static const char *const 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 while (this->available())
36 this->read();
37 this->send_ppm_command_();
38 }
39 return;
40 }
41
42 uint8_t response_buffer[6];
43
44 /* by the time we get here, we know we have at least five bytes in the buffer */
45 this->read_array(response_buffer, 5);
46
47 // Read header
48 if (response_buffer[0] != T6615_MAGIC || response_buffer[1] != T6615_ADDR_HOST) {
49 ESP_LOGW(TAG, "Got bad data from T6615! Magic was %02X and address was %02X", response_buffer[0],
50 response_buffer[1]);
51 /* make sure the buffer is empty */
52 while (this->available())
53 this->read();
54 /* try again to read the sensor */
55 this->send_ppm_command_();
56 this->status_set_warning();
57 return;
58 }
59
61
62 switch (this->command_) {
64 const uint16_t ppm = encode_uint16(response_buffer[3], response_buffer[4]);
65 ESP_LOGD(TAG, "T6615 Received CO₂=%uppm", ppm);
66 if (this->co2_sensor_ != nullptr)
67 this->co2_sensor_->publish_state(ppm);
68 break;
69 }
70 default:
71 break;
72 }
73 this->command_time_ = 0;
75}
76
78
80 if (this->co2_sensor_ == nullptr ||
81 (this->command_ != T6615Command::NONE && millis() - this->command_time_ < T6615_TIMEOUT)) {
82 return;
83 }
84
85 this->send_ppm_command_();
86}
87
90 ESP_LOGCONFIG(TAG, "T6615:");
91 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
92 this->check_uart_settings(19200);
93}
94
95} // namespace t6615
96} // namespace esphome
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
void dump_config() override
Definition t6615.cpp:89
sensor::Sensor * co2_sensor_
Definition t6615.h:40
float get_setup_priority() const override
Definition t6615.cpp:88
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:33
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition uart.cpp:13
void write_byte(uint8_t data)
Definition uart.h:19
void write_array(const uint8_t *data, size_t len)
Definition uart.h:21
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:191
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:27