ESPHome  2024.4.0
shtcx.cpp
Go to the documentation of this file.
1 #include "shtcx.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/hal.h"
4 
5 namespace esphome {
6 namespace shtcx {
7 
8 static const char *const TAG = "shtcx";
9 
10 static const uint16_t SHTCX_COMMAND_SLEEP = 0xB098;
11 static const uint16_t SHTCX_COMMAND_WAKEUP = 0x3517;
12 static const uint16_t SHTCX_COMMAND_READ_ID_REGISTER = 0xEFC8;
13 static const uint16_t SHTCX_COMMAND_SOFT_RESET = 0x805D;
14 static const uint16_t SHTCX_COMMAND_POLLING_H = 0x7866;
15 
16 inline const char *to_string(SHTCXType type) {
17  switch (type) {
18  case SHTCX_TYPE_SHTC3:
19  return "SHTC3";
20  case SHTCX_TYPE_SHTC1:
21  return "SHTC1";
22  default:
23  return "[Unknown model]";
24  }
25 }
26 
28  ESP_LOGCONFIG(TAG, "Setting up SHTCx...");
29  this->wake_up();
30  this->soft_reset();
31 
32  if (!this->write_command(SHTCX_COMMAND_READ_ID_REGISTER)) {
33  ESP_LOGE(TAG, "Error requesting Device ID");
34  this->mark_failed();
35  return;
36  }
37 
38  uint16_t device_id_register;
39  if (!this->read_data(&device_id_register, 1)) {
40  ESP_LOGE(TAG, "Error reading Device ID");
41  this->mark_failed();
42  return;
43  }
44 
45  this->sensor_id_ = device_id_register;
46 
47  if ((device_id_register & 0x3F) == 0x07) {
48  if (device_id_register & 0x800) {
49  this->type_ = SHTCX_TYPE_SHTC3;
50  } else {
51  this->type_ = SHTCX_TYPE_SHTC1;
52  }
53  } else {
54  this->type_ = SHTCX_TYPE_UNKNOWN;
55  }
56  ESP_LOGCONFIG(TAG, " Device identified: %s (%04x)", to_string(this->type_), device_id_register);
57 }
59  ESP_LOGCONFIG(TAG, "SHTCx:");
60  ESP_LOGCONFIG(TAG, " Model: %s (%04x)", to_string(this->type_), this->sensor_id_);
61  LOG_I2C_DEVICE(this);
62  if (this->is_failed()) {
63  ESP_LOGE(TAG, "Communication with SHTCx failed!");
64  }
65  LOG_UPDATE_INTERVAL(this);
66 
67  LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
68  LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
69 }
72  if (this->status_has_warning()) {
73  ESP_LOGW(TAG, "Retrying to reconnect the sensor.");
74  this->soft_reset();
75  }
76  if (this->type_ != SHTCX_TYPE_SHTC1) {
77  this->wake_up();
78  }
79  if (!this->write_command(SHTCX_COMMAND_POLLING_H)) {
80  ESP_LOGE(TAG, "sensor polling failed");
81  if (this->temperature_sensor_ != nullptr)
83  if (this->humidity_sensor_ != nullptr)
84  this->humidity_sensor_->publish_state(NAN);
85  this->status_set_warning();
86  return;
87  }
88 
89  this->set_timeout(50, [this]() {
90  float temperature = NAN;
91  float humidity = NAN;
92  uint16_t raw_data[2];
93  if (!this->read_data(raw_data, 2)) {
94  ESP_LOGE(TAG, "sensor read failed");
95  this->status_set_warning();
96  } else {
97  temperature = 175.0f * float(raw_data[0]) / 65536.0f - 45.0f;
98  humidity = 100.0f * float(raw_data[1]) / 65536.0f;
99 
100  ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity);
101  }
102  if (this->temperature_sensor_ != nullptr)
103  this->temperature_sensor_->publish_state(temperature);
104  if (this->humidity_sensor_ != nullptr)
105  this->humidity_sensor_->publish_state(humidity);
106  this->status_clear_warning();
107  if (this->type_ != SHTCX_TYPE_SHTC1) {
108  this->sleep();
109  }
110  });
111 }
112 
114  this->write_command(SHTCX_COMMAND_SOFT_RESET);
115  delayMicroseconds(200);
116 }
117 void SHTCXComponent::sleep() { this->write_command(SHTCX_COMMAND_SLEEP); }
118 
120  this->write_command(SHTCX_COMMAND_WAKEUP);
121  delayMicroseconds(200);
122 }
123 
124 } // namespace shtcx
125 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
const char * to_string(SHTCXType type)
Definition: shtcx.cpp:16
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 dump_config() override
Definition: shtcx.cpp:58
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.
sensor::Sensor * humidity_sensor_
Definition: shtcx.h:30
void status_clear_warning()
Definition: component.cpp:166
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint8_t type
uint16_t temperature
Definition: sun_gtil2.cpp:26
void update() override
Definition: shtcx.cpp:71
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
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:28
sensor::Sensor * temperature_sensor_
Definition: shtcx.h:29
void setup() override
Definition: shtcx.cpp:27
float get_setup_priority() const override
Definition: shtcx.cpp:70