ESPHome 2026.11.0b6
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::shtcx {
6
7ESPHOME_LOG_TAG(TAG, "shtcx");
8
9static constexpr uint16_t SHTCX_COMMAND_SLEEP = 0xB098;
10static constexpr uint16_t SHTCX_COMMAND_WAKEUP = 0x3517;
11static constexpr uint16_t SHTCX_COMMAND_READ_ID_REGISTER = 0xEFC8;
12static constexpr uint16_t SHTCX_COMMAND_SOFT_RESET = 0x805D;
13static constexpr uint16_t SHTCX_COMMAND_POLLING_H = 0x7866;
14
15static const LogString *shtcx_type_to_string(SHTCXType type) {
16 switch (type) {
18 return LOG_STR("SHTC3");
20 return LOG_STR("SHTC1");
21 default:
22 return LOG_STR("UNKNOWN");
23 }
24}
25
27 this->wake_up();
28 this->soft_reset();
29
30 uint16_t device_id_register;
31 if (!this->write_command(SHTCX_COMMAND_READ_ID_REGISTER) || !this->read_data(&device_id_register, 1)) {
32 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
33 return;
34 }
35
36 this->sensor_id_ = device_id_register;
37
38 if ((device_id_register & 0x3F) == 0x07) {
39 if (device_id_register & 0x800) {
40 this->type_ = SHTCX_TYPE_SHTC3;
41 } else {
42 this->type_ = SHTCX_TYPE_SHTC1;
43 }
44 } else {
46 }
47}
48
49void SHTCXComponent::dump_config() {
50 ESP_LOGCONFIG(TAG,
51 "SHTCx:\n"
52 " Model: %s (%04x)",
53 LOG_STR_ARG(shtcx_type_to_string(this->type_)), this->sensor_id_);
54 LOG_I2C_DEVICE(this);
55 LOG_UPDATE_INTERVAL(this);
56
57 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
58 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
59}
60
61void SHTCXComponent::update() {
62 if (this->status_has_warning()) {
63 ESP_LOGW(TAG, "Retrying communication");
64 this->soft_reset();
65 }
66 if (this->type_ != SHTCX_TYPE_SHTC1) {
67 this->wake_up();
68 }
69 if (!this->write_command(SHTCX_COMMAND_POLLING_H)) {
70 ESP_LOGE(TAG, "Polling failed");
71 PUBLISH_STATE(this->temperature_sensor_, NAN);
72 PUBLISH_STATE(this->humidity_sensor_, NAN);
73 this->status_set_warning();
74 return;
75 }
76
77 this->set_timeout(50, [this]() {
78 float temperature = NAN;
79 float humidity = NAN;
80 uint16_t raw_data[2];
81 if (!this->read_data(raw_data, 2)) {
82 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
83 } else {
84 temperature = 175.0f * float(raw_data[0]) / 65536.0f - 45.0f;
85 humidity = 100.0f * float(raw_data[1]) / 65536.0f;
86 }
87 PUBLISH_STATE(this->temperature_sensor_, temperature);
88 PUBLISH_STATE(this->humidity_sensor_, humidity);
90 if (this->type_ != SHTCX_TYPE_SHTC1) {
91 this->sleep();
92 }
93 });
94}
95
96void SHTCXComponent::soft_reset() {
97 this->write_command(SHTCX_COMMAND_SOFT_RESET);
99}
100
101void SHTCXComponent::sleep() { this->write_command(SHTCX_COMMAND_SLEEP); }
102
103void SHTCXComponent::wake_up() {
104 this->write_command(SHTCX_COMMAND_WAKEUP);
106}
107
108} // namespace esphome::shtcx
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool status_has_warning() const
Definition component.h:293
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:309
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.
uint8_t type
FLAG_HAS_TRANSITION float
@ SHTCX_TYPE_SHTC1
Definition shtcx.h:11
@ SHTCX_TYPE_UNKNOWN
Definition shtcx.h:12
@ SHTCX_TYPE_SHTC3
Definition shtcx.h:10
ESPHOME_LOG_TAG(TAG, "shtcx")
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48
uint16_t temperature
Definition sun_gtil2.cpp:12