21 static const char *
const TAG =
"htu31d";
24 static const uint8_t HTU31D_DEFAULT_I2CADDR = 0x40;
27 static const uint8_t HTU31D_READTEMPHUM = 0x00;
30 static const uint8_t HTU31D_CONVERSION = 0x40;
33 static const uint8_t HTU31D_READSERIAL = 0x0A;
36 static const uint8_t HTU31D_HEATERON = 0x04;
39 static const uint8_t HTU31D_HEATEROFF = 0x02;
42 static const uint8_t HTU31D_RESET = 0x1E;
45 static const uint8_t HTU31D_DIAGNOSTICS = 0x08;
53 uint32_t polynom = 0x98800000;
54 uint32_t msb = 0x80000000;
55 uint32_t mask = 0xFF800000;
56 uint32_t threshold = 0x00000080;
57 uint32_t result = value;
59 while (msb != threshold) {
62 result = ((result ^ polynom) & mask) | (result & ~mask);
78 ESP_LOGCONFIG(TAG,
"Setting up esphome/components/htu31d HTU31D...");
96 ESP_LOGD(TAG,
"Checking temperature and humidty values");
102 ESP_LOGE(TAG,
"Received errror writing conversion register");
111 ESP_LOGE(TAG,
"Error reading temperature/humidty register");
119 if (crc != thdata[2]) {
121 ESP_LOGE(TAG,
"Error validating temperature CRC");
126 temperature /= 65535.0f;
138 if (crc != thdata[5]) {
140 ESP_LOGE(TAG,
"Error validating humidty CRC");
144 float humidity = raw_hum;
145 humidity /= 65535.0f;
152 ESP_LOGD(TAG,
"Got Temperature=%.1f°C Humidity=%.1f%%", temperature, humidity);
161 ESP_LOGCONFIG(TAG,
"HTU31D:");
162 LOG_I2C_DEVICE(
this);
164 ESP_LOGE(TAG,
"Communication with HTU31D failed!");
166 LOG_UPDATE_INTERVAL(
this);
168 LOG_SENSOR(
" ",
"Humidity", this->
humidity_);
197 ESP_LOGE(TAG,
"Error reading device serial");
201 serial =
encode_uint32(reply[0], reply[1], reply[2], padding);
204 if (crc != reply[3]) {
205 ESP_LOGE(TAG,
"Error validating serial CRC");
209 ESP_LOGD(TAG,
"Found serial: 0x%" PRIX32, serial);
222 uint8_t heater_enabled_position = 0;
223 uint8_t mask = 1 << heater_enabled_position;
224 uint8_t diagnostics = 0;
227 ESP_LOGE(TAG,
"Error reading device serial");
231 diagnostics = reply[0];
232 return (diagnostics & mask) != 0;
244 if (current == desired) {
259 ESP_LOGE(TAG,
"Received error updating heater state");
const float DATA
For components that import data from directly connected sensors like DHT.
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
void status_set_warning(const char *message="unspecified")
void setup() override
Resets the sensor and ensures that the devices serial number can be read over I2C.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
bool reset_()
Sends a 'reset' request to the HTU31D, followed by a 15ms delay.
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.
sensor::Sensor * humidity_
No error found during execution of method.
float get_setup_priority() const override
Sets the startup priority for this component.
void dump_config() override
Update the sensor values (temperature+humidity).
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
uint32_t read_serial_num_()
Reads the serial number from the device and checks the CRC.
uint8_t compute_crc(uint32_t value)
Computes a CRC result for the provided input.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
bool is_heater_enabled()
Checks the diagnostics register to determine if the heater is currently enabled.
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
void set_heater_state(bool desired)
Sets the heater state on or off.
void update() override
Setup (reset) the sensor and check connection.
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
sensor::Sensor * temperature_
void IRAM_ATTR HOT delay(uint32_t ms)