ESPHome  2022.12.8
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 
5 namespace esphome {
6 namespace hdc1080 {
7 
8 static const char *const TAG = "hdc1080";
9 
10 static const uint8_t HDC1080_ADDRESS = 0x40; // 0b1000000 from datasheet
11 static const uint8_t HDC1080_CMD_CONFIGURATION = 0x02;
12 static const uint8_t HDC1080_CMD_TEMPERATURE = 0x00;
13 static 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);
70  this->status_clear_warning();
71 }
73 
74 } // namespace hdc1080
75 } // namespace esphome
void update() override
Retrieve the latest sensor values. This operation takes approximately 16ms.
Definition: hdc1080.cpp:40
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
uint16_t i2ctohs(uint16_t i2cshort)
Definition: i2c.h:36
sensor::Sensor * temperature_
Definition: hdc1080.h:24
sensor::Sensor * humidity_
Definition: hdc1080.h:25
void setup() override
Setup the sensor and check for connection.
Definition: hdc1080.cpp:15
ErrorCode read(uint8_t *data, size_t len)
Definition: i2c.h:48
void status_clear_warning()
Definition: component.cpp:149
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:72
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:56
void status_set_warning()
Definition: component.cpp:141
float get_setup_priority() const override
Definition: hdc1080.cpp:72
Definition: a4988.cpp:4
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:109