ESPHome 2025.9.0-dev
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 this->wake_up();
29 this->soft_reset();
30
31 uint16_t device_id_register;
32 if (!this->write_command(SHTCX_COMMAND_READ_ID_REGISTER) || !this->read_data(&device_id_register, 1)) {
33 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
34 this->mark_failed();
35 return;
36 }
37
38 this->sensor_id_ = device_id_register;
39
40 if ((device_id_register & 0x3F) == 0x07) {
41 if (device_id_register & 0x800) {
42 this->type_ = SHTCX_TYPE_SHTC3;
43 } else {
44 this->type_ = SHTCX_TYPE_SHTC1;
45 }
46 } else {
48 }
49}
50
52 ESP_LOGCONFIG(TAG, "SHTCx:");
53 ESP_LOGCONFIG(TAG, " Model: %s (%04x)", to_string(this->type_), this->sensor_id_);
54 LOG_I2C_DEVICE(this);
55 if (this->is_failed()) {
56 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
57 }
58 LOG_UPDATE_INTERVAL(this);
59
60 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
61 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
62}
63
65
67 if (this->status_has_warning()) {
68 ESP_LOGW(TAG, "Retrying communication");
69 this->soft_reset();
70 }
71 if (this->type_ != SHTCX_TYPE_SHTC1) {
72 this->wake_up();
73 }
74 if (!this->write_command(SHTCX_COMMAND_POLLING_H)) {
75 ESP_LOGE(TAG, "Polling failed");
76 if (this->temperature_sensor_ != nullptr)
78 if (this->humidity_sensor_ != nullptr)
80 this->status_set_warning();
81 return;
82 }
83
84 this->set_timeout(50, [this]() {
85 float temperature = NAN;
86 float humidity = NAN;
87 uint16_t raw_data[2];
88 if (!this->read_data(raw_data, 2)) {
89 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
90 this->status_set_warning();
91 } else {
92 temperature = 175.0f * float(raw_data[0]) / 65536.0f - 45.0f;
93 humidity = 100.0f * float(raw_data[1]) / 65536.0f;
94
95 ESP_LOGD(TAG, "Temperature=%.2f°C Humidity=%.2f%%", temperature, humidity);
96 }
97 if (this->temperature_sensor_ != nullptr)
98 this->temperature_sensor_->publish_state(temperature);
99 if (this->humidity_sensor_ != nullptr)
100 this->humidity_sensor_->publish_state(humidity);
101 this->status_clear_warning();
102 if (this->type_ != SHTCX_TYPE_SHTC1) {
103 this->sleep();
104 }
105 });
106}
107
109 this->write_command(SHTCX_COMMAND_SOFT_RESET);
111}
112
113void SHTCXComponent::sleep() { this->write_command(SHTCX_COMMAND_SLEEP); }
114
116 this->write_command(SHTCX_COMMAND_WAKEUP);
118}
119
120} // namespace shtcx
121} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
bool status_has_warning() const
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.
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:45
sensor::Sensor * humidity_sensor_
Definition shtcx.h:30
sensor::Sensor * temperature_sensor_
Definition shtcx.h:29
void dump_config() override
Definition shtcx.cpp:51
float get_setup_priority() const override
Definition shtcx.cpp:64
uint8_t type
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
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