ESPHome 2026.11.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
7ESPHOME_LOG_TAG(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 LOG_UPDATE_INTERVAL(this);
28 LOG_SENSOR(" ", "Temperature", this->temperature_);
29 LOG_SENSOR(" ", "Humidity", this->humidity_);
30}
31
33 // regardless of what sensor/s are defined in yaml configuration
34 // the hdc1080 setup configuration used, requires both temperature and humidity to be read
35
37
38 if (this->write(&HDC1080_CMD_TEMPERATURE, 1) != i2c::ERROR_OK) {
39 this->status_set_warning();
40 return;
41 }
42
43 this->set_timeout(20, [this]() {
44 uint16_t raw_temperature;
45 if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) {
46 this->status_set_warning();
47 return;
48 }
49
50 if (this->temperature_ != nullptr) {
51 raw_temperature = i2c::i2ctohs(raw_temperature);
52 float temperature = raw_temperature * 0.0025177f - 40.0f; // raw * 2^-16 * 165 - 40
53 this->temperature_->publish_state(temperature);
54 }
55
56 if (this->write(&HDC1080_CMD_HUMIDITY, 1) != i2c::ERROR_OK) {
57 this->status_set_warning();
58 return;
59 }
60
61 this->set_timeout(20, [this]() {
62 uint16_t raw_humidity;
63 if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
64 this->status_set_warning();
65 return;
66 }
67
68 if (this->humidity_ != nullptr) {
69 raw_humidity = i2c::i2ctohs(raw_humidity);
70 float humidity = raw_humidity * 0.001525879f; // raw * 2^-16 * 100
71 this->humidity_->publish_state(humidity);
72 }
73 });
74 });
75}
76
77} // namespace esphome::hdc1080
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
void status_clear_warning()
Definition component.h:296
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:37
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
ESPHOME_LOG_TAG(TAG, "hdc1080")
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:139
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
uint16_t temperature
Definition sun_gtil2.cpp:12