ESPHome 2026.4.0-dev
Loading...
Searching...
No Matches
hdc2080.cpp
Go to the documentation of this file.
1#include "hdc2080.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5namespace esphome::hdc2080 {
6
7static const char *const TAG = "hdc2080";
8
9// Register map (Table 8-6)
10static constexpr uint8_t REG_TEMPERATURE_LOW = 0x00; // Temperature [7:0]
11static constexpr uint8_t REG_TEMPERATURE_HIGH = 0x01; // Temperature [15:8]
12static constexpr uint8_t REG_HUMIDITY_LOW = 0x02; // Humidity [7:0]
13static constexpr uint8_t REG_HUMIDITY_HIGH = 0x03; // Humidity [15:8]
14static constexpr uint8_t REG_RESET_DRDY_INT_CONF = 0x0E; // Soft Reset and Interrupt Configuration
15static constexpr uint8_t REG_MEASUREMENT_CONFIGURATION = 0x0F;
16
17// Measurement register (0x0F) bit fields
18static constexpr uint8_t MEAS_TRIG = 0x01; // Bit 0: start measurement
19static constexpr uint8_t MEAS_CONF_TEMP = 0x02; // Bits 2:1 = 01: temperature only
20static constexpr uint8_t MEAS_CONF_HUM = 0x04; // Bits 2:1 = 10: humidity only
21
23 const uint8_t data = 0x00; // automatic measurement mode disabled, heater off
24 if (this->write_register(REG_RESET_DRDY_INT_CONF, &data, 1) != i2c::ERROR_OK) {
25 this->mark_failed(ESP_LOG_MSG_COMM_FAIL);
26 return;
27 }
28}
29
31 ESP_LOGCONFIG(TAG, "HDC2080:");
32 LOG_I2C_DEVICE(this);
33 LOG_UPDATE_INTERVAL(this);
34 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
35 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
36 if (this->is_failed()) {
37 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
38 }
39}
40
42 uint8_t data = MEAS_TRIG; // 14-bit resolution, measure both, start
43 if (this->temperature_sensor_ != nullptr && this->humidity_sensor_ == nullptr) {
44 data = MEAS_TRIG | MEAS_CONF_TEMP;
45 } else if (this->temperature_sensor_ == nullptr && this->humidity_sensor_ != nullptr) {
46 data = MEAS_TRIG | MEAS_CONF_HUM;
47 }
48 if (this->write_register(REG_MEASUREMENT_CONFIGURATION, &data, 1) != i2c::ERROR_OK) {
49 this->status_set_warning(ESP_LOG_MSG_COMM_FAIL);
50 return;
51 }
52 // wait for conversion to complete 2ms should be enough, more is fine
53 this->set_timeout(5, [this]() {
54 uint8_t raw_data[4];
55 if (this->read_register(REG_TEMPERATURE_LOW, raw_data, 4) != i2c::ERROR_OK) {
56 this->status_set_warning(ESP_LOG_MSG_COMM_FAIL);
57 return;
58 }
60 if (this->temperature_sensor_ != nullptr) {
61 float temp = encode_uint16(raw_data[1], raw_data[0]) * (165.0f / 65536.0f) - 40.5f;
63 }
64 if (this->humidity_sensor_ != nullptr) {
65 float humidity = encode_uint16(raw_data[3], raw_data[2]) * (100.0f / 65536.0f);
66 this->humidity_sensor_->publish_state(humidity);
67 }
68 });
69}
70
71} // namespace esphome::hdc2080
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:271
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:497
void status_clear_warning()
Definition component.h:293
sensor::Sensor * temperature_sensor_
Definition hdc2080.h:20
void setup() override
Setup the sensor and check for connection.
Definition hdc2080.cpp:22
sensor::Sensor * humidity_sensor_
Definition hdc2080.h:21
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:34
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:25
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
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:881