ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
mlx90614.cpp
Go to the documentation of this file.
1#include "mlx90614.h"
2
3#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6
8
9static const uint8_t MLX90614_RAW_IR_1 = 0x04;
10static const uint8_t MLX90614_RAW_IR_2 = 0x05;
11static const uint8_t MLX90614_TEMPERATURE_AMBIENT = 0x06;
12static const uint8_t MLX90614_TEMPERATURE_OBJECT_1 = 0x07;
13static const uint8_t MLX90614_TEMPERATURE_OBJECT_2 = 0x08;
14
15static const uint8_t MLX90614_TOMAX = 0x20;
16static const uint8_t MLX90614_TOMIN = 0x21;
17static const uint8_t MLX90614_PWMCTRL = 0x22;
18static const uint8_t MLX90614_TARANGE = 0x23;
19static const uint8_t MLX90614_EMISSIVITY = 0x24;
20static const uint8_t MLX90614_CONFIG = 0x25;
21static const uint8_t MLX90614_ADDR = 0x2E;
22static const uint8_t MLX90614_ID1 = 0x3C;
23static const uint8_t MLX90614_ID2 = 0x3D;
24static const uint8_t MLX90614_ID3 = 0x3E;
25static const uint8_t MLX90614_ID4 = 0x3F;
26
27static const char *const TAG = "mlx90614";
28
30 if (!this->write_emissivity_()) {
31 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
32 this->mark_failed();
33 return;
34 }
35}
36
38 if (std::isnan(this->emissivity_))
39 return true;
40 uint16_t value = (uint16_t) (this->emissivity_ * 65535);
41 if (!this->write_bytes_(MLX90614_EMISSIVITY, 0)) {
42 return false;
43 }
44 delay(10);
45 if (!this->write_bytes_(MLX90614_EMISSIVITY, value)) {
46 return false;
47 }
48 delay(10);
49 return true;
50}
51
52bool MLX90614Component::write_bytes_(uint8_t reg, uint16_t data) {
53 uint8_t buf[5];
54 buf[0] = this->address_ << 1;
55 buf[1] = reg;
56 buf[2] = data & 0xFF;
57 buf[3] = data >> 8;
58 buf[4] = crc8(buf, 4, 0x00, 0x07, true);
59 return this->write_bytes(reg, buf + 2, 3);
60}
61
63 ESP_LOGCONFIG(TAG, "MLX90614:");
64 LOG_I2C_DEVICE(this);
65 if (this->is_failed()) {
66 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
67 }
68 LOG_UPDATE_INTERVAL(this);
69 LOG_SENSOR(" ", "Ambient", this->ambient_sensor_);
70 LOG_SENSOR(" ", "Object", this->object_sensor_);
71}
72
74 uint8_t emissivity[3];
75 if (this->read_register(MLX90614_EMISSIVITY, emissivity, 3) != i2c::ERROR_OK) {
76 this->status_set_warning();
77 return;
78 }
79 uint8_t raw_object[3];
80 if (this->read_register(MLX90614_TEMPERATURE_OBJECT_1, raw_object, 3) != i2c::ERROR_OK) {
81 this->status_set_warning();
82 return;
83 }
84
85 uint8_t raw_ambient[3];
86 if (this->read_register(MLX90614_TEMPERATURE_AMBIENT, raw_ambient, 3) != i2c::ERROR_OK) {
87 this->status_set_warning();
88 return;
89 }
90
91 float ambient = raw_ambient[1] & 0x80 ? NAN : encode_uint16(raw_ambient[1], raw_ambient[0]) * 0.02f - 273.15f;
92 float object = raw_object[1] & 0x80 ? NAN : encode_uint16(raw_object[1], raw_object[0]) * 0.02f - 273.15f;
93
94 ESP_LOGD(TAG, "Got Temperature=%.1f°C Ambient=%.1f°C", object, ambient);
95
96 if (this->ambient_sensor_ != nullptr && !std::isnan(ambient))
97 this->ambient_sensor_->publish_state(ambient);
98 if (this->object_sensor_ != nullptr && !std::isnan(object))
99 this->object_sensor_->publish_state(object);
100 this->status_clear_warning();
101}
102
103} // namespace esphome::mlx90614
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
void status_clear_warning()
Definition component.h:289
uint8_t address_
store the address of the device on the bus
Definition i2c.h:270
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:152
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
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:251
bool write_bytes_(uint8_t reg, uint16_t data)
Definition mlx90614.cpp:52
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
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
Definition helpers.cpp:59
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:859
void HOT delay(uint32_t ms)
Definition hal.cpp:85