ESPHome 2026.11.0b6
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
24void HDC1080Component::dump_config() {
25 ESP_LOGCONFIG(TAG, "HDC1080:");
26 LOG_I2C_DEVICE(this);
27 LOG_UPDATE_INTERVAL(this);
28 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
29 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
30}
31
32void HDC1080Component::update() {
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 // raw * 2^-16 * 165 - 40
51 PUBLISH_STATE(this->temperature_sensor_, i2c::i2ctohs(raw_temperature) * 0.0025177f - 40.0f);
52
53 if (this->write(&HDC1080_CMD_HUMIDITY, 1) != i2c::ERROR_OK) {
54 this->status_set_warning();
55 return;
56 }
57
58 this->set_timeout(20, [this]() {
59 uint16_t raw_humidity;
60 if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
61 this->status_set_warning();
62 return;
63 }
64
65 // raw * 2^-16 * 100
66 PUBLISH_STATE(this->humidity_sensor_, i2c::i2ctohs(raw_humidity) * 0.001525879f);
67 });
68 });
69}
70
71} // namespace esphome::hdc1080
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
void status_clear_warning()
Definition component.h:309
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:54
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
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