ESPHome 2026.4.0-dev
Loading...
Searching...
No Matches
hdc2010.cpp
Go to the documentation of this file.
1#include "esphome/core/hal.h"
2#include "hdc2010.h"
3// https://github.com/vigsterkr/homebridge-hdc2010/blob/main/src/hdc2010.js
4// https://github.com/lime-labs/HDC2080-Arduino/blob/master/src/HDC2080.cpp
5namespace esphome {
6namespace hdc2010 {
7
8static const char *const TAG = "hdc2010";
9
10// Register addresses
11static constexpr uint8_t REG_TEMPERATURE_LOW = 0x00;
12static constexpr uint8_t REG_TEMPERATURE_HIGH = 0x01;
13static constexpr uint8_t REG_HUMIDITY_LOW = 0x02;
14static constexpr uint8_t REG_HUMIDITY_HIGH = 0x03;
15static constexpr uint8_t REG_RESET_DRDY_INT_CONF = 0x0E;
16static constexpr uint8_t REG_MEASUREMENT_CONF = 0x0F;
17
18// REG_MEASUREMENT_CONF (0x0F) bit masks
19static constexpr uint8_t MEAS_TRIG = 0x01; // Bit 0: measurement trigger
20static constexpr uint8_t MEAS_CONF_MASK = 0x06; // Bits 2:1: measurement mode
21static constexpr uint8_t HRES_MASK = 0x30; // Bits 5:4: humidity resolution
22static constexpr uint8_t TRES_MASK = 0xC0; // Bits 7:6: temperature resolution
23
24// REG_RESET_DRDY_INT_CONF (0x0E) bit masks
25static constexpr uint8_t AMM_MASK = 0x70; // Bits 6:4: auto measurement mode
26
28 ESP_LOGCONFIG(TAG, "Running setup");
29
30 // Set 14-bit resolution for both sensors and measurement mode to temp + humidity
31 uint8_t config_contents;
32 this->read_register(REG_MEASUREMENT_CONF, &config_contents, 1);
33 config_contents &= ~(TRES_MASK | HRES_MASK | MEAS_CONF_MASK); // 14-bit temp, 14-bit humidity, temp+humidity mode
34 this->write_bytes(REG_MEASUREMENT_CONF, &config_contents, 1);
35
36 // Set auto measurement rate to manual (on-demand via MEAS_TRIG)
37 this->read_register(REG_RESET_DRDY_INT_CONF, &config_contents, 1);
38 config_contents &= ~AMM_MASK;
39 this->write_bytes(REG_RESET_DRDY_INT_CONF, &config_contents, 1);
40}
41
43 ESP_LOGCONFIG(TAG, "HDC2010:");
44 LOG_I2C_DEVICE(this);
45 if (this->is_failed()) {
46 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
47 }
48 LOG_UPDATE_INTERVAL(this);
49 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
50 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
51}
52
54 // Trigger measurement
55 uint8_t config_contents;
56 this->read_register(REG_MEASUREMENT_CONF, &config_contents, 1);
57 config_contents |= MEAS_TRIG;
58 this->write_bytes(REG_MEASUREMENT_CONF, &config_contents, 1);
59
60 // 1ms delay after triggering the sample
61 set_timeout(1, [this]() {
62 if (this->temperature_sensor_ != nullptr) {
63 float temp = this->read_temp();
65 ESP_LOGD(TAG, "Temp=%.1f°C", temp);
66 }
67
68 if (this->humidity_sensor_ != nullptr) {
69 float humidity = this->read_humidity();
70 this->humidity_sensor_->publish_state(humidity);
71 ESP_LOGD(TAG, "Humidity=%.1f%%", humidity);
72 }
73 });
74}
75
77 uint8_t byte[2];
78
79 this->read_register(REG_TEMPERATURE_LOW, &byte[0], 1);
80 this->read_register(REG_TEMPERATURE_HIGH, &byte[1], 1);
81
82 uint16_t temp = encode_uint16(byte[1], byte[0]);
83 return (float) temp * 0.0025177f - 40.0f;
84}
85
87 uint8_t byte[2];
88
89 this->read_register(REG_HUMIDITY_LOW, &byte[0], 1);
90 this->read_register(REG_HUMIDITY_HIGH, &byte[1], 1);
91
92 uint16_t humidity = encode_uint16(byte[1], byte[0]);
93 return (float) humidity * 0.001525879f;
94}
95
96} // namespace hdc2010
97} // namespace esphome
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 update() override
Retrieve the latest sensor values. This operation takes approximately 16ms.
Definition hdc2010.cpp:53
sensor::Sensor * temperature_sensor_
Definition hdc2010.h:27
void setup() override
Setup the sensor and check for connection.
Definition hdc2010.cpp:27
sensor::Sensor * humidity_sensor_
Definition hdc2010.h:28
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
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:251
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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