ESPHome  2022.6.3
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  uint16_t raw_temperature;
32  if (this->write(&TMP102_REGISTER_TEMPERATURE, 1) != i2c::ERROR_OK) {
33  this->status_set_warning();
34  return;
35  }
36  delay(50); // NOLINT
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 
43  raw_temperature = raw_temperature >> 4;
44  float temperature = raw_temperature * TMP102_CONVERSION_FACTOR;
45  ESP_LOGD(TAG, "Got Temperature=%.1f°C", temperature);
46 
47  this->publish_state(temperature);
48  this->status_clear_warning();
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:18
uint16_t i2ctohs(uint16_t i2cshort)
Definition: i2c.h:36
void update() override
Update the sensor values (temperature)
Definition: tmp102.cpp:30
float get_setup_priority() const override
Definition: tmp102.cpp:51
ErrorCode read(uint8_t *data, size_t len)
Definition: i2c.h:48
float temperature
Definition: qmp6988.h:71
void status_clear_warning()
Definition: component.cpp:148
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:70
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:56
void status_set_warning()
Definition: component.cpp:140
Definition: a4988.cpp:4
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27