ESPHome  2024.11.0
sht3xd.cpp
Go to the documentation of this file.
1 #include "sht3xd.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace sht3xd {
6 
7 static 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 
16 static const uint16_t SHT3XD_COMMAND_READ_SERIAL_NUMBER_CLOCK_STRETCHING = 0x3780;
17 static const uint16_t SHT3XD_COMMAND_READ_SERIAL_NUMBER = 0x3682;
18 
19 static const uint16_t SHT3XD_COMMAND_READ_STATUS = 0xF32D;
20 static const uint16_t SHT3XD_COMMAND_CLEAR_STATUS = 0x3041;
21 static const uint16_t SHT3XD_COMMAND_HEATER_ENABLE = 0x306D;
22 static const uint16_t SHT3XD_COMMAND_HEATER_DISABLE = 0x3066;
23 static const uint16_t SHT3XD_COMMAND_SOFT_RESET = 0x30A2;
24 static const uint16_t SHT3XD_COMMAND_POLLING_H = 0x2400;
25 static const uint16_t SHT3XD_COMMAND_FETCH_DATA = 0xE000;
26 
28  ESP_LOGCONFIG(TAG, "Setting up SHT3xD...");
29  uint16_t raw_serial_number[2];
30  if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER_CLOCK_STRETCHING, raw_serial_number, 2)) {
31  this->error_code_ = READ_SERIAL_STRETCHED_FAILED;
32  if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER, raw_serial_number, 2)) {
33  this->error_code_ = READ_SERIAL_FAILED;
34  this->mark_failed();
35  return;
36  }
37  }
38 
39  this->serial_number_ = (uint32_t(raw_serial_number[0]) << 16) | uint32_t(raw_serial_number[1]);
40 
41  if (!this->write_command(heater_enabled_ ? SHT3XD_COMMAND_HEATER_ENABLE : SHT3XD_COMMAND_HEATER_DISABLE)) {
42  this->error_code_ = WRITE_HEATER_MODE_FAILED;
43  this->mark_failed();
44  return;
45  }
46 }
47 
49  ESP_LOGCONFIG(TAG, "SHT3xD:");
50  switch (this->error_code_) {
51  case READ_SERIAL_FAILED:
52  ESP_LOGD(TAG, " Error reading serial number");
53  break;
55  ESP_LOGD(TAG, " Error writing heater mode");
56  break;
57  default:
58  break;
59  }
60  if (this->is_failed()) {
61  ESP_LOGE(TAG, " Communication with SHT3xD failed!");
62  return;
63  }
64  ESP_LOGD(TAG, " Setup successful");
65  ESP_LOGD(TAG, " Serial Number: 0x%08" PRIX32, this->serial_number_);
66  ESP_LOGD(TAG, " Heater Enabled: %s", this->heater_enabled_ ? "true" : "false");
67 
68  LOG_I2C_DEVICE(this);
69  LOG_UPDATE_INTERVAL(this);
70 
71  LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
72  LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
73 }
74 
76 
78  if (this->status_has_warning()) {
79  ESP_LOGD(TAG, "Retrying to reconnect the sensor.");
80  this->write_command(SHT3XD_COMMAND_SOFT_RESET);
81  }
82  if (!this->write_command(SHT3XD_COMMAND_POLLING_H)) {
83  this->status_set_warning();
84  return;
85  }
86 
87  this->set_timeout(50, [this]() {
88  uint16_t raw_data[2];
89  if (!this->read_data(raw_data, 2)) {
90  this->status_set_warning();
91  return;
92  }
93 
94  float temperature = 175.0f * float(raw_data[0]) / 65535.0f - 45.0f;
95  float humidity = 100.0f * float(raw_data[1]) / 65535.0f;
96 
97  ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity);
98  if (this->temperature_sensor_ != nullptr)
99  this->temperature_sensor_->publish_state(temperature);
100  if (this->humidity_sensor_ != nullptr)
101  this->humidity_sensor_->publish_state(humidity);
102  this->status_clear_warning();
103  });
104 }
105 
106 } // namespace sht3xd
107 } // namespace esphome
sensor::Sensor * temperature_sensor_
Definition: sht3xd.h:32
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
bool status_has_warning() const
Definition: component.cpp:149
bool write_command(T i2c_register)
Write a command to the i2c device.
Definition: i2c_sensirion.h:82
bool is_failed() const
Definition: component.cpp:143
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:69
bool read_data(uint16_t *data, uint8_t len)
Read data words from i2c device.
void status_clear_warning()
Definition: component.cpp:166
float get_setup_priority() const override
Definition: sht3xd.cpp:75
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
void dump_config() override
Definition: sht3xd.cpp:48
uint16_t temperature
Definition: sun_gtil2.cpp:26
bool get_register(uint16_t command, uint16_t *data, uint8_t len, uint8_t delay=0)
get data words from i2c register.
Definition: i2c_sensirion.h:43
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
sensor::Sensor * humidity_sensor_
Definition: sht3xd.h:33