19 static const char *
const TAG =
"htu31d";
22 static const uint8_t HTU31D_DEFAULT_I2CADDR = 0x40;
25 static const uint8_t HTU31D_READTEMPHUM = 0x00;
28 static const uint8_t HTU31D_CONVERSION = 0x40;
31 static const uint8_t HTU31D_READSERIAL = 0x0A;
34 static const uint8_t HTU31D_HEATERON = 0x04;
37 static const uint8_t HTU31D_HEATEROFF = 0x02;
40 static const uint8_t HTU31D_RESET = 0x1E;
43 static const uint8_t HTU31D_DIAGNOSTICS = 0x08;
51 uint32_t polynom = 0x98800000;
52 uint32_t msb = 0x80000000;
53 uint32_t mask = 0xFF800000;
54 uint32_t threshold = 0x00000080;
55 uint32_t result = value;
57 while (msb != threshold) {
60 result = ((result ^ polynom) & mask) | (result & ~mask);
76 ESP_LOGCONFIG(TAG,
"Setting up esphome/components/htu31d HTU31D...");
94 ESP_LOGD(TAG,
"Checking temperature and humidty values");
100 ESP_LOGE(TAG,
"Received errror writing conversion register");
109 ESP_LOGE(TAG,
"Error reading temperature/humidty register");
117 if (crc != thdata[2]) {
119 ESP_LOGE(TAG,
"Error validating temperature CRC");
124 temperature /= 65535.0f;
136 if (crc != thdata[5]) {
138 ESP_LOGE(TAG,
"Error validating humidty CRC");
142 float humidity = raw_hum;
143 humidity /= 65535.0f;
150 ESP_LOGD(TAG,
"Got Temperature=%.1f°C Humidity=%.1f%%", temperature, humidity);
159 ESP_LOGCONFIG(TAG,
"HTU31D:");
160 LOG_I2C_DEVICE(
this);
162 ESP_LOGE(TAG,
"Communication with HTU31D failed!");
164 LOG_UPDATE_INTERVAL(
this);
166 LOG_SENSOR(
" ",
"Humidity", this->
humidity_);
195 ESP_LOGE(TAG,
"Error reading device serial");
199 serial =
encode_uint32(reply[0], reply[1], reply[2], padding);
202 if (crc != reply[3]) {
203 ESP_LOGE(TAG,
"Error validating serial CRC");
207 ESP_LOGD(TAG,
"Found serial: 0x%X", serial);
220 uint8_t heater_enabled_position = 0;
221 uint8_t mask = 1 << heater_enabled_position;
222 uint8_t diagnostics = 0;
225 ESP_LOGE(TAG,
"Error reading device serial");
229 diagnostics = reply[0];
230 return (diagnostics & mask) != 0;
242 if (current == desired) {
257 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.
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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)