ESPHome 2025.11.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
10static const uint8_t HDC2010_ADDRESS = 0x40; // 0b1000000 or 0b1000001 from datasheet
11static const uint8_t HDC2010_CMD_CONFIGURATION_MEASUREMENT = 0x8F;
12static const uint8_t HDC2010_CMD_START_MEASUREMENT = 0xF9;
13static const uint8_t HDC2010_CMD_TEMPERATURE_LOW = 0x00;
14static const uint8_t HDC2010_CMD_TEMPERATURE_HIGH = 0x01;
15static const uint8_t HDC2010_CMD_HUMIDITY_LOW = 0x02;
16static const uint8_t HDC2010_CMD_HUMIDITY_HIGH = 0x03;
17static const uint8_t CONFIG = 0x0E;
18static const uint8_t MEASUREMENT_CONFIG = 0x0F;
19
21 ESP_LOGCONFIG(TAG, "Running setup");
22
23 const uint8_t data[2] = {
24 0b00000000, // resolution 14bit for both humidity and temperature
25 0b00000000 // reserved
26 };
27
28 if (!this->write_bytes(HDC2010_CMD_CONFIGURATION_MEASUREMENT, data, 2)) {
29 ESP_LOGW(TAG, "Initial config instruction error");
30 this->status_set_warning();
31 return;
32 }
33
34 // Set measurement mode to temperature and humidity
35 uint8_t config_contents;
36 this->read_register(MEASUREMENT_CONFIG, &config_contents, 1);
37 config_contents = (config_contents & 0xF9); // Always set to TEMP_AND_HUMID mode
38 this->write_bytes(MEASUREMENT_CONFIG, &config_contents, 1);
39
40 // Set rate to manual
41 this->read_register(CONFIG, &config_contents, 1);
42 config_contents &= 0x8F;
43 this->write_bytes(CONFIG, &config_contents, 1);
44
45 // Set temperature resolution to 14bit
46 this->read_register(CONFIG, &config_contents, 1);
47 config_contents &= 0x3F;
48 this->write_bytes(CONFIG, &config_contents, 1);
49
50 // Set humidity resolution to 14bit
51 this->read_register(CONFIG, &config_contents, 1);
52 config_contents &= 0xCF;
53 this->write_bytes(CONFIG, &config_contents, 1);
54}
55
57 ESP_LOGCONFIG(TAG, "HDC2010:");
58 LOG_I2C_DEVICE(this);
59 if (this->is_failed()) {
60 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
61 }
62 LOG_UPDATE_INTERVAL(this);
63 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
64 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
65}
66
68 // Trigger measurement
69 uint8_t config_contents;
70 this->read_register(CONFIG, &config_contents, 1);
71 config_contents |= 0x01;
72 this->write_bytes(MEASUREMENT_CONFIG, &config_contents, 1);
73
74 // 1ms delay after triggering the sample
75 set_timeout(1, [this]() {
76 if (this->temperature_sensor_ != nullptr) {
77 float temp = this->read_temp();
79 ESP_LOGD(TAG, "Temp=%.1f°C", temp);
80 }
81
82 if (this->humidity_sensor_ != nullptr) {
83 float humidity = this->read_humidity();
84 this->humidity_sensor_->publish_state(humidity);
85 ESP_LOGD(TAG, "Humidity=%.1f%%", humidity);
86 }
87 });
88}
89
91 uint8_t byte[2];
92
93 this->read_register(HDC2010_CMD_TEMPERATURE_LOW, &byte[0], 1);
94 this->read_register(HDC2010_CMD_TEMPERATURE_HIGH, &byte[1], 1);
95
96 uint16_t temp = encode_uint16(byte[1], byte[0]);
97 return (float) temp * 0.0025177f - 40.0f;
98}
99
101 uint8_t byte[2];
102
103 this->read_register(HDC2010_CMD_HUMIDITY_LOW, &byte[0], 1);
104 this->read_register(HDC2010_CMD_HUMIDITY_HIGH, &byte[1], 1);
105
106 uint16_t humidity = encode_uint16(byte[1], byte[0]);
107 return (float) humidity * 0.001525879f;
108}
109
110} // namespace hdc2010
111} // namespace esphome
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void update() override
Retrieve the latest sensor values. This operation takes approximately 16ms.
Definition hdc2010.cpp:67
sensor::Sensor * temperature_sensor_
Definition hdc2010.h:27
void setup() override
Setup the sensor and check for connection.
Definition hdc2010.cpp:20
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:32
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:252
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:73
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:363