ESPHome 2025.5.0
Loading...
Searching...
No Matches
hyt271.cpp
Go to the documentation of this file.
1#include "hyt271.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome {
6namespace hyt271 {
7
8static const char *const TAG = "hyt271";
9
10static const uint8_t HYT271_ADDRESS = 0x28;
11
13 ESP_LOGCONFIG(TAG, "HYT271:");
14 LOG_I2C_DEVICE(this);
15 LOG_UPDATE_INTERVAL(this);
16 LOG_SENSOR(" ", "Temperature", this->temperature_);
17 LOG_SENSOR(" ", "Humidity", this->humidity_);
18}
20 uint8_t raw_data[4] = {0, 0, 0, 0};
21
22 if (this->write(&raw_data[0], 0) != i2c::ERROR_OK) {
23 this->status_set_warning();
24 ESP_LOGE(TAG, "Communication with HYT271 failed! => Ask new values");
25 return;
26 }
27 this->set_timeout("wait_convert", 50, [this]() {
28 uint8_t raw_data[4];
29 if (this->read(raw_data, 4) != i2c::ERROR_OK) {
30 this->status_set_warning();
31 ESP_LOGE(TAG, "Communication with HYT271 failed! => Read values");
32 return;
33 }
34 uint16_t raw_temperature = ((raw_data[2] << 8) | raw_data[3]) >> 2;
35 uint16_t raw_humidity = ((raw_data[0] & 0x3F) << 8) | raw_data[1];
36
37 float temperature = ((float(raw_temperature)) * (165.0f / 16383.0f)) - 40.0f;
38 float humidity = (float(raw_humidity)) * (100.0f / 16383.0f);
39
40 ESP_LOGD(TAG, "Got Temperature=%.1f°C Humidity=%.1f%%", temperature, humidity);
41
42 if (this->temperature_ != nullptr)
43 this->temperature_->publish_state(temperature);
44 if (this->humidity_ != nullptr)
45 this->humidity_->publish_state(humidity);
47 });
48}
50
51} // namespace hyt271
52} // namespace esphome
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
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:72
void update() override
Update the sensor values (temperature+humidity).
Definition hyt271.cpp:19
sensor::Sensor * temperature_
Definition hyt271.h:22
sensor::Sensor * humidity_
Definition hyt271.h:23
float get_setup_priority() const override
Definition hyt271.cpp:49
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:190
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t temperature
Definition sun_gtil2.cpp:12