ESPHome  2024.10.2
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 
5 namespace esphome {
6 namespace tmp102 {
7 
8 static const char *const TAG = "tmp102";
9 
10 static const uint8_t TMP102_ADDRESS = 0x48;
11 static const uint8_t TMP102_REGISTER_TEMPERATURE = 0x00;
12 static const uint8_t TMP102_REGISTER_CONFIGURATION = 0x01;
13 static const uint8_t TMP102_REGISTER_LOW_LIMIT = 0x02;
14 static const uint8_t TMP102_REGISTER_HIGH_LIMIT = 0x03;
15 
16 static const float TMP102_CONVERSION_FACTOR = 0.0625;
17 
18 void 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);
47  this->status_clear_warning();
48  });
49 }
50 
52 
53 } // namespace tmp102
54 } // namespace esphome
void dump_config() override
Definition: tmp102.cpp:20
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
uint16_t i2ctohs(uint16_t i2cshort)
Definition: i2c.h:128
void update() override
Update the sensor values (temperature)
Definition: tmp102.cpp:30
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
float get_setup_priority() const override
Definition: tmp102.cpp:51
bool is_failed() const
Definition: component.cpp:143
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition: i2c.h:160
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:69
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:186
No error found during execution of method.
Definition: i2c_bus.h:13
void status_clear_warning()
Definition: component.cpp:166
void setup() override
Setup (reset) the sensor and check connection.
Definition: tmp102.cpp:18
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint16_t temperature
Definition: sun_gtil2.cpp:26
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7