ESPHome 2025.9.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 {
6namespace hdc1080 {
7
8static const char *const TAG = "hdc1080";
9
10static const uint8_t HDC1080_ADDRESS = 0x40; // 0b1000000 from datasheet
11static const uint8_t HDC1080_CMD_CONFIGURATION = 0x02;
12static const uint8_t HDC1080_CMD_TEMPERATURE = 0x00;
13static const uint8_t HDC1080_CMD_HUMIDITY = 0x01;
14
16 const uint8_t data[2] = {
17 0b00000000, // resolution 14bit for both humidity and temperature
18 0b00000000 // reserved
19 };
20
21 if (!this->write_bytes(HDC1080_CMD_CONFIGURATION, data, 2)) {
22 // as instruction is same as powerup defaults (for now), interpret as warning if this fails
23 ESP_LOGW(TAG, "HDC1080 initial config instruction error");
24 this->status_set_warning();
25 return;
26 }
27}
29 ESP_LOGCONFIG(TAG, "HDC1080:");
30 LOG_I2C_DEVICE(this);
31 if (this->is_failed()) {
32 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
33 }
34 LOG_UPDATE_INTERVAL(this);
35 LOG_SENSOR(" ", "Temperature", this->temperature_);
36 LOG_SENSOR(" ", "Humidity", this->humidity_);
37}
39 uint16_t raw_temp;
40 if (this->write(&HDC1080_CMD_TEMPERATURE, 1) != i2c::ERROR_OK) {
41 this->status_set_warning();
42 return;
43 }
44 delay(20);
45 if (this->read(reinterpret_cast<uint8_t *>(&raw_temp), 2) != i2c::ERROR_OK) {
46 this->status_set_warning();
47 return;
48 }
49 raw_temp = i2c::i2ctohs(raw_temp);
50 float temp = raw_temp * 0.0025177f - 40.0f; // raw * 2^-16 * 165 - 40
51 this->temperature_->publish_state(temp);
52
53 uint16_t raw_humidity;
54 if (this->write(&HDC1080_CMD_HUMIDITY, 1) != i2c::ERROR_OK) {
55 this->status_set_warning();
56 return;
57 }
58 delay(20);
59 if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
60 this->status_set_warning();
61 return;
62 }
63 raw_humidity = i2c::i2ctohs(raw_humidity);
64 float humidity = raw_humidity * 0.001525879f; // raw * 2^-16 * 100
65 this->humidity_->publish_state(humidity);
66
67 ESP_LOGD(TAG, "Got temperature=%.1f°C humidity=%.1f%%", temp, humidity);
69}
71
72} // namespace hdc1080
73} // namespace esphome
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
void update() override
Retrieve the latest sensor values. This operation takes approximately 16ms.
Definition hdc1080.cpp:38
sensor::Sensor * temperature_
Definition hdc1080.h:24
float get_setup_priority() const override
Definition hdc1080.cpp:70
void setup() override
Setup the sensor and check for connection.
Definition hdc1080.cpp:15
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition i2c.h:252
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
ErrorCode read(uint8_t *data, size_t len)
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:45
u_int8_t raw_temp
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:128
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29