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