ESPHome 2025.6.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";
24 }
25}
26
28 ESP_LOGCONFIG(TAG, "Running setup");
29 this->wake_up();
30 this->soft_reset();
31
32 uint16_t device_id_register;
33 if (!this->write_command(SHTCX_COMMAND_READ_ID_REGISTER) || !this->read_data(&device_id_register, 1)) {
34 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
35 this->mark_failed();
36 return;
37 }
38
39 this->sensor_id_ = device_id_register;
40
41 if ((device_id_register & 0x3F) == 0x07) {
42 if (device_id_register & 0x800) {
43 this->type_ = SHTCX_TYPE_SHTC3;
44 } else {
45 this->type_ = SHTCX_TYPE_SHTC1;
46 }
47 } else {
49 }
50}
51
53 ESP_LOGCONFIG(TAG, "SHTCx:");
54 ESP_LOGCONFIG(TAG, " Model: %s (%04x)", to_string(this->type_), this->sensor_id_);
55 LOG_I2C_DEVICE(this);
56 if (this->is_failed()) {
57 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
58 }
59 LOG_UPDATE_INTERVAL(this);
60
61 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
62 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
63}
64
66
68 if (this->status_has_warning()) {
69 ESP_LOGW(TAG, "Retrying communication");
70 this->soft_reset();
71 }
72 if (this->type_ != SHTCX_TYPE_SHTC1) {
73 this->wake_up();
74 }
75 if (!this->write_command(SHTCX_COMMAND_POLLING_H)) {
76 ESP_LOGE(TAG, "Polling failed");
77 if (this->temperature_sensor_ != nullptr)
79 if (this->humidity_sensor_ != nullptr)
81 this->status_set_warning();
82 return;
83 }
84
85 this->set_timeout(50, [this]() {
86 float temperature = NAN;
87 float humidity = NAN;
88 uint16_t raw_data[2];
89 if (!this->read_data(raw_data, 2)) {
90 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
91 this->status_set_warning();
92 } else {
93 temperature = 175.0f * float(raw_data[0]) / 65536.0f - 45.0f;
94 humidity = 100.0f * float(raw_data[1]) / 65536.0f;
95
96 ESP_LOGD(TAG, "Temperature=%.2f°C Humidity=%.2f%%", temperature, humidity);
97 }
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 if (this->type_ != SHTCX_TYPE_SHTC1) {
104 this->sleep();
105 }
106 });
107}
108
110 this->write_command(SHTCX_COMMAND_SOFT_RESET);
112}
113
114void SHTCXComponent::sleep() { this->write_command(SHTCX_COMMAND_SLEEP); }
115
117 this->write_command(SHTCX_COMMAND_WAKEUP);
119}
120
121} // namespace shtcx
122} // 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:75
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:52
float get_setup_priority() const override
Definition shtcx.cpp:65
uint8_t type
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:20
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:31
uint16_t temperature
Definition sun_gtil2.cpp:12