ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
tmp102.cpp
Go to the documentation of this file.
1#include "tmp102.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome::tmp102 {
6
7static const char *const TAG = "tmp102";
8
9static const uint8_t TMP102_ADDRESS = 0x48;
10static const uint8_t TMP102_REGISTER_TEMPERATURE = 0x00;
11static const uint8_t TMP102_REGISTER_CONFIGURATION = 0x01;
12static const uint8_t TMP102_REGISTER_LOW_LIMIT = 0x02;
13static const uint8_t TMP102_REGISTER_HIGH_LIMIT = 0x03;
14
15static const float TMP102_CONVERSION_FACTOR = 0.0625;
16
18 ESP_LOGCONFIG(TAG, "TMP102:");
19 LOG_I2C_DEVICE(this);
20 if (this->is_failed()) {
21 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
22 }
23 LOG_UPDATE_INTERVAL(this);
24 LOG_SENSOR(" ", "Temperature", this);
25}
26
28 if (this->write(&TMP102_REGISTER_TEMPERATURE, 1) != i2c::ERROR_OK) {
29 this->status_set_warning();
30 return;
31 }
32 this->set_timeout("read_temp", 50, [this]() {
33 int16_t raw_temperature;
34 if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) {
35 this->status_set_warning();
36 return;
37 }
38 raw_temperature = i2c::i2ctohs(raw_temperature);
39 raw_temperature = raw_temperature >> 4;
40 float temperature = raw_temperature * TMP102_CONVERSION_FACTOR;
41 ESP_LOGD(TAG, "Got Temperature=%.1f°C", temperature);
42
43 this->publish_state(temperature);
45 });
46}
47
48} // namespace esphome::tmp102
bool is_failed() const
Definition component.h:272
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
void status_clear_warning()
Definition component.h:289
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:127
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
uint16_t temperature
Definition sun_gtil2.cpp:12