ESPHome 2025.5.0
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 ESP_LOGCONFIG(TAG, "Setting up HDC1080...");
17
18 const uint8_t data[2] = {
19 0b00000000, // resolution 14bit for both humidity and temperature
20 0b00000000 // reserved
21 };
22
23 if (!this->write_bytes(HDC1080_CMD_CONFIGURATION, data, 2)) {
24 // as instruction is same as powerup defaults (for now), interpret as warning if this fails
25 ESP_LOGW(TAG, "HDC1080 initial config instruction error");
26 this->status_set_warning();
27 return;
28 }
29}
31 ESP_LOGCONFIG(TAG, "HDC1080:");
32 LOG_I2C_DEVICE(this);
33 if (this->is_failed()) {
34 ESP_LOGE(TAG, "Communication with HDC1080 failed!");
35 }
36 LOG_UPDATE_INTERVAL(this);
37 LOG_SENSOR(" ", "Temperature", this->temperature_);
38 LOG_SENSOR(" ", "Humidity", this->humidity_);
39}
41 uint16_t raw_temp;
42 if (this->write(&HDC1080_CMD_TEMPERATURE, 1) != i2c::ERROR_OK) {
43 this->status_set_warning();
44 return;
45 }
46 delay(20);
47 if (this->read(reinterpret_cast<uint8_t *>(&raw_temp), 2) != i2c::ERROR_OK) {
48 this->status_set_warning();
49 return;
50 }
51 raw_temp = i2c::i2ctohs(raw_temp);
52 float temp = raw_temp * 0.0025177f - 40.0f; // raw * 2^-16 * 165 - 40
53 this->temperature_->publish_state(temp);
54
55 uint16_t raw_humidity;
56 if (this->write(&HDC1080_CMD_HUMIDITY, 1) != i2c::ERROR_OK) {
57 this->status_set_warning();
58 return;
59 }
60 delay(20);
61 if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
62 this->status_set_warning();
63 return;
64 }
65 raw_humidity = i2c::i2ctohs(raw_humidity);
66 float humidity = raw_humidity * 0.001525879f; // raw * 2^-16 * 100
67 this->humidity_->publish_state(humidity);
68
69 ESP_LOGD(TAG, "Got temperature=%.1f°C humidity=%.1f%%", temp, humidity);
71}
73
74} // namespace hdc1080
75} // namespace esphome
bool is_failed() const
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
void update() override
Retrieve the latest sensor values. This operation takes approximately 16ms.
Definition hdc1080.cpp:40
sensor::Sensor * temperature_
Definition hdc1080.h:24
float get_setup_priority() const override
Definition hdc1080.cpp:72
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:39
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:19
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:28