mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-04 09:01:49 +00:00 
			
		
		
		
	Add mlx90614 sensors (#3749)
Co-authored-by: Greg Arnold <greg@arnoldassociates.com> Co-authored-by: notsonominal <130870838+notsonominal@users.noreply.github.com>
This commit is contained in:
		@@ -162,6 +162,7 @@ esphome/components/midea/* @dudanov
 | 
				
			|||||||
esphome/components/midea_ir/* @dudanov
 | 
					esphome/components/midea_ir/* @dudanov
 | 
				
			||||||
esphome/components/mitsubishi/* @RubyBailey
 | 
					esphome/components/mitsubishi/* @RubyBailey
 | 
				
			||||||
esphome/components/mlx90393/* @functionpointer
 | 
					esphome/components/mlx90393/* @functionpointer
 | 
				
			||||||
 | 
					esphome/components/mlx90614/* @jesserockz
 | 
				
			||||||
esphome/components/mmc5603/* @benhoff
 | 
					esphome/components/mmc5603/* @benhoff
 | 
				
			||||||
esphome/components/modbus_controller/* @martgras
 | 
					esphome/components/modbus_controller/* @martgras
 | 
				
			||||||
esphome/components/modbus_controller/binary_sensor/* @martgras
 | 
					esphome/components/modbus_controller/binary_sensor/* @martgras
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										0
									
								
								esphome/components/mlx90614/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								esphome/components/mlx90614/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										122
									
								
								esphome/components/mlx90614/mlx90614.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										122
									
								
								esphome/components/mlx90614/mlx90614.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,122 @@
 | 
				
			|||||||
 | 
					#include "mlx90614.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "esphome/core/hal.h"
 | 
				
			||||||
 | 
					#include "esphome/core/log.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace esphome {
 | 
				
			||||||
 | 
					namespace mlx90614 {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_RAW_IR_1 = 0x04;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_RAW_IR_2 = 0x05;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_TEMPERATURE_AMBIENT = 0x06;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_TEMPERATURE_OBJECT_1 = 0x07;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_TEMPERATURE_OBJECT_2 = 0x08;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_TOMAX = 0x20;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_TOMIN = 0x21;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_PWMCTRL = 0x22;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_TARANGE = 0x23;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_EMISSIVITY = 0x24;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_CONFIG = 0x25;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_ADDR = 0x2E;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_ID1 = 0x3C;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_ID2 = 0x3D;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_ID3 = 0x3E;
 | 
				
			||||||
 | 
					static const uint8_t MLX90614_ID4 = 0x3F;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const char *const TAG = "mlx90614";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void MLX90614Component::setup() {
 | 
				
			||||||
 | 
					  ESP_LOGCONFIG(TAG, "Setting up MLX90614...");
 | 
				
			||||||
 | 
					  if (!this->write_emissivity_()) {
 | 
				
			||||||
 | 
					    ESP_LOGE(TAG, "Communication with MLX90614 failed!");
 | 
				
			||||||
 | 
					    this->mark_failed();
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool MLX90614Component::write_emissivity_() {
 | 
				
			||||||
 | 
					  if (std::isnan(this->emissivity_))
 | 
				
			||||||
 | 
					    return true;
 | 
				
			||||||
 | 
					  uint16_t value = (uint16_t) (this->emissivity_ * 65535);
 | 
				
			||||||
 | 
					  if (!this->write_bytes_(MLX90614_EMISSIVITY, 0)) {
 | 
				
			||||||
 | 
					    return false;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  delay(10);
 | 
				
			||||||
 | 
					  if (!this->write_bytes_(MLX90614_EMISSIVITY, value)) {
 | 
				
			||||||
 | 
					    return false;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  delay(10);
 | 
				
			||||||
 | 
					  return true;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					uint8_t MLX90614Component::crc8_pec_(const uint8_t *data, uint8_t len) {
 | 
				
			||||||
 | 
					  uint8_t crc = 0;
 | 
				
			||||||
 | 
					  for (uint8_t i = 0; i < len; i++) {
 | 
				
			||||||
 | 
					    uint8_t in = data[i];
 | 
				
			||||||
 | 
					    for (uint8_t j = 0; j < 8; j++) {
 | 
				
			||||||
 | 
					      bool carry = (crc ^ in) & 0x80;
 | 
				
			||||||
 | 
					      crc <<= 1;
 | 
				
			||||||
 | 
					      if (carry)
 | 
				
			||||||
 | 
					        crc ^= 0x07;
 | 
				
			||||||
 | 
					      in <<= 1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  return crc;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool MLX90614Component::write_bytes_(uint8_t reg, uint16_t data) {
 | 
				
			||||||
 | 
					  uint8_t buf[5];
 | 
				
			||||||
 | 
					  buf[0] = this->address_ << 1;
 | 
				
			||||||
 | 
					  buf[1] = reg;
 | 
				
			||||||
 | 
					  buf[2] = data & 0xFF;
 | 
				
			||||||
 | 
					  buf[3] = data >> 8;
 | 
				
			||||||
 | 
					  buf[4] = this->crc8_pec_(buf, 4);
 | 
				
			||||||
 | 
					  return this->write_bytes(reg, buf + 2, 3);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void MLX90614Component::dump_config() {
 | 
				
			||||||
 | 
					  ESP_LOGCONFIG(TAG, "MLX90614:");
 | 
				
			||||||
 | 
					  LOG_I2C_DEVICE(this);
 | 
				
			||||||
 | 
					  if (this->is_failed()) {
 | 
				
			||||||
 | 
					    ESP_LOGE(TAG, "Communication with MLX90614 failed!");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  LOG_UPDATE_INTERVAL(this);
 | 
				
			||||||
 | 
					  LOG_SENSOR("  ", "Ambient", this->ambient_sensor_);
 | 
				
			||||||
 | 
					  LOG_SENSOR("  ", "Object", this->object_sensor_);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					float MLX90614Component::get_setup_priority() const { return setup_priority::DATA; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void MLX90614Component::update() {
 | 
				
			||||||
 | 
					  uint8_t emissivity[3];
 | 
				
			||||||
 | 
					  if (this->read_register(MLX90614_EMISSIVITY, emissivity, 3, false) != i2c::ERROR_OK) {
 | 
				
			||||||
 | 
					    this->status_set_warning();
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  uint8_t raw_object[3];
 | 
				
			||||||
 | 
					  if (this->read_register(MLX90614_TEMPERATURE_OBJECT_1, raw_object, 3, false) != i2c::ERROR_OK) {
 | 
				
			||||||
 | 
					    this->status_set_warning();
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  uint8_t raw_ambient[3];
 | 
				
			||||||
 | 
					  if (this->read_register(MLX90614_TEMPERATURE_AMBIENT, raw_ambient, 3, false) != i2c::ERROR_OK) {
 | 
				
			||||||
 | 
					    this->status_set_warning();
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  float ambient = raw_ambient[1] & 0x80 ? NAN : encode_uint16(raw_ambient[1], raw_ambient[0]) * 0.02f - 273.15f;
 | 
				
			||||||
 | 
					  float object = raw_object[1] & 0x80 ? NAN : encode_uint16(raw_object[1], raw_object[0]) * 0.02f - 273.15f;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  ESP_LOGD(TAG, "Got Temperature=%.1f°C Ambient=%.1f°C", object, ambient);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (this->ambient_sensor_ != nullptr && !std::isnan(ambient))
 | 
				
			||||||
 | 
					    this->ambient_sensor_->publish_state(ambient);
 | 
				
			||||||
 | 
					  if (this->object_sensor_ != nullptr && !std::isnan(object))
 | 
				
			||||||
 | 
					    this->object_sensor_->publish_state(object);
 | 
				
			||||||
 | 
					  this->status_clear_warning();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}  // namespace mlx90614
 | 
				
			||||||
 | 
					}  // namespace esphome
 | 
				
			||||||
							
								
								
									
										34
									
								
								esphome/components/mlx90614/mlx90614.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								esphome/components/mlx90614/mlx90614.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
				
			|||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "esphome/components/i2c/i2c.h"
 | 
				
			||||||
 | 
					#include "esphome/components/sensor/sensor.h"
 | 
				
			||||||
 | 
					#include "esphome/core/component.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace esphome {
 | 
				
			||||||
 | 
					namespace mlx90614 {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class MLX90614Component : public PollingComponent, public i2c::I2CDevice {
 | 
				
			||||||
 | 
					 public:
 | 
				
			||||||
 | 
					  void setup() override;
 | 
				
			||||||
 | 
					  void dump_config() override;
 | 
				
			||||||
 | 
					  void update() override;
 | 
				
			||||||
 | 
					  float get_setup_priority() const override;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  void set_ambient_sensor(sensor::Sensor *ambient_sensor) { ambient_sensor_ = ambient_sensor; }
 | 
				
			||||||
 | 
					  void set_object_sensor(sensor::Sensor *object_sensor) { object_sensor_ = object_sensor; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  void set_emissivity(float emissivity) { emissivity_ = emissivity; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 protected:
 | 
				
			||||||
 | 
					  bool write_emissivity_();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  uint8_t crc8_pec_(const uint8_t *data, uint8_t len);
 | 
				
			||||||
 | 
					  bool write_bytes_(uint8_t reg, uint16_t data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  sensor::Sensor *ambient_sensor_{nullptr};
 | 
				
			||||||
 | 
					  sensor::Sensor *object_sensor_{nullptr};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  float emissivity_{NAN};
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					}  // namespace mlx90614
 | 
				
			||||||
 | 
					}  // namespace esphome
 | 
				
			||||||
							
								
								
									
										63
									
								
								esphome/components/mlx90614/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								esphome/components/mlx90614/sensor.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,63 @@
 | 
				
			|||||||
 | 
					import esphome.codegen as cg
 | 
				
			||||||
 | 
					import esphome.config_validation as cv
 | 
				
			||||||
 | 
					from esphome.components import i2c, sensor
 | 
				
			||||||
 | 
					from esphome.const import (
 | 
				
			||||||
 | 
					    CONF_ID,
 | 
				
			||||||
 | 
					    DEVICE_CLASS_TEMPERATURE,
 | 
				
			||||||
 | 
					    STATE_CLASS_MEASUREMENT,
 | 
				
			||||||
 | 
					    UNIT_CELSIUS,
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CODEOWNERS = ["@jesserockz"]
 | 
				
			||||||
 | 
					DEPENDENCIES = ["i2c"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CONF_AMBIENT = "ambient"
 | 
				
			||||||
 | 
					CONF_EMISSIVITY = "emissivity"
 | 
				
			||||||
 | 
					CONF_OBJECT = "object"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					mlx90614_ns = cg.esphome_ns.namespace("mlx90614")
 | 
				
			||||||
 | 
					MLX90614Component = mlx90614_ns.class_(
 | 
				
			||||||
 | 
					    "MLX90614Component", cg.PollingComponent, i2c.I2CDevice
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CONFIG_SCHEMA = (
 | 
				
			||||||
 | 
					    cv.Schema(
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            cv.GenerateID(): cv.declare_id(MLX90614Component),
 | 
				
			||||||
 | 
					            cv.Optional(CONF_AMBIENT): sensor.sensor_schema(
 | 
				
			||||||
 | 
					                unit_of_measurement=UNIT_CELSIUS,
 | 
				
			||||||
 | 
					                accuracy_decimals=1,
 | 
				
			||||||
 | 
					                device_class=DEVICE_CLASS_TEMPERATURE,
 | 
				
			||||||
 | 
					                state_class=STATE_CLASS_MEASUREMENT,
 | 
				
			||||||
 | 
					            ),
 | 
				
			||||||
 | 
					            cv.Optional(CONF_OBJECT): sensor.sensor_schema(
 | 
				
			||||||
 | 
					                unit_of_measurement=UNIT_CELSIUS,
 | 
				
			||||||
 | 
					                accuracy_decimals=1,
 | 
				
			||||||
 | 
					                device_class=DEVICE_CLASS_TEMPERATURE,
 | 
				
			||||||
 | 
					                state_class=STATE_CLASS_MEASUREMENT,
 | 
				
			||||||
 | 
					            ).extend(
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    cv.Optional(CONF_EMISSIVITY, default=1.0): cv.percentage,
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            ),
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    .extend(cv.polling_component_schema("60s"))
 | 
				
			||||||
 | 
					    .extend(i2c.i2c_device_schema(0x5A))
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async def to_code(config):
 | 
				
			||||||
 | 
					    var = cg.new_Pvariable(config[CONF_ID])
 | 
				
			||||||
 | 
					    await cg.register_component(var, config)
 | 
				
			||||||
 | 
					    await i2c.register_i2c_device(var, config)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if CONF_AMBIENT in config:
 | 
				
			||||||
 | 
					        sens = await sensor.new_sensor(config[CONF_AMBIENT])
 | 
				
			||||||
 | 
					        cg.add(var.set_ambient_sensor(sens))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if CONF_OBJECT in config:
 | 
				
			||||||
 | 
					        sens = await sensor.new_sensor(config[CONF_OBJECT])
 | 
				
			||||||
 | 
					        cg.add(var.set_object_sensor(sens))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        cg.add(var.set_emissivity(config[CONF_OBJECT][CONF_EMISSIVITY]))
 | 
				
			||||||
@@ -1239,6 +1239,13 @@ sensor:
 | 
				
			|||||||
    temperature:
 | 
					    temperature:
 | 
				
			||||||
      name: Max9611 Temp
 | 
					      name: Max9611 Temp
 | 
				
			||||||
    update_interval: 1s
 | 
					    update_interval: 1s
 | 
				
			||||||
 | 
					  - platform: mlx90614
 | 
				
			||||||
 | 
					    i2c_id: i2c_bus
 | 
				
			||||||
 | 
					    ambient:
 | 
				
			||||||
 | 
					      name: Ambient
 | 
				
			||||||
 | 
					    object:
 | 
				
			||||||
 | 
					      name: Object
 | 
				
			||||||
 | 
					      emissivity: 1.0
 | 
				
			||||||
  - platform: mpl3115a2
 | 
					  - platform: mpl3115a2
 | 
				
			||||||
    i2c_id: i2c_bus
 | 
					    i2c_id: i2c_bus
 | 
				
			||||||
    temperature:
 | 
					    temperature:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user