ESPHome 2025.5.0
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 ESP_LOGCONFIG(TAG, "Setting up STS3x...");
22 if (!this->write_command(STS3X_COMMAND_READ_SERIAL_NUMBER)) {
23 this->mark_failed();
24 return;
25 }
26
27 uint16_t raw_serial_number[2];
28 if (!this->read_data(raw_serial_number, 1)) {
29 this->mark_failed();
30 return;
31 }
32 uint32_t serial_number = (uint32_t(raw_serial_number[0]) << 16);
33 ESP_LOGV(TAG, " Serial Number: 0x%08" PRIX32, serial_number);
34}
36 ESP_LOGCONFIG(TAG, "STS3x:");
37 LOG_I2C_DEVICE(this);
38 if (this->is_failed()) {
39 ESP_LOGE(TAG, "Communication with ST3x failed!");
40 }
41 LOG_UPDATE_INTERVAL(this);
42
43 LOG_SENSOR(" ", "STS3x", this);
44}
47 if (this->status_has_warning()) {
48 ESP_LOGD(TAG, "Retrying to reconnect the sensor.");
49 this->write_command(STS3X_COMMAND_SOFT_RESET);
50 }
51 if (!this->write_command(STS3X_COMMAND_POLLING_H)) {
52 this->status_set_warning();
53 return;
54 }
55
56 this->set_timeout(50, [this]() {
57 uint16_t raw_data[1];
58 if (!this->read_data(raw_data, 1)) {
59 this->status_set_warning();
60 return;
61 }
62
63 float temperature = 175.0f * float(raw_data[0]) / 65535.0f - 45.0f;
64 ESP_LOGD(TAG, "Got temperature=%.2f°C", temperature);
65 this->publish_state(temperature);
67 });
68}
69
70} // namespace sts3x
71} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
bool status_has_warning() const
void status_set_warning(const char *message="unspecified")
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.
Definition component.cpp:72
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:39
float get_setup_priority() const override
Definition sts3x.cpp:45
void dump_config() override
Definition sts3x.cpp:35
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:19
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