ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
stts22h.cpp
Go to the documentation of this file.
1#include "esphome/core/log.h"
2#include "stts22h.h"
3
4namespace esphome::stts22h {
5
6static const char *const TAG = "stts22h";
7
8static const uint8_t WHOAMI_REG = 0x01;
9static const uint8_t CTRL_REG = 0x04;
10static const uint8_t TEMPERATURE_REG = 0x06;
11
12// CTRL_REG flags
13static const uint8_t LOW_ODR_CTRL_ENABLE_FLAG = 0x80; // Flag to enable low ODR mode in CTRL_REG
14static const uint8_t FREERUN_CTRL_ENABLE_FLAG = 0x04; // Flag to enable FREERUN mode in CTRL_REG
15static const uint8_t ADD_INC_ENABLE_FLAG = 0x08; // Flag to enable ADD_INC (IF_ADD_INC) mode in CTRL_REG
16
17static const uint8_t WHOAMI_STTS22H_IDENTIFICATION = 0xA0; // ID value of STTS22H in WHOAMI_REG
18
19static const float SENSOR_SCALE = 0.01f; // Sensor resolution in degrees Celsius
20
22 // Check if device is a STTS22H
23 if (!this->is_stts22h_sensor_()) {
24 this->mark_failed(LOG_STR("Device is not a STTS22H sensor"));
25 return;
26 }
27
28 this->initialize_sensor_();
29}
30
32 if (this->is_failed()) {
33 return;
34 }
35
36 this->publish_state(this->read_temperature_());
37}
38
40 LOG_SENSOR("", "STTS22H", this);
41 LOG_I2C_DEVICE(this);
42 LOG_UPDATE_INTERVAL(this);
43 if (this->is_failed()) {
44 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
45 }
46}
47
49 uint8_t temp_reg_value[2];
50 if (this->read_register(TEMPERATURE_REG, temp_reg_value, 2) != i2c::NO_ERROR) {
51 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
52 return NAN;
53 }
54
55 // Combine the two bytes into a single 16-bit signed integer
56 // The STTS22H temperature data is in two's complement format
57 int16_t temp_raw_value = static_cast<int16_t>(encode_uint16(temp_reg_value[1], temp_reg_value[0]));
58 return temp_raw_value * SENSOR_SCALE; // Apply sensor resolution
59}
60
62 uint8_t whoami_value;
63 if (this->read_register(WHOAMI_REG, &whoami_value, 1) != i2c::NO_ERROR) {
64 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
65 return false;
66 }
67
68 if (whoami_value != WHOAMI_STTS22H_IDENTIFICATION) {
69 this->mark_failed(LOG_STR("Unexpected WHOAMI identifier. Sensor is not a STTS22H"));
70 return false;
71 }
72
73 return true;
74}
75
77 // Read current CTRL_REG configuration
78 uint8_t ctrl_value;
79 if (this->read_register(CTRL_REG, &ctrl_value, 1) != i2c::NO_ERROR) {
80 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
81 return;
82 }
83
84 // Enable low ODR mode and enable ADD_INC
85 // Before low ODR mode can be used,
86 // FREERUN bit must be cleared (see sensor documentation)
87 ctrl_value &= ~FREERUN_CTRL_ENABLE_FLAG; // Clear FREERUN bit
88 if (this->write_register(CTRL_REG, &ctrl_value, 1) != i2c::NO_ERROR) {
89 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
90 return;
91 }
92
93 // Enable LOW ODR mode and ADD_INC
94 ctrl_value |= LOW_ODR_CTRL_ENABLE_FLAG | ADD_INC_ENABLE_FLAG; // Set LOW ODR bit and ADD_INC bit
95 if (this->write_register(CTRL_REG, &ctrl_value, 1) != i2c::NO_ERROR) {
96 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
97 return;
98 }
99}
100
101} // namespace esphome::stts22h
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
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:44
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:35
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:77
@ NO_ERROR
No error found during execution of method.
Definition i2c_bus.h:32
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:420