ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
hdc2010.cpp
Go to the documentation of this file.
1#include "esphome/core/hal.h"
3#include "hdc2010.h"
4// https://github.com/vigsterkr/homebridge-hdc2010/blob/main/src/hdc2010.js
5// https://github.com/lime-labs/HDC2080-Arduino/blob/master/src/HDC2080.cpp
6
7namespace esphome::hdc2010 {
8
9ESPHOME_LOG_TAG(TAG, "hdc2010");
10
11// Register addresses
12static constexpr uint8_t REG_TEMPERATURE_LOW = 0x00;
13static constexpr uint8_t REG_TEMPERATURE_HIGH = 0x01;
14static constexpr uint8_t REG_HUMIDITY_LOW = 0x02;
15static constexpr uint8_t REG_HUMIDITY_HIGH = 0x03;
16static constexpr uint8_t REG_RESET_DRDY_INT_CONF = 0x0E;
17static constexpr uint8_t REG_MEASUREMENT_CONF = 0x0F;
18
19// REG_MEASUREMENT_CONF (0x0F) bit masks
20static constexpr uint8_t MEAS_TRIG = 0x01; // Bit 0: measurement trigger
21static constexpr uint8_t MEAS_CONF_MASK = 0x06; // Bits 2:1: measurement mode
22static constexpr uint8_t HRES_MASK = 0x30; // Bits 5:4: humidity resolution
23static constexpr uint8_t TRES_MASK = 0xC0; // Bits 7:6: temperature resolution
24
25// REG_RESET_DRDY_INT_CONF (0x0E) bit masks
26static constexpr uint8_t AMM_MASK = 0x70; // Bits 6:4: auto measurement mode
27
29 ESP_LOGCONFIG(TAG, "Running setup");
30
31 // Set 14-bit resolution for both sensors and measurement mode to temp + humidity
32 uint8_t config_contents;
33 this->read_register(REG_MEASUREMENT_CONF, &config_contents, 1);
34 config_contents &= ~(TRES_MASK | HRES_MASK | MEAS_CONF_MASK); // 14-bit temp, 14-bit humidity, temp+humidity mode
35 this->write_bytes(REG_MEASUREMENT_CONF, &config_contents, 1);
36
37 // Set auto measurement rate to manual (on-demand via MEAS_TRIG)
38 this->read_register(REG_RESET_DRDY_INT_CONF, &config_contents, 1);
39 config_contents &= ~AMM_MASK;
40 this->write_bytes(REG_RESET_DRDY_INT_CONF, &config_contents, 1);
41}
42
43void HDC2010Component::dump_config() {
44 ESP_LOGCONFIG(TAG, "HDC2010:");
45 LOG_I2C_DEVICE(this);
46 LOG_UPDATE_INTERVAL(this);
47 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
48 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
49}
50
51void HDC2010Component::update() {
52 // Trigger measurement
53 uint8_t config_contents;
54 this->read_register(REG_MEASUREMENT_CONF, &config_contents, 1);
55 config_contents |= MEAS_TRIG;
56 this->write_bytes(REG_MEASUREMENT_CONF, &config_contents, 1);
57
58 // 1ms delay after triggering the sample
59 set_timeout(1, [this]() {
60 if (this->temperature_sensor_ != nullptr) {
61 float temp = this->read_temp();
62 this->temperature_sensor_->publish_state(temp);
63 ESP_LOGD(TAG, "Temp=%.1f°C", temp);
64 }
65
66 if (this->humidity_sensor_ != nullptr) {
67 float humidity = this->read_humidity();
68 this->humidity_sensor_->publish_state(humidity);
69 ESP_LOGD(TAG, "Humidity=%.1f%%", humidity);
70 }
71 });
72}
73
74float HDC2010Component::read_temp() {
75 uint8_t byte[2];
76
77 this->read_register(REG_TEMPERATURE_LOW, &byte[0], 1);
78 this->read_register(REG_TEMPERATURE_HIGH, &byte[1], 1);
79
80 uint16_t temp = encode_value_le<uint16_t>(byte);
81 return (float) temp * 0.0025177f - 40.0f;
82}
83
84float HDC2010Component::read_humidity() {
85 uint8_t byte[2];
86
87 this->read_register(REG_HUMIDITY_LOW, &byte[0], 1);
88 this->read_register(REG_HUMIDITY_HIGH, &byte[1], 1);
89
90 uint16_t humidity = encode_value_le<uint16_t>(byte);
91 return (float) humidity * 0.001525879f;
92}
93
94} // namespace esphome::hdc2010
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
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
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
ESPHOME_LOG_TAG(TAG, "hdc2010")
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1084