ESPHome 2026.11.0-dev
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
10ESPHOME_LOG_TAG(TAG, "honeywell_hih.i2c");
11
12static constexpr uint32_t MEASUREMENT_TIMEOUT_ID = 0;
13
14static const uint8_t REQUEST_CMD[1] = {0x00}; // Measurement Request Format
15static const uint16_t MAX_COUNT = 0x3FFE; // 2^14 - 2
16
17void HoneywellHIComponent::read_sensor_data_() {
18 uint8_t data[4];
19
20 if (this->read(data, sizeof(data)) != i2c::ERROR_OK) {
21 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
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
38void HoneywellHIComponent::start_measurement_() {
39 if (this->write(REQUEST_CMD, sizeof(REQUEST_CMD)) != i2c::ERROR_OK) {
40 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
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 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
52 return false;
53 }
54
55 // Check status bits
56 return ((data[0] & 0xC0) == 0x00);
57}
58
59void HoneywellHIComponent::measurement_timeout_() {
60 this->measurement_running_ = false;
61 this->mark_failed(LOG_STR("Measurement timeout"));
62}
63
65 ESP_LOGV(TAG, "Update Honeywell HIH Sensor");
66
67 this->start_measurement_();
68 // The measurement cycle duration is typically 36.65 ms for temperature and humidity readings.
69 this->set_timeout(MEASUREMENT_TIMEOUT_ID, 100, [this] { this->measurement_timeout_(); });
70}
71
73 if (this->measurement_running_ && this->is_measurement_ready_()) {
74 this->measurement_running_ = false;
75 this->cancel_timeout(MEASUREMENT_TIMEOUT_ID);
76 this->read_sensor_data_();
77 }
78}
79
81 ESP_LOGD(TAG, "Honeywell HIH:");
82 LOG_I2C_DEVICE(this);
83 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
84 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
85 LOG_UPDATE_INTERVAL(this);
86}
87
88} // namespace esphome::honeywell_hih_i2c
void mark_failed()
Mark this component as failed.
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
ESPHOME_LOG_TAG(TAG, "honeywell_hih.i2c")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
std::span< const uint8_t > data
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12