ESPHome 2025.6.3
Loading...
Searching...
No Matches
tee501.cpp
Go to the documentation of this file.
1#include "tee501.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace tee501 {
7
8static const char *const TAG = "tee501";
9
11 ESP_LOGCONFIG(TAG, "Running setup");
12 uint8_t address[] = {0x70, 0x29};
13 this->write(address, 2, false);
14 uint8_t identification[9];
15 this->read(identification, 9);
16 if (identification[8] != calc_crc8_(identification, 0, 7)) {
17 this->error_code_ = CRC_CHECK_FAILED;
18 this->mark_failed();
19 return;
20 }
21 ESP_LOGV(TAG, " Serial Number: 0x%s", format_hex(identification + 0, 7).c_str());
22}
23
25 ESP_LOGCONFIG(TAG, "TEE501:");
26 LOG_I2C_DEVICE(this);
27 switch (this->error_code_) {
29 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
30 break;
32 ESP_LOGE(TAG, "The crc check failed");
33 break;
34 case NONE:
35 default:
36 break;
37 }
38 LOG_UPDATE_INTERVAL(this);
39 LOG_SENSOR(" ", "TEE501", this);
40}
41
44 uint8_t address_1[] = {0x2C, 0x1B};
45 this->write(address_1, 2, true);
46 this->set_timeout(50, [this]() {
47 uint8_t i2c_response[3];
48 this->read(i2c_response, 3);
49 if (i2c_response[2] != calc_crc8_(i2c_response, 0, 1)) {
50 this->error_code_ = CRC_CHECK_FAILED;
51 this->status_set_warning();
52 return;
53 }
54 float temperature = (float) encode_uint16(i2c_response[0], i2c_response[1]);
55 if (temperature > 55536) {
56 temperature = (temperature - 65536) / 100;
57 } else {
59 }
60 ESP_LOGD(TAG, "Got temperature=%.2f°C", temperature);
61 this->publish_state(temperature);
63 });
64}
65
66unsigned char TEE501Component::calc_crc8_(const unsigned char buf[], unsigned char from, unsigned char to) {
67 unsigned char crc_val = 0xFF;
68 unsigned char i = 0;
69 unsigned char j = 0;
70 for (i = from; i <= to; i++) {
71 int cur_val = buf[i];
72 for (j = 0; j < 8; j++) {
73 if (((crc_val ^ cur_val) & 0x80) != 0) // If MSBs are not equal
74 {
75 crc_val = ((crc_val << 1) ^ 0x31);
76 } else {
77 crc_val = (crc_val << 1);
78 }
79 cur_val = cur_val << 1;
80 }
81 }
82 return crc_val;
83}
84
85} // namespace tee501
86} // namespace esphome
uint8_t address
Definition bl0906.h:4
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
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:75
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:190
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
enum esphome::tee501::TEE501Component::ErrorCode NONE
unsigned char calc_crc8_(const unsigned char buf[], unsigned char from, unsigned char to)
Definition tee501.cpp:66
float get_setup_priority() const override
Definition tee501.cpp:42
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:360
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:192
uint16_t temperature
Definition sun_gtil2.cpp:12