ESPHome  2024.4.1
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 // use read serial number register with clock stretching disabled as per other SHT3XD_COMMAND registers
10 // which provides support for SHT85 sensor
11 // SHT85 does not support clock stretching and uses same registers as SHT3xd with clock stretching disabled
12 static const uint16_t SHT3XD_COMMAND_READ_SERIAL_NUMBER = 0x3682;
13 
14 static const uint16_t SHT3XD_COMMAND_READ_STATUS = 0xF32D;
15 static const uint16_t SHT3XD_COMMAND_CLEAR_STATUS = 0x3041;
16 static const uint16_t SHT3XD_COMMAND_HEATER_ENABLE = 0x306D;
17 static const uint16_t SHT3XD_COMMAND_HEATER_DISABLE = 0x3066;
18 static const uint16_t SHT3XD_COMMAND_SOFT_RESET = 0x30A2;
19 static const uint16_t SHT3XD_COMMAND_POLLING_H = 0x2400;
20 static const uint16_t SHT3XD_COMMAND_FETCH_DATA = 0xE000;
21 
23  ESP_LOGCONFIG(TAG, "Setting up SHT3xD...");
24  uint16_t raw_serial_number[2];
25  if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER, raw_serial_number, 2)) {
26  this->mark_failed();
27  return;
28  }
29  this->serial_number_ = (uint32_t(raw_serial_number[0]) << 16) | uint32_t(raw_serial_number[1]);
30 
31  if (!this->write_command(heater_enabled_ ? SHT3XD_COMMAND_HEATER_ENABLE : SHT3XD_COMMAND_HEATER_DISABLE)) {
32  this->mark_failed();
33  return;
34  }
35 }
36 
38  ESP_LOGCONFIG(TAG, "SHT3xD:");
39  if (this->is_failed()) {
40  ESP_LOGE(TAG, " Communication with SHT3xD failed!");
41  return;
42  }
43  ESP_LOGD(TAG, " Serial Number: 0x%08" PRIX32, this->serial_number_);
44  ESP_LOGD(TAG, " Heater Enabled: %s", this->heater_enabled_ ? "true" : "false");
45 
46  LOG_I2C_DEVICE(this);
47  LOG_UPDATE_INTERVAL(this);
48 
49  LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
50  LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
51 }
52 
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);
80  this->status_clear_warning();
81  });
82 }
83 
84 } // namespace sht3xd
85 } // namespace esphome
sensor::Sensor * temperature_sensor_
Definition: sht3xd.h:25
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
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:53
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
void dump_config() override
Definition: sht3xd.cpp:37
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
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
sensor::Sensor * humidity_sensor_
Definition: sht3xd.h:26