ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
hdc1080.cpp
Go to the documentation of this file.
1#include "hdc1080.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome::hdc1080 {
6
7static const char *const TAG = "hdc1080";
8
9static const uint8_t HDC1080_CMD_CONFIGURATION = 0x02;
10static const uint8_t HDC1080_CMD_TEMPERATURE = 0x00;
11static const uint8_t HDC1080_CMD_HUMIDITY = 0x01;
12
14 const uint8_t config[2] = {0x00, 0x00}; // resolution 14bit for both humidity and temperature
15
16 // if configuration fails - there is a problem
17 if (this->write_register(HDC1080_CMD_CONFIGURATION, config, 2) != i2c::ERROR_OK) {
18 ESP_LOGW(TAG, "Failed to configure HDC1080");
19 this->status_set_warning();
20 return;
21 }
22}
23
25 ESP_LOGCONFIG(TAG, "HDC1080:");
26 LOG_I2C_DEVICE(this);
27 if (this->is_failed()) {
28 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
29 }
30 LOG_UPDATE_INTERVAL(this);
31 LOG_SENSOR(" ", "Temperature", this->temperature_);
32 LOG_SENSOR(" ", "Humidity", this->humidity_);
33}
34
36 // regardless of what sensor/s are defined in yaml configuration
37 // the hdc1080 setup configuration used, requires both temperature and humidity to be read
38
40
41 if (this->write(&HDC1080_CMD_TEMPERATURE, 1) != i2c::ERROR_OK) {
42 this->status_set_warning();
43 return;
44 }
45
46 this->set_timeout(20, [this]() {
47 uint16_t raw_temperature;
48 if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) {
49 this->status_set_warning();
50 return;
51 }
52
53 if (this->temperature_ != nullptr) {
54 raw_temperature = i2c::i2ctohs(raw_temperature);
55 float temperature = raw_temperature * 0.0025177f - 40.0f; // raw * 2^-16 * 165 - 40
56 this->temperature_->publish_state(temperature);
57 }
58
59 if (this->write(&HDC1080_CMD_HUMIDITY, 1) != i2c::ERROR_OK) {
60 this->status_set_warning();
61 return;
62 }
63
64 this->set_timeout(20, [this]() {
65 uint16_t raw_humidity;
66 if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
67 this->status_set_warning();
68 return;
69 }
70
71 if (this->humidity_ != nullptr) {
72 raw_humidity = i2c::i2ctohs(raw_humidity);
73 float humidity = raw_humidity * 0.001525879f; // raw * 2^-16 * 100
74 this->humidity_->publish_state(humidity);
75 }
76 });
77 });
78}
79
80} // namespace esphome::hdc1080
bool is_failed() const
Definition component.h:272
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:493
void status_clear_warning()
Definition component.h:289
sensor::Sensor * temperature_
Definition hdc1080.h:19
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 write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:127
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
uint16_t temperature
Definition sun_gtil2.cpp:12