ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
sts3x.cpp
Go to the documentation of this file.
1#include "sts3x.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace sts3x {
6
7static const char *const TAG = "sts3x";
8
9static const uint16_t STS3X_COMMAND_READ_SERIAL_NUMBER = 0x3780;
10static const uint16_t STS3X_COMMAND_READ_STATUS = 0xF32D;
11static const uint16_t STS3X_COMMAND_SOFT_RESET = 0x30A2;
12static const uint16_t STS3X_COMMAND_POLLING_H = 0x2400;
13
15static const uint16_t STS3X_COMMAND_CLEAR_STATUS = 0x3041;
16static const uint16_t STS3X_COMMAND_HEATER_ENABLE = 0x306D;
17static const uint16_t STS3X_COMMAND_HEATER_DISABLE = 0x3066;
18static const uint16_t STS3X_COMMAND_FETCH_DATA = 0xE000;
19
21 if (!this->write_command(STS3X_COMMAND_READ_SERIAL_NUMBER)) {
22 this->mark_failed();
23 return;
24 }
25
26 uint16_t raw_serial_number[2];
27 if (!this->read_data(raw_serial_number, 1)) {
28 this->mark_failed();
29 return;
30 }
31 uint32_t serial_number = (uint32_t(raw_serial_number[0]) << 16);
32 ESP_LOGV(TAG, " Serial Number: 0x%08" PRIX32, serial_number);
33}
35 ESP_LOGCONFIG(TAG, "STS3x:");
36 LOG_I2C_DEVICE(this);
37 if (this->is_failed()) {
38 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
39 }
40 LOG_UPDATE_INTERVAL(this);
41
42 LOG_SENSOR(" ", "STS3x", this);
43}
46 if (this->status_has_warning()) {
47 ESP_LOGD(TAG, "Retrying to reconnect the sensor.");
48 this->write_command(STS3X_COMMAND_SOFT_RESET);
49 }
50 if (!this->write_command(STS3X_COMMAND_POLLING_H)) {
51 this->status_set_warning();
52 return;
53 }
54
55 this->set_timeout(50, [this]() {
56 uint16_t raw_data[1];
57 if (!this->read_data(raw_data, 1)) {
58 this->status_set_warning();
59 return;
60 }
61
62 float temperature = 175.0f * float(raw_data[0]) / 65535.0f - 45.0f;
63 ESP_LOGD(TAG, "Got temperature=%.2f°C", temperature);
64 this->publish_state(temperature);
66 });
67}
68
69} // namespace sts3x
70} // 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
float get_setup_priority() const override
Definition sts3x.cpp:44
void dump_config() override
Definition sts3x.cpp:34
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t temperature
Definition sun_gtil2.cpp:12
char serial_number[10]
Definition sun_gtil2.cpp:15