ESPHome 2025.6.3
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 {
6namespace tmp102 {
7
8static const char *const TAG = "tmp102";
9
10static const uint8_t TMP102_ADDRESS = 0x48;
11static const uint8_t TMP102_REGISTER_TEMPERATURE = 0x00;
12static const uint8_t TMP102_REGISTER_CONFIGURATION = 0x01;
13static const uint8_t TMP102_REGISTER_LOW_LIMIT = 0x02;
14static const uint8_t TMP102_REGISTER_HIGH_LIMIT = 0x03;
15
16static const float TMP102_CONVERSION_FACTOR = 0.0625;
17
19 ESP_LOGCONFIG(TAG, "TMP102:");
20 LOG_I2C_DEVICE(this);
21 if (this->is_failed()) {
22 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
23 }
24 LOG_UPDATE_INTERVAL(this);
25 LOG_SENSOR(" ", "Temperature", this);
26}
27
29 if (this->write(&TMP102_REGISTER_TEMPERATURE, 1) != i2c::ERROR_OK) {
30 this->status_set_warning();
31 return;
32 }
33 this->set_timeout("read_temp", 50, [this]() {
34 int16_t raw_temperature;
35 if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) {
36 this->status_set_warning();
37 return;
38 }
39 raw_temperature = i2c::i2ctohs(raw_temperature);
40 raw_temperature = raw_temperature >> 4;
41 float temperature = raw_temperature * TMP102_CONVERSION_FACTOR;
42 ESP_LOGD(TAG, "Got Temperature=%.1f°C", temperature);
43
44 this->publish_state(temperature);
46 });
47}
48
50
51} // namespace tmp102
52} // namespace esphome
bool is_failed() 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:75
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition i2c.h:190
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
float get_setup_priority() const override
Definition tmp102.cpp:49
uint16_t i2ctohs(uint16_t i2cshort)
Definition i2c.h:128
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t temperature
Definition sun_gtil2.cpp:12