ESPHome 2026.5.1
Loading...
Searching...
No Matches
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
9
10static const char *const TAG = "honeywell_hih.i2c";
11
12static const uint8_t REQUEST_CMD[1] = {0x00}; // Measurement Request Format
13static const uint16_t MAX_COUNT = 0x3FFE; // 2^14 - 2
14
15void HoneywellHIComponent::read_sensor_data_() {
16 uint8_t data[4];
17
18 if (this->read(data, sizeof(data)) != i2c::ERROR_OK) {
19 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
20 this->mark_failed();
21 return;
22 }
23
24 const uint16_t raw_humidity = (static_cast<uint16_t>(data[0] & 0x3F) << 8) | data[1];
25 float humidity = (static_cast<float>(raw_humidity) / MAX_COUNT) * 100;
26
27 const uint16_t raw_temperature = (static_cast<uint16_t>(data[2]) << 6) | (data[3] >> 2);
28 float temperature = (static_cast<float>(raw_temperature) / MAX_COUNT) * 165 - 40;
29
30 ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity);
31 if (this->temperature_sensor_ != nullptr)
32 this->temperature_sensor_->publish_state(temperature);
33 if (this->humidity_sensor_ != nullptr)
34 this->humidity_sensor_->publish_state(humidity);
35}
36
37void HoneywellHIComponent::start_measurement_() {
38 if (this->write(REQUEST_CMD, sizeof(REQUEST_CMD)) != i2c::ERROR_OK) {
39 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
40 this->mark_failed();
41 return;
42 }
43
44 this->measurement_running_ = true;
45}
46
47bool HoneywellHIComponent::is_measurement_ready_() {
48 uint8_t data[1];
49
50 if (this->read(data, sizeof(data)) != i2c::ERROR_OK) {
51 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
52 this->mark_failed();
53 return false;
54 }
55
56 // Check status bits
57 return ((data[0] & 0xC0) == 0x00);
58}
59
60void HoneywellHIComponent::measurement_timeout_() {
61 ESP_LOGE(TAG, "Honeywell HIH Timeout!");
62 this->measurement_running_ = false;
63 this->mark_failed();
64}
65
67 ESP_LOGV(TAG, "Update Honeywell HIH Sensor");
68
69 this->start_measurement_();
70 // The measurement cycle duration is typically 36.65 ms for temperature and humidity readings.
71 this->set_timeout("meas_timeout", 100, [this] { this->measurement_timeout_(); });
72}
73
75 if (this->measurement_running_ && this->is_measurement_ready_()) {
76 this->measurement_running_ = false;
77 this->cancel_timeout("meas_timeout");
78 this->read_sensor_data_();
79 }
80}
81
83 ESP_LOGD(TAG, "Honeywell HIH:");
84 LOG_I2C_DEVICE(this);
85 if (this->is_failed()) {
86 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
87 }
88 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
89 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
90 LOG_UPDATE_INTERVAL(this);
91}
92
93} // namespace esphome::honeywell_hih_i2c
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_timeout(const std boo cancel_timeout)(const char *name)
Cancel a timeout function.
Definition component.h:532
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
uint16_t temperature
Definition sun_gtil2.cpp:12