ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
stts22h.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3#include "stts22h.h"
4
5namespace esphome::stts22h {
6
7ESPHOME_LOG_TAG(TAG, "stts22h");
8
9static const uint8_t WHOAMI_REG = 0x01;
10static const uint8_t CTRL_REG = 0x04;
11static const uint8_t TEMPERATURE_REG = 0x06;
12
13// CTRL_REG flags
14static const uint8_t LOW_ODR_CTRL_ENABLE_FLAG = 0x80; // Flag to enable low ODR mode in CTRL_REG
15static const uint8_t FREERUN_CTRL_ENABLE_FLAG = 0x04; // Flag to enable FREERUN mode in CTRL_REG
16static const uint8_t ADD_INC_ENABLE_FLAG = 0x08; // Flag to enable ADD_INC (IF_ADD_INC) mode in CTRL_REG
17
18static const uint8_t WHOAMI_STTS22H_IDENTIFICATION = 0xA0; // ID value of STTS22H in WHOAMI_REG
19
20static const float SENSOR_SCALE = 0.01f; // Sensor resolution in degrees Celsius
21
23 if (!this->check_chip_id(this, WHOAMI_REG, WHOAMI_STTS22H_IDENTIFICATION))
24 return;
25
26 this->initialize_sensor_();
27}
28
30 if (this->is_failed()) {
31 return;
32 }
33
34 this->publish_state(this->read_temperature_());
35}
36
38 LOG_SENSOR("", "STTS22H", this);
39 LOG_I2C_DEVICE(this);
40 LOG_UPDATE_INTERVAL(this);
41}
42
44 uint8_t temp_reg_value[2];
45 if (this->read_register(TEMPERATURE_REG, temp_reg_value, 2) != i2c::NO_ERROR) {
46 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
47 return NAN;
48 }
49
50 // Combine the two bytes into a single 16-bit signed integer
51 // The STTS22H temperature data is in two's complement format
52 int16_t temp_raw_value = encode_value_le<int16_t>(temp_reg_value);
53 return temp_raw_value * SENSOR_SCALE; // Apply sensor resolution
54}
55
57 // Read current CTRL_REG configuration
58 uint8_t ctrl_value;
59 if (this->read_register(CTRL_REG, &ctrl_value, 1) != i2c::NO_ERROR) {
60 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
61 return;
62 }
63
64 // Enable low ODR mode and enable ADD_INC
65 // Before low ODR mode can be used,
66 // FREERUN bit must be cleared (see sensor documentation)
67 ctrl_value &= ~FREERUN_CTRL_ENABLE_FLAG; // Clear FREERUN bit
68 if (this->write_register(CTRL_REG, &ctrl_value, 1) != i2c::NO_ERROR) {
69 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
70 return;
71 }
72
73 // Enable LOW ODR mode and ADD_INC
74 ctrl_value |= LOW_ODR_CTRL_ENABLE_FLAG | ADD_INC_ENABLE_FLAG; // Set LOW ODR bit and ADD_INC bit
75 if (this->write_register(CTRL_REG, &ctrl_value, 1) != i2c::NO_ERROR) {
76 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
77 return;
78 }
79}
80
81} // namespace esphome::stts22h
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:54
bool check_chip_id(Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.
Definition i2c.h:232
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:45
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
@ NO_ERROR
No error found during execution of method.
Definition i2c_bus.h:14
ESPHOME_LOG_TAG(TAG, "stts22h")
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1084