ESPHome  2024.11.0
honeywell_hih.cpp
Go to the documentation of this file.
1 // Honeywell HumidIcon I2C Sensors
2 // https://prod-edam.honeywell.com/content/dam/honeywell-edam/sps/siot/en-us/products/sensors/humidity-with-temperature-sensors/common/documents/sps-siot-i2c-comms-humidicon-tn-009061-2-en-ciid-142171.pdf
3 //
4 
5 #include "honeywell_hih.h"
6 #include "esphome/core/log.h"
7 
8 namespace esphome {
9 namespace honeywell_hih_i2c {
10 
11 static const char *const TAG = "honeywell_hih.i2c";
12 
13 static const uint8_t REQUEST_CMD[1] = {0x00}; // Measurement Request Format
14 static const uint16_t MAX_COUNT = 0x3FFE; // 2^14 - 2
15 
16 void HoneywellHIComponent::read_sensor_data_() {
17  uint8_t data[4];
18 
19  if (this->read(data, sizeof(data)) != i2c::ERROR_OK) {
20  ESP_LOGE(TAG, "Communication with Honeywell HIH failed!");
21  this->mark_failed();
22  return;
23  }
24 
25  const uint16_t raw_humidity = (static_cast<uint16_t>(data[0] & 0x3F) << 8) | data[1];
26  float humidity = (static_cast<float>(raw_humidity) / MAX_COUNT) * 100;
27 
28  const uint16_t raw_temperature = (static_cast<uint16_t>(data[2]) << 6) | (data[3] >> 2);
29  float temperature = (static_cast<float>(raw_temperature) / MAX_COUNT) * 165 - 40;
30 
31  ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity);
32  if (this->temperature_sensor_ != nullptr)
33  this->temperature_sensor_->publish_state(temperature);
34  if (this->humidity_sensor_ != nullptr)
35  this->humidity_sensor_->publish_state(humidity);
36 }
37 
38 void HoneywellHIComponent::start_measurement_() {
39  if (this->write(REQUEST_CMD, sizeof(REQUEST_CMD)) != i2c::ERROR_OK) {
40  ESP_LOGE(TAG, "Communication with Honeywell HIH failed!");
41  this->mark_failed();
42  return;
43  }
44 
45  this->measurement_running_ = true;
46 }
47 
48 bool HoneywellHIComponent::is_measurement_ready_() {
49  uint8_t data[1];
50 
51  if (this->read(data, sizeof(data)) != i2c::ERROR_OK) {
52  ESP_LOGE(TAG, "Communication with Honeywell HIH failed!");
53  this->mark_failed();
54  return false;
55  }
56 
57  // Check status bits
58  return ((data[0] & 0xC0) == 0x00);
59 }
60 
61 void HoneywellHIComponent::measurement_timeout_() {
62  ESP_LOGE(TAG, "Honeywell HIH Timeout!");
63  this->measurement_running_ = false;
64  this->mark_failed();
65 }
66 
68  ESP_LOGV(TAG, "Update Honeywell HIH Sensor");
69 
70  this->start_measurement_();
71  // The measurement cycle duration is typically 36.65 ms for temperature and humidity readings.
72  this->set_timeout("meas_timeout", 100, [this] { this->measurement_timeout_(); });
73 }
74 
76  if (this->measurement_running_ && this->is_measurement_ready_()) {
77  this->measurement_running_ = false;
78  this->cancel_timeout("meas_timeout");
79  this->read_sensor_data_();
80  }
81 }
82 
84  ESP_LOGD(TAG, "Honeywell HIH:");
85  LOG_I2C_DEVICE(this);
86  if (this->is_failed()) {
87  ESP_LOGE(TAG, "Communication with Honeywell HIH failed!");
88  }
89  LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
90  LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
91  LOG_UPDATE_INTERVAL(this);
92 }
93 
95 
96 } // namespace honeywell_hih_i2c
97 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
Definition: component.cpp:73
bool is_failed() const
Definition: component.cpp:143
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition: i2c.h:160
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:69
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:186
No error found during execution of method.
Definition: i2c_bus.h:13
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint16_t temperature
Definition: sun_gtil2.cpp:26
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7