ESPHome 2026.11.0b6
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"
8
10
11ESPHOME_LOG_TAG(TAG, "honeywell_hih.i2c");
12
13static constexpr uint32_t MEASUREMENT_TIMEOUT_ID = 0;
14
15static const uint8_t REQUEST_CMD[1] = {0x00}; // Measurement Request Format
16static const uint16_t MAX_COUNT = 0x3FFE; // 2^14 - 2
17
18void HoneywellHIComponent::read_sensor_data_() {
19 uint8_t data[4];
20
21 if (this->read(data, sizeof(data)) != i2c::ERROR_OK) {
22 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
23 return;
24 }
25
26 const uint16_t raw_humidity = encode_uint16(data[0] & 0x3F, data[1]);
27 float humidity = (static_cast<float>(raw_humidity) / MAX_COUNT) * 100;
28
29 const uint16_t raw_temperature = encode_uint16(data[2], data[3]) >> 2;
30 float temperature = (static_cast<float>(raw_temperature) / MAX_COUNT) * 165 - 40;
31
32 ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity);
33 PUBLISH_STATE(this->temperature_sensor_, temperature);
34 PUBLISH_STATE(this->humidity_sensor_, humidity);
35}
36
38 if (this->write(REQUEST_CMD, sizeof(REQUEST_CMD)) != i2c::ERROR_OK) {
39 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
40 return;
41 }
42
43 this->measurement_running_ = true;
44}
45
47 uint8_t data[1];
48
49 if (this->read(data, sizeof(data)) != i2c::ERROR_OK) {
50 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
51 return false;
52 }
53
54 // Check status bits
55 return ((data[0] & 0xC0) == 0x00);
56}
57
59 this->measurement_running_ = false;
60 this->mark_failed(LOG_STR("Measurement timeout"));
61}
62
64 ESP_LOGV(TAG, "Update Honeywell HIH Sensor");
65
66 this->start_measurement_();
67 // The measurement cycle duration is typically 36.65 ms for temperature and humidity readings.
68 this->set_timeout(MEASUREMENT_TIMEOUT_ID, 100, [this] { this->measurement_timeout_(); });
69}
70
72 if (this->measurement_running_ && this->is_measurement_ready_()) {
73 this->measurement_running_ = false;
74 this->cancel_timeout(MEASUREMENT_TIMEOUT_ID);
75 this->read_sensor_data_();
76 }
77}
78
80 ESP_LOGD(TAG, "Honeywell HIH:");
81 LOG_I2C_DEVICE(this);
82 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
83 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
84 LOG_UPDATE_INTERVAL(this);
85}
86
87} // 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
SUB_SENSOR(temperature) SUB_SENSOR(humidity) private void start_measurement_()
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
ESPHOME_LOG_TAG(TAG, "honeywell_hih.i2c")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12