ESPHome 2025.9.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
16static const uint16_t SHT3XD_COMMAND_READ_SERIAL_NUMBER_CLOCK_STRETCHING = 0x3780;
17static const uint16_t SHT3XD_COMMAND_READ_SERIAL_NUMBER = 0x3682;
18
19static const uint16_t SHT3XD_COMMAND_READ_STATUS = 0xF32D;
20static const uint16_t SHT3XD_COMMAND_CLEAR_STATUS = 0x3041;
21static const uint16_t SHT3XD_COMMAND_HEATER_ENABLE = 0x306D;
22static const uint16_t SHT3XD_COMMAND_HEATER_DISABLE = 0x3066;
23static const uint16_t SHT3XD_COMMAND_SOFT_RESET = 0x30A2;
24static const uint16_t SHT3XD_COMMAND_POLLING_H = 0x2400;
25static const uint16_t SHT3XD_COMMAND_FETCH_DATA = 0xE000;
26
28 uint16_t raw_serial_number[2];
29 if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER_CLOCK_STRETCHING, raw_serial_number, 2)) {
30 this->error_code_ = READ_SERIAL_STRETCHED_FAILED;
31 if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER, raw_serial_number, 2)) {
32 this->error_code_ = READ_SERIAL_FAILED;
33 this->mark_failed();
34 return;
35 }
36 }
37
38 this->serial_number_ = (uint32_t(raw_serial_number[0]) << 16) | uint32_t(raw_serial_number[1]);
39
40 if (!this->write_command(heater_enabled_ ? SHT3XD_COMMAND_HEATER_ENABLE : SHT3XD_COMMAND_HEATER_DISABLE)) {
41 this->error_code_ = WRITE_HEATER_MODE_FAILED;
42 this->mark_failed();
43 return;
44 }
45}
46
48 ESP_LOGCONFIG(TAG, "SHT3xD:");
49 switch (this->error_code_) {
51 ESP_LOGD(TAG, " Error reading serial number");
52 break;
54 ESP_LOGD(TAG, " Error writing heater mode");
55 break;
56 default:
57 break;
58 }
59 if (this->is_failed()) {
60 ESP_LOGE(TAG, " Communication with SHT3xD failed!");
61 return;
62 }
63 ESP_LOGD(TAG, " Serial Number: 0x%08" PRIX32, this->serial_number_);
64 ESP_LOGD(TAG, " Heater Enabled: %s", this->heater_enabled_ ? "true" : "false");
65
66 LOG_I2C_DEVICE(this);
67 LOG_UPDATE_INTERVAL(this);
68
69 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
70 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
71}
72
74
76 if (this->status_has_warning()) {
77 ESP_LOGD(TAG, "Retrying to reconnect the sensor.");
78 this->write_command(SHT3XD_COMMAND_SOFT_RESET);
79 }
80 if (!this->write_command(SHT3XD_COMMAND_POLLING_H)) {
81 this->status_set_warning();
82 return;
83 }
84
85 this->set_timeout(50, [this]() {
86 uint16_t raw_data[2];
87 if (!this->read_data(raw_data, 2)) {
88 this->status_set_warning();
89 return;
90 }
91
92 float temperature = 175.0f * float(raw_data[0]) / 65535.0f - 45.0f;
93 float humidity = 100.0f * float(raw_data[1]) / 65535.0f;
94
95 ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity);
96 if (this->temperature_sensor_ != nullptr)
97 this->temperature_sensor_->publish_state(temperature);
98 if (this->humidity_sensor_ != nullptr)
99 this->humidity_sensor_->publish_state(humidity);
100 this->status_clear_warning();
101 });
102}
103
104} // namespace sht3xd
105} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
bool status_has_warning() const
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.
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:45
sensor::Sensor * temperature_sensor_
Definition sht3xd.h:32
sensor::Sensor * humidity_sensor_
Definition sht3xd.h:33
float get_setup_priority() const override
Definition sht3xd.cpp:73
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t temperature
Definition sun_gtil2.cpp:12