ESPHome 2026.5.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::shtcx {
6
7static const char *const 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 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
33 this->mark_failed();
34 return;
35 }
36
37 this->sensor_id_ = device_id_register;
38
39 if ((device_id_register & 0x3F) == 0x07) {
40 if (device_id_register & 0x800) {
41 this->type_ = SHTCX_TYPE_SHTC3;
42 } else {
43 this->type_ = SHTCX_TYPE_SHTC1;
44 }
45 } else {
47 }
48}
49
51 ESP_LOGCONFIG(TAG,
52 "SHTCx:\n"
53 " Model: %s (%04x)",
54 LOG_STR_ARG(shtcx_type_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 if (this->status_has_warning()) {
67 ESP_LOGW(TAG, "Retrying communication");
68 this->soft_reset();
69 }
70 if (this->type_ != SHTCX_TYPE_SHTC1) {
71 this->wake_up();
72 }
73 if (!this->write_command(SHTCX_COMMAND_POLLING_H)) {
74 ESP_LOGE(TAG, "Polling failed");
75 if (this->temperature_sensor_ != nullptr)
77 if (this->humidity_sensor_ != nullptr)
79 this->status_set_warning();
80 return;
81 }
82
83 this->set_timeout(50, [this]() {
84 float temperature = NAN;
85 float humidity = NAN;
86 uint16_t raw_data[2];
87 if (!this->read_data(raw_data, 2)) {
88 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
89 this->status_set_warning();
90 } else {
91 temperature = 175.0f * float(raw_data[0]) / 65536.0f - 45.0f;
92 humidity = 100.0f * float(raw_data[1]) / 65536.0f;
93 }
94 if (this->temperature_sensor_ != nullptr)
95 this->temperature_sensor_->publish_state(temperature);
96 if (this->humidity_sensor_ != nullptr)
97 this->humidity_sensor_->publish_state(humidity);
99 if (this->type_ != SHTCX_TYPE_SHTC1) {
100 this->sleep();
101 }
102 });
103}
104
106 this->write_command(SHTCX_COMMAND_SOFT_RESET);
108}
109
110void SHTCXComponent::sleep() { this->write_command(SHTCX_COMMAND_SLEEP); }
111
113 this->write_command(SHTCX_COMMAND_WAKEUP);
115}
116
117} // namespace esphome::shtcx
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
bool status_has_warning() const
Definition component.h:290
void status_clear_warning()
Definition component.h:306
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:68
sensor::Sensor * humidity_sensor_
Definition shtcx.h:30
sensor::Sensor * temperature_sensor_
Definition shtcx.h:29
void dump_config() override
Definition shtcx.cpp:50
uint16_t type
@ SHTCX_TYPE_SHTC1
Definition shtcx.h:11
@ SHTCX_TYPE_UNKNOWN
Definition shtcx.h:12
@ SHTCX_TYPE_SHTC3
Definition shtcx.h:10
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:30
uint16_t temperature
Definition sun_gtil2.cpp:12