23static const uint8_t HTU31D_DEFAULT_I2CADDR = 0x40;
26static const uint8_t HTU31D_READTEMPHUM = 0x00;
29static const uint8_t HTU31D_CONVERSION = 0x40;
32static const uint8_t HTU31D_READSERIAL = 0x0A;
35static const uint8_t HTU31D_HEATERON = 0x04;
38static const uint8_t HTU31D_HEATEROFF = 0x02;
41static const uint8_t HTU31D_RESET = 0x1E;
44static const uint8_t HTU31D_DIAGNOSTICS = 0x08;
66void HTU31DComponent::update() {
67 ESP_LOGD(TAG,
"Checking temperature and humidty values");
73 ESP_LOGE(TAG,
"Received errror writing conversion register");
82 ESP_LOGE(TAG,
"Error reading temperature/humidty register");
89 uint8_t crc =
crc8(thdata, 2, 0, 0x31,
true);
90 if (crc != thdata[2]) {
92 ESP_LOGE(TAG,
"Error validating temperature CRC");
101 PUBLISH_STATE(this->temperature_sensor_,
temperature);
106 crc =
crc8(thdata + 3, 2, 0, 0x31,
true);
107 if (crc != thdata[5]) {
109 ESP_LOGE(TAG,
"Error validating humidty CRC");
113 float humidity = raw_hum;
114 humidity /= 65535.0f;
117 PUBLISH_STATE(this->humidity_sensor_, humidity);
119 ESP_LOGD(TAG,
"Got Temperature=%.1f°C Humidity=%.1f%%",
temperature, humidity);
127void HTU31DComponent::dump_config() {
128 ESP_LOGCONFIG(TAG,
"HTU31D:");
129 LOG_I2C_DEVICE(
this);
130 LOG_UPDATE_INTERVAL(
this);
131 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
132 LOG_SENSOR(
" ",
"Humidity", this->humidity_sensor_);
161 ESP_LOGE(TAG,
"Error reading device serial");
165 serial =
encode_uint32(reply[0], reply[1], reply[2], padding);
167 uint8_t crc =
crc8(reply, 3, 0, 0x31,
true);
168 if (crc != reply[3]) {
169 ESP_LOGE(TAG,
"Error validating serial CRC");
173 ESP_LOGD(TAG,
"Found serial: 0x%" PRIX32, serial);
184bool HTU31DComponent::is_heater_enabled() {
186 uint8_t heater_enabled_position = 0;
187 uint8_t mask = 1 << heater_enabled_position;
188 uint8_t diagnostics = 0;
191 ESP_LOGE(TAG,
"Error reading device serial");
195 diagnostics = reply[0];
196 return (diagnostics & mask) != 0;
204void HTU31DComponent::set_heater_state(
bool desired) {
205 bool current = this->is_heater_enabled();
208 if (current == desired) {
223 ESP_LOGE(TAG,
"Received error updating heater state");
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
void status_set_warning()
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
void status_clear_warning()
uint32_t read_serial_num_()
Reads the serial number from the device and checks the CRC.
bool reset_()
Sends a 'reset' request to the HTU31D, followed by a 15ms delay.
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
ESPHOME_LOG_TAG(TAG, "htu31d")
Logging prefix.
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
@ ERROR_OK
No error found during execution of method.
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void HOT delay(uint32_t ms)