ESPHome 2026.11.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
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
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
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 if (this->temperature_sensor_ != nullptr)
73 if (this->humidity_sensor_ != nullptr)
75 this->status_set_warning();
76 return;
77 }
78
79 this->set_timeout(50, [this]() {
80 float temperature = NAN;
81 float humidity = NAN;
82 uint16_t raw_data[2];
83 if (!this->read_data(raw_data, 2)) {
84 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
85 } else {
86 temperature = 175.0f * float(raw_data[0]) / 65536.0f - 45.0f;
87 humidity = 100.0f * float(raw_data[1]) / 65536.0f;
88 }
89 if (this->temperature_sensor_ != nullptr)
90 this->temperature_sensor_->publish_state(temperature);
91 if (this->humidity_sensor_ != nullptr)
92 this->humidity_sensor_->publish_state(humidity);
94 if (this->type_ != SHTCX_TYPE_SHTC1) {
95 this->sleep();
96 }
97 });
98}
99
101 this->write_command(SHTCX_COMMAND_SOFT_RESET);
103}
104
105void SHTCXComponent::sleep() { this->write_command(SHTCX_COMMAND_SLEEP); }
106
108 this->write_command(SHTCX_COMMAND_WAKEUP);
110}
111
112} // namespace esphome::shtcx
void mark_failed()
Mark this component as failed.
bool status_has_warning() const
Definition component.h:280
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:296
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:65
sensor::Sensor * humidity_sensor_
Definition shtcx.h:30
sensor::Sensor * temperature_sensor_
Definition shtcx.h:29
void dump_config() override
Definition shtcx.cpp:49
uint8_t type
@ 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