ESPHome  2024.11.0
tee501.cpp
Go to the documentation of this file.
1 #include "tee501.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace tee501 {
6 
7 static const char *const TAG = "tee501";
8 
10  ESP_LOGCONFIG(TAG, "Setting up TEE501...");
11  uint8_t address[] = {0x70, 0x29};
12  this->write(address, 2, false);
13  uint8_t identification[9];
14  this->read(identification, 9);
15  if (identification[8] != calc_crc8_(identification, 0, 7)) {
16  this->error_code_ = CRC_CHECK_FAILED;
17  this->mark_failed();
18  return;
19  }
20  ESP_LOGV(TAG, " Serial Number: 0x%s", format_hex(identification + 0, 7).c_str());
21 }
22 
24  ESP_LOGCONFIG(TAG, "TEE501:");
25  LOG_I2C_DEVICE(this);
26  switch (this->error_code_) {
28  ESP_LOGE(TAG, "Communication with TEE501 failed!");
29  break;
30  case CRC_CHECK_FAILED:
31  ESP_LOGE(TAG, "The crc check failed");
32  break;
33  case NONE:
34  default:
35  break;
36  }
37  LOG_UPDATE_INTERVAL(this);
38  LOG_SENSOR(" ", "TEE501", this);
39 }
40 
43  uint8_t address_1[] = {0x2C, 0x1B};
44  this->write(address_1, 2, true);
45  this->set_timeout(50, [this]() {
46  uint8_t i2c_response[3];
47  this->read(i2c_response, 3);
48  if (i2c_response[2] != calc_crc8_(i2c_response, 0, 1)) {
49  this->error_code_ = CRC_CHECK_FAILED;
50  this->status_set_warning();
51  return;
52  }
53  float temperature = (float) encode_uint16(i2c_response[0], i2c_response[1]);
54  if (temperature > 55536) {
55  temperature = (temperature - 65536) / 100;
56  } else {
57  temperature = temperature / 100;
58  }
59  ESP_LOGD(TAG, "Got temperature=%.2f°C", temperature);
60  this->publish_state(temperature);
61  this->status_clear_warning();
62  });
63 }
64 
65 unsigned char TEE501Component::calc_crc8_(const unsigned char buf[], unsigned char from, unsigned char to) {
66  unsigned char crc_val = 0xFF;
67  unsigned char i = 0;
68  unsigned char j = 0;
69  for (i = from; i <= to; i++) {
70  int cur_val = buf[i];
71  for (j = 0; j < 8; j++) {
72  if (((crc_val ^ cur_val) & 0x80) != 0) // If MSBs are not equal
73  {
74  crc_val = ((crc_val << 1) ^ 0x31);
75  } else {
76  crc_val = (crc_val << 1);
77  }
78  cur_val = cur_val << 1;
79  }
80  }
81  return crc_val;
82 }
83 
84 } // namespace tee501
85 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
float get_setup_priority() const override
Definition: tee501.cpp:41
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
Definition: helpers.cpp:352
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition: i2c.h:160
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:69
enum esphome::tee501::TEE501Component::ErrorCode NONE
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition: i2c.h:186
void dump_config() override
Definition: tee501.cpp:23
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
uint16_t temperature
Definition: sun_gtil2.cpp:26
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:183
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t address
Definition: bl0906.h:211
unsigned char calc_crc8_(const unsigned char buf[], unsigned char from, unsigned char to)
Definition: tee501.cpp:65