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