ESPHome  2024.12.2
sts3x.cpp
Go to the documentation of this file.
1 #include "sts3x.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace sts3x {
6 
7 static const char *const TAG = "sts3x";
8 
9 static const uint16_t STS3X_COMMAND_READ_SERIAL_NUMBER = 0x3780;
10 static const uint16_t STS3X_COMMAND_READ_STATUS = 0xF32D;
11 static const uint16_t STS3X_COMMAND_SOFT_RESET = 0x30A2;
12 static const uint16_t STS3X_COMMAND_POLLING_H = 0x2400;
13 
15 static const uint16_t STS3X_COMMAND_CLEAR_STATUS = 0x3041;
16 static const uint16_t STS3X_COMMAND_HEATER_ENABLE = 0x306D;
17 static const uint16_t STS3X_COMMAND_HEATER_DISABLE = 0x3066;
18 static 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);
66  this->status_clear_warning();
67  });
68 }
69 
70 } // namespace sts3x
71 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
bool status_has_warning() const
Definition: component.cpp:149
bool write_command(T i2c_register)
Write a command to the i2c device.
Definition: i2c_sensirion.h:82
bool is_failed() const
Definition: component.cpp:143
float get_setup_priority() const override
Definition: sts3x.cpp:45
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:69
void setup() override
Definition: sts3x.cpp:20
bool read_data(uint16_t *data, uint8_t len)
Read data words from i2c device.
char serial_number[10]
Definition: sun_gtil2.cpp:29
void status_clear_warning()
Definition: component.cpp:166
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint16_t temperature
Definition: sun_gtil2.cpp:26
void update() override
Definition: sts3x.cpp:46
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void dump_config() override
Definition: sts3x.cpp:35