ESPHome 2025.5.0
Loading...
Searching...
No Matches
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
5namespace esphome {
6namespace shtcx {
7
8static const char *const TAG = "shtcx";
9
10static const uint16_t SHTCX_COMMAND_SLEEP = 0xB098;
11static const uint16_t SHTCX_COMMAND_WAKEUP = 0x3517;
12static const uint16_t SHTCX_COMMAND_READ_ID_REGISTER = 0xEFC8;
13static const uint16_t SHTCX_COMMAND_SOFT_RESET = 0x805D;
14static const uint16_t SHTCX_COMMAND_POLLING_H = 0x7866;
15
16inline const char *to_string(SHTCXType type) {
17 switch (type) {
19 return "SHTC3";
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 {
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)
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);
116}
117void SHTCXComponent::sleep() { this->write_command(SHTCX_COMMAND_SLEEP); }
118
120 this->write_command(SHTCX_COMMAND_WAKEUP);
122}
123
124} // namespace shtcx
125} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
bool status_has_warning() const
void status_set_warning(const char *message="unspecified")
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.
Definition component.cpp:72
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:39
sensor::Sensor * humidity_sensor_
Definition shtcx.h:30
sensor::Sensor * temperature_sensor_
Definition shtcx.h:29
void dump_config() override
Definition shtcx.cpp:58
float get_setup_priority() const override
Definition shtcx.cpp:70
uint8_t type
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
@ SHTCX_TYPE_SHTC1
Definition shtcx.h:10
@ SHTCX_TYPE_UNKNOWN
Definition shtcx.h:10
@ SHTCX_TYPE_SHTC3
Definition shtcx.h:10
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:30
uint16_t temperature
Definition sun_gtil2.cpp:12