ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
lm75b.cpp
Go to the documentation of this file.
1#include "lm75b.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome::lm75b {
6
7static const char *const TAG = "lm75b";
8
10 ESP_LOGCONFIG(TAG, "LM75B:");
11 LOG_I2C_DEVICE(this);
12 if (this->is_failed()) {
13 ESP_LOGE(TAG, "Setting up LM75B failed!");
14 }
15 LOG_UPDATE_INTERVAL(this);
16 LOG_SENSOR(" ", "Temperature", this);
17}
18
20 // Create a temporary buffer
21 uint8_t buff[2];
22 if (this->read_register(LM75B_REG_TEMPERATURE, buff, 2) != i2c::ERROR_OK) {
23 this->status_set_warning();
24 return;
25 }
26 // Obtain combined 16-bit value
27 int16_t raw_temperature = (buff[0] << 8) | buff[1];
28 // Read the 11-bit raw temperature value
29 raw_temperature >>= 5;
30 // Publish the temperature in °C
31 this->publish_state(raw_temperature * 0.125);
32 if (this->status_has_warning()) {
34 }
35}
36
37} // namespace esphome::lm75b
bool is_failed() const
Definition component.h:272
bool status_has_warning() const
Definition component.h:278
void status_clear_warning()
Definition component.h:289
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:25
void dump_config() override
Definition lm75b.cpp:9
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14