ESPHome  2024.10.2
t6615.cpp
Go to the documentation of this file.
1 #include "t6615.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace t6615 {
6 
7 static const char *const TAG = "t6615";
8 
9 static const uint32_t T6615_TIMEOUT = 1000;
10 static const uint8_t T6615_MAGIC = 0xFF;
11 static const uint8_t T6615_ADDR_HOST = 0xFA;
12 static const uint8_t T6615_ADDR_SENSOR = 0xFE;
13 static const uint8_t T6615_COMMAND_GET_PPM[] = {0x02, 0x03};
14 static const uint8_t T6615_COMMAND_GET_SERIAL[] = {0x02, 0x01};
15 static const uint8_t T6615_COMMAND_GET_VERSION[] = {0x02, 0x0D};
16 static const uint8_t T6615_COMMAND_GET_ELEVATION[] = {0x02, 0x0F};
17 static const uint8_t T6615_COMMAND_GET_ABC[] = {0xB7, 0x00};
18 static const uint8_t T6615_COMMAND_ENABLE_ABC[] = {0xB7, 0x01};
19 static const uint8_t T6615_COMMAND_DISABLE_ABC[] = {0xB7, 0x02};
20 static 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 
60  this->status_clear_warning();
61 
62  switch (this->command_) {
63  case T6615Command::GET_PPM: {
64  const uint16_t ppm = encode_uint16(response_buffer[3], response_buffer[4]);
65  ESP_LOGD(TAG, "T6615 Received CO₂=%uppm", ppm);
66  this->co2_sensor_->publish_state(ppm);
67  break;
68  }
69  default:
70  break;
71  }
72  this->command_time_ = 0;
74 }
75 
77 
79  if (this->co2_sensor_ == nullptr ||
80  (this->command_ != T6615Command::NONE && millis() - this->command_time_ < T6615_TIMEOUT)) {
81  return;
82  }
83 
84  this->send_ppm_command_();
85 }
86 
89  ESP_LOGCONFIG(TAG, "T6615:");
90  LOG_SENSOR(" ", "CO2", this->co2_sensor_);
91  this->check_uart_settings(19200);
92 }
93 
94 } // namespace t6615
95 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
optional< std::array< uint8_t, N > > read_array()
Definition: uart.h:33
void write_array(const uint8_t *data, size_t len)
Definition: uart.h:21
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
void write_byte(uint8_t data)
Definition: uart.h:19
void dump_config() override
Definition: t6615.cpp:88
void update() override
Definition: t6615.cpp:76
void loop() override
Definition: t6615.cpp:31
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
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 status_clear_warning()
Definition: component.cpp:166
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
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:182
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
float get_setup_priority() const override
Definition: t6615.cpp:87
sensor::Sensor * co2_sensor_
Definition: t6615.h:40