ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
htu31d.cpp
Go to the documentation of this file.
1/*
2 * This file contains source code derived from Adafruit_HTU31D which is under
3 * the BSD license:
4 * Written by Limor Fried/Ladyada for Adafruit Industries.
5 * BSD license, all text above must be included in any redistribution.
6 *
7 * Modifications made by Mark Spicer.
8 */
9
10#include "htu31d.h"
11#include "esphome/core/hal.h"
13#include "esphome/core/log.h"
14
15#include <cinttypes>
16
17namespace esphome::htu31d {
18
20ESPHOME_LOG_TAG(TAG, "htu31d");
21
23static const uint8_t HTU31D_DEFAULT_I2CADDR = 0x40;
24
26static const uint8_t HTU31D_READTEMPHUM = 0x00;
27
29static const uint8_t HTU31D_CONVERSION = 0x40;
30
32static const uint8_t HTU31D_READSERIAL = 0x0A;
33
35static const uint8_t HTU31D_HEATERON = 0x04;
36
38static const uint8_t HTU31D_HEATEROFF = 0x02;
39
41static const uint8_t HTU31D_RESET = 0x1E;
42
44static const uint8_t HTU31D_DIAGNOSTICS = 0x08;
45
51 if (!this->reset_()) {
52 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
53 return;
54 }
55
56 if (this->read_serial_num_() == 0) {
57 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
58 return;
59 }
60}
61
66void HTU31DComponent::update() {
67 ESP_LOGD(TAG, "Checking temperature and humidty values");
68
69 // Trigger a conversion. From the spec sheet: The conversion command triggers
70 // a single temperature and humidity conversion.
71 if (this->write_register(HTU31D_CONVERSION, nullptr, 0) != i2c::ERROR_OK) {
72 this->status_set_warning();
73 ESP_LOGE(TAG, "Received errror writing conversion register");
74 return;
75 }
76
77 // Wait conversion time.
78 this->set_timeout(20, [this]() {
79 uint8_t thdata[6];
80 if (this->read_register(HTU31D_READTEMPHUM, thdata, 6) != i2c::ERROR_OK) {
81 this->status_set_warning();
82 ESP_LOGE(TAG, "Error reading temperature/humidty register");
83 return;
84 }
85
86 // Calculate temperature value.
87 uint16_t raw_temp = encode_uint16(thdata[0], thdata[1]);
88
89 uint8_t crc = crc8(thdata, 2, 0, 0x31, true);
90 if (crc != thdata[2]) {
91 this->status_set_warning();
92 ESP_LOGE(TAG, "Error validating temperature CRC");
93 return;
94 }
95
96 float temperature = raw_temp;
97 temperature /= 65535.0f;
98 temperature *= 165;
99 temperature -= 40;
100
101 PUBLISH_STATE(this->temperature_sensor_, temperature);
102
103 // Calculate humidty value.
104 uint16_t raw_hum = encode_uint16(thdata[3], thdata[4]);
105
106 crc = crc8(thdata + 3, 2, 0, 0x31, true);
107 if (crc != thdata[5]) {
108 this->status_set_warning();
109 ESP_LOGE(TAG, "Error validating humidty CRC");
110 return;
111 }
112
113 float humidity = raw_hum;
114 humidity /= 65535.0f;
115 humidity *= 100;
116
117 PUBLISH_STATE(this->humidity_sensor_, humidity);
118
119 ESP_LOGD(TAG, "Got Temperature=%.1f°C Humidity=%.1f%%", temperature, humidity);
120 this->status_clear_warning();
121 });
122}
123
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_);
133}
134
141 if (this->write_register(HTU31D_RESET, nullptr, 0) != i2c::ERROR_OK) {
142 return false;
143 }
144
145 delay(15);
146 return true;
147}
148
155 uint8_t reply[4];
156 uint32_t serial = 0;
157 uint8_t padding = 0;
158
159 // Verify we can read the device serial.
160 if (this->read_register(HTU31D_READSERIAL, reply, 4) != i2c::ERROR_OK) {
161 ESP_LOGE(TAG, "Error reading device serial");
162 return 0;
163 }
164
165 serial = encode_uint32(reply[0], reply[1], reply[2], padding);
166
167 uint8_t crc = crc8(reply, 3, 0, 0x31, true);
168 if (crc != reply[3]) {
169 ESP_LOGE(TAG, "Error validating serial CRC");
170 return 0;
171 }
172
173 ESP_LOGD(TAG, "Found serial: 0x%" PRIX32, serial);
174
175 return serial;
176}
177
184bool HTU31DComponent::is_heater_enabled() {
185 uint8_t reply[1];
186 uint8_t heater_enabled_position = 0;
187 uint8_t mask = 1 << heater_enabled_position;
188 uint8_t diagnostics = 0;
189
190 if (this->read_register(HTU31D_DIAGNOSTICS, reply, 1) != i2c::ERROR_OK) {
191 ESP_LOGE(TAG, "Error reading device serial");
192 return false;
193 }
194
195 diagnostics = reply[0];
196 return (diagnostics & mask) != 0;
197}
198
204void HTU31DComponent::set_heater_state(bool desired) {
205 bool current = this->is_heater_enabled();
206
207 // If the current state matches the desired state, there is nothing to do.
208 if (current == desired) {
209 return;
210 }
211
212 // Update heater state.
214 if (desired) {
215 err = this->write_register(HTU31D_HEATERON, nullptr, 0);
216 } else {
217 err = this->write_register(HTU31D_HEATEROFF, nullptr, 0);
218 }
219
220 // Record any error.
221 if (err != i2c::ERROR_OK) {
222 this->status_set_warning();
223 ESP_LOGE(TAG, "Received error updating heater state");
224 return;
225 }
226}
227
228} // namespace esphome::htu31d
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:309
uint32_t read_serial_num_()
Reads the serial number from the device and checks the CRC.
Definition htu31d.cpp:154
bool reset_()
Sends a 'reset' request to the HTU31D, followed by a 15ms delay.
Definition htu31d.cpp:140
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
Definition i2c.cpp:54
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
Definition i2c.cpp:45
u_int8_t raw_temp
ESPHOME_LOG_TAG(TAG, "htu31d")
Logging prefix.
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:13
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
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.
Definition helpers.h:1051
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.
Definition helpers.cpp:68
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:1043
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12