ESPHome 2025.5.0
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
18void TMP102Component::setup() { ESP_LOGCONFIG(TAG, "Setting up TMP102..."); }
19
21 ESP_LOGCONFIG(TAG, "TMP102:");
22 LOG_I2C_DEVICE(this);
23 if (this->is_failed()) {
24 ESP_LOGE(TAG, "Communication with TMP102 failed!");
25 }
26 LOG_UPDATE_INTERVAL(this);
27 LOG_SENSOR(" ", "Temperature", this);
28}
29
31 if (this->write(&TMP102_REGISTER_TEMPERATURE, 1) != i2c::ERROR_OK) {
32 this->status_set_warning();
33 return;
34 }
35 this->set_timeout("read_temp", 50, [this]() {
36 int16_t raw_temperature;
37 if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) {
38 this->status_set_warning();
39 return;
40 }
41 raw_temperature = i2c::i2ctohs(raw_temperature);
42 raw_temperature = raw_temperature >> 4;
43 float temperature = raw_temperature * TMP102_CONVERSION_FACTOR;
44 ESP_LOGD(TAG, "Got Temperature=%.1f°C", temperature);
45
46 this->publish_state(temperature);
48 });
49}
50
52
53} // namespace tmp102
54} // 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:72
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
void update() override
Update the sensor values (temperature)
Definition tmp102.cpp:30
float get_setup_priority() const override
Definition tmp102.cpp:51
void setup() override
Setup (reset) the sensor and check connection.
Definition tmp102.cpp:18
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:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t temperature
Definition sun_gtil2.cpp:12