ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
sht3xd.cpp
Go to the documentation of this file.
1#include "sht3xd.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace sht3xd {
6
7static const char *const TAG = "sht3xd";
8
9// https://sensirion.com/media/documents/E5762713/63D103C2/Sensirion_electronic_identification_code_SHT3x.pdf
10// indicates two possible read serial number registers either with clock stretching enabled or disabled.
11// Other SHT3XD_COMMAND registers use the clock stretching disabled register.
12// To ensure compatibility, reading serial number using the register with clock stretching register enabled
13// (used originally in this component) is tried first and if that fails the alternate register address
14// with clock stretching disabled is read.
15// If both fail (e.g. some clones don't support the command), we continue so temp/humidity still work.
16// Second attempt uses 10ms delay for boards that need more time before read (max permitted by ESPHome guidelines).
17
18static const uint16_t SHT3XD_COMMAND_READ_SERIAL_NUMBER_CLOCK_STRETCHING = 0x3780;
19static const uint16_t SHT3XD_COMMAND_READ_SERIAL_NUMBER = 0x3682;
20
21static const uint16_t SHT3XD_COMMAND_READ_STATUS = 0xF32D;
22static const uint16_t SHT3XD_COMMAND_CLEAR_STATUS = 0x3041;
23static const uint16_t SHT3XD_COMMAND_HEATER_ENABLE = 0x306D;
24static const uint16_t SHT3XD_COMMAND_HEATER_DISABLE = 0x3066;
25static const uint16_t SHT3XD_COMMAND_SOFT_RESET = 0x30A2;
26static const uint16_t SHT3XD_COMMAND_POLLING_H = 0x2400;
27static const uint16_t SHT3XD_COMMAND_FETCH_DATA = 0xE000;
28
30 uint16_t raw_serial_number[2]{0};
31 if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER_CLOCK_STRETCHING, raw_serial_number, 2)) {
32 if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER, raw_serial_number, 2, 10)) {
33 ESP_LOGW(TAG, "Serial number read failed, continuing without it (clone or non-standard sensor)");
34 }
35 }
36 this->serial_number_ = (uint32_t(raw_serial_number[0]) << 16) | uint32_t(raw_serial_number[1]);
37
38 if (!this->write_command(this->heater_enabled_ ? SHT3XD_COMMAND_HEATER_ENABLE : SHT3XD_COMMAND_HEATER_DISABLE)) {
39 this->mark_failed(LOG_STR("Failed to set heater mode"));
40 return;
41 }
42}
43
45 ESP_LOGCONFIG(TAG,
46 "SHT3xD:\n"
47 " Serial Number: 0x%08" PRIX32 "\n"
48 " Heater Enabled: %s",
49 this->serial_number_, TRUEFALSE(this->heater_enabled_));
50 LOG_I2C_DEVICE(this);
51 LOG_UPDATE_INTERVAL(this);
52 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
53 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
54}
55
57 if (this->status_has_warning()) {
58 ESP_LOGD(TAG, "Retrying to reconnect the sensor.");
59 this->write_command(SHT3XD_COMMAND_SOFT_RESET);
60 }
61 if (!this->write_command(SHT3XD_COMMAND_POLLING_H)) {
62 this->status_set_warning();
63 return;
64 }
65
66 this->set_timeout(50, [this]() {
67 uint16_t raw_data[2];
68 if (!this->read_data(raw_data, 2)) {
69 this->status_set_warning();
70 return;
71 }
72
73 float temperature = 175.0f * float(raw_data[0]) / 65535.0f - 45.0f;
74 float humidity = 100.0f * float(raw_data[1]) / 65535.0f;
75
76 ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity);
77 if (this->temperature_sensor_ != nullptr)
78 this->temperature_sensor_->publish_state(temperature);
79 if (this->humidity_sensor_ != nullptr)
80 this->humidity_sensor_->publish_state(humidity);
82 });
83}
84
85} // namespace sht3xd
86} // namespace esphome
void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
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:443
bool status_has_warning() const
void status_clear_warning()
bool get_register(uint16_t command, uint16_t *data, uint8_t len, uint8_t delay=0)
get data words from I2C register.
bool write_command(T i2c_register)
Write a command to the I2C device.
bool read_data(uint16_t *data, uint8_t len)
Read data words from I2C device.
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
sensor::Sensor * temperature_sensor_
Definition sht3xd.h:22
sensor::Sensor * humidity_sensor_
Definition sht3xd.h:23
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t temperature
Definition sun_gtil2.cpp:12