ESPHome 2025.8.2
Loading...
Searching...
No Matches
hte501.cpp
Go to the documentation of this file.
1#include "hte501.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace hte501 {
7
8static const char *const TAG = "hte501";
9
11 uint8_t address[] = {0x70, 0x29};
12 uint8_t identification[9];
13 this->write_read(address, sizeof address, identification, sizeof identification);
14 if (identification[8] != calc_crc8_(identification, 0, 7)) {
15 this->error_code_ = CRC_CHECK_FAILED;
16 this->mark_failed();
17 return;
18 }
19 ESP_LOGV(TAG, " Serial Number: 0x%s", format_hex(identification + 0, 7).c_str());
20}
21
23 ESP_LOGCONFIG(TAG, "HTE501:");
24 LOG_I2C_DEVICE(this);
25 switch (this->error_code_) {
27 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
28 break;
30 ESP_LOGE(TAG, "The crc check failed");
31 break;
32 case NONE:
33 default:
34 break;
35 }
36 LOG_UPDATE_INTERVAL(this);
37 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
38 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
39}
40
43 uint8_t address_1[] = {0x2C, 0x1B};
44 this->write(address_1, 2);
45 this->set_timeout(50, [this]() {
46 uint8_t i2c_response[6];
47 this->read(i2c_response, 6);
48 if (i2c_response[2] != calc_crc8_(i2c_response, 0, 1) && i2c_response[5] != calc_crc8_(i2c_response, 3, 4)) {
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 {
58 }
59 float humidity = ((float) encode_uint16(i2c_response[3], i2c_response[4])) / 100.0f;
60
61 ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity);
62 if (this->temperature_sensor_ != nullptr)
63 this->temperature_sensor_->publish_state(temperature);
64 if (this->humidity_sensor_ != nullptr)
65 this->humidity_sensor_->publish_state(humidity);
67 });
68}
69
70unsigned char HTE501Component::calc_crc8_(const unsigned char buf[], unsigned char from, unsigned char to) {
71 unsigned char crc_val = 0xFF;
72 unsigned char i = 0;
73 unsigned char j = 0;
74 for (i = from; i <= to; i++) {
75 int cur_val = buf[i];
76 for (j = 0; j < 8; j++) {
77 if (((crc_val ^ cur_val) & 0x80) != 0) // If MSBs are not equal
78 {
79 crc_val = ((crc_val << 1) ^ 0x31);
80 } else {
81 crc_val = (crc_val << 1);
82 }
83 cur_val = cur_val << 1;
84 }
85 }
86 return crc_val;
87}
88} // namespace hte501
89} // 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=nullptr)
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.
sensor::Sensor * temperature_sensor_
Definition hte501.h:23
float get_setup_priority() const override
Definition hte501.cpp:41
sensor::Sensor * humidity_sensor_
Definition hte501.h:24
unsigned char calc_crc8_(const unsigned char buf[], unsigned char from, unsigned char to)
Definition hte501.cpp:70
enum esphome::hte501::HTE501Component::ErrorCode NONE
ErrorCode write_read(const uint8_t *write_data, size_t write_len, uint8_t *read_data, size_t read_len) const
writes an array of bytes to a device, then reads an array, as a single transaction
Definition i2c.h:194
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:184
ErrorCode read(uint8_t *data, size_t len) const
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:45
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
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:249
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:173
uint16_t temperature
Definition sun_gtil2.cpp:12