MLX90393 three-axis magnetometer (#2770)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
functionpointer 2022-02-08 08:42:11 +01:00 committed by GitHub
parent 7ca9245735
commit 397ef72b16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 302 additions and 0 deletions

View file

@ -109,6 +109,7 @@ esphome/components/mdns/* @esphome/core
esphome/components/midea/* @dudanov 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/modbus_controller/* @martgras esphome/components/modbus_controller/* @martgras
esphome/components/modbus_controller/binary_sensor/* @martgras esphome/components/modbus_controller/binary_sensor/* @martgras
esphome/components/modbus_controller/number/* @martgras esphome/components/modbus_controller/number/* @martgras

View file

@ -0,0 +1 @@
CODEOWNERS = ["@functionpointer"]

View file

@ -0,0 +1,135 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import (
CONF_ID,
UNIT_MICROTESLA,
UNIT_CELSIUS,
STATE_CLASS_MEASUREMENT,
ICON_MAGNET,
ICON_THERMOMETER,
CONF_GAIN,
CONF_RESOLUTION,
CONF_OVERSAMPLING,
CONF_FILTER,
CONF_TEMPERATURE,
)
from esphome import pins
CODEOWNERS = ["@functionpointer"]
DEPENDENCIES = ["i2c"]
mlx90393_ns = cg.esphome_ns.namespace("mlx90393")
MLX90393Component = mlx90393_ns.class_(
"MLX90393Cls", cg.PollingComponent, i2c.I2CDevice
)
GAIN = {
"1X": 7,
"1_33X": 6,
"1_67X": 5,
"2X": 4,
"2_5X": 3,
"3X": 2,
"4X": 1,
"5X": 0,
}
RESOLUTION = {
"16BIT": 0,
"17BIT": 1,
"18BIT": 2,
"19BIT": 3,
}
CONF_X_AXIS = "x_axis"
CONF_Y_AXIS = "y_axis"
CONF_Z_AXIS = "z_axis"
CONF_DRDY_PIN = "drdy_pin"
def mlx90393_axis_schema(default_resolution: str):
return sensor.sensor_schema(
unit_of_measurement=UNIT_MICROTESLA,
accuracy_decimals=0,
icon=ICON_MAGNET,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
cv.Schema(
{
cv.Optional(CONF_RESOLUTION, default=default_resolution): cv.enum(
RESOLUTION, upper=True, space="_"
)
}
)
)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(MLX90393Component),
cv.Optional(CONF_GAIN, default="2_5X"): cv.enum(
GAIN, upper=True, space="_"
),
cv.Optional(CONF_DRDY_PIN): pins.gpio_input_pin_schema,
cv.Optional(CONF_OVERSAMPLING, default=2): cv.int_range(min=0, max=3),
cv.Optional(CONF_FILTER, default=6): cv.int_range(min=0, max=7),
cv.Optional(CONF_X_AXIS): mlx90393_axis_schema("19BIT"),
cv.Optional(CONF_Y_AXIS): mlx90393_axis_schema("19BIT"),
cv.Optional(CONF_Z_AXIS): mlx90393_axis_schema("16BIT"),
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
icon=ICON_THERMOMETER,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
cv.Schema(
{
cv.Optional(CONF_OVERSAMPLING, default=0): cv.int_range(
min=0, max=3
),
}
)
),
},
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x0C))
)
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_DRDY_PIN in config:
pin = await cg.gpio_pin_expression(config[CONF_DRDY_PIN])
cg.add(var.set_drdy_pin(pin))
cg.add(var.set_gain(GAIN[config[CONF_GAIN]]))
cg.add(var.set_oversampling(config[CONF_OVERSAMPLING]))
cg.add(var.set_filter(config[CONF_FILTER]))
if CONF_X_AXIS in config:
sens = await sensor.new_sensor(config[CONF_X_AXIS])
cg.add(var.set_x_sensor(sens))
cg.add(var.set_resolution(0, RESOLUTION[config[CONF_X_AXIS][CONF_RESOLUTION]]))
if CONF_Y_AXIS in config:
sens = await sensor.new_sensor(config[CONF_Y_AXIS])
cg.add(var.set_y_sensor(sens))
cg.add(var.set_resolution(1, RESOLUTION[config[CONF_Y_AXIS][CONF_RESOLUTION]]))
if CONF_Z_AXIS in config:
sens = await sensor.new_sensor(config[CONF_Z_AXIS])
cg.add(var.set_z_sensor(sens))
cg.add(var.set_resolution(2, RESOLUTION[config[CONF_Z_AXIS][CONF_RESOLUTION]]))
if CONF_TEMPERATURE in config:
sens = await sensor.new_sensor(config[CONF_TEMPERATURE])
cg.add(var.set_t_sensor(sens))
cg.add(var.set_t_oversampling(config[CONF_TEMPERATURE][CONF_OVERSAMPLING]))
if CONF_DRDY_PIN in config:
pin = await cg.gpio_pin_expression(config[CONF_DRDY_PIN])
cg.add(var.set_drdy_gpio(pin))
cg.add_library("functionpointer/arduino-MLX90393", "1.0.0")

View file

@ -0,0 +1,91 @@
#include "sensor_mlx90393.h"
#include "esphome/core/log.h"
namespace esphome {
namespace mlx90393 {
static const char *const TAG = "mlx90393";
bool MLX90393Cls::transceive(const uint8_t *request, size_t request_size, uint8_t *response, size_t response_size) {
i2c::ErrorCode e = this->write(request, request_size);
if (e != i2c::ErrorCode::ERROR_OK) {
return false;
}
e = this->read(response, response_size);
return e == i2c::ErrorCode::ERROR_OK;
}
bool MLX90393Cls::has_drdy_pin() { return this->drdy_pin_ != nullptr; }
bool MLX90393Cls::read_drdy_pin() {
if (this->drdy_pin_ == nullptr) {
return false;
} else {
return this->drdy_pin_->digital_read();
}
}
void MLX90393Cls::sleep_millis(uint32_t millis) { delay(millis); }
void MLX90393Cls::sleep_micros(uint32_t micros) { delayMicroseconds(micros); }
void MLX90393Cls::setup() {
ESP_LOGCONFIG(TAG, "Setting up MLX90393...");
// note the two arguments A0 and A1 which are used to construct an i2c address
// we can hard-code these because we never actually use the constructed address
// see the transceive function above, which uses the address from I2CComponent
this->mlx_.begin_with_hal(this, 0, 0);
this->mlx_.setGainSel(this->gain_);
this->mlx_.setResolution(this->resolutions_[0], this->resolutions_[1], this->resolutions_[2]);
this->mlx_.setOverSampling(this->oversampling_);
this->mlx_.setDigitalFiltering(this->filter_);
this->mlx_.setTemperatureOverSampling(this->temperature_oversampling_);
}
void MLX90393Cls::dump_config() {
ESP_LOGCONFIG(TAG, "MLX90393:");
LOG_I2C_DEVICE(this);
if (this->is_failed()) {
ESP_LOGE(TAG, "Communication with MLX90393 failed!");
return;
}
LOG_UPDATE_INTERVAL(this);
LOG_SENSOR(" ", "X Axis", this->x_sensor_);
LOG_SENSOR(" ", "Y Axis", this->y_sensor_);
LOG_SENSOR(" ", "Z Axis", this->z_sensor_);
LOG_SENSOR(" ", "Temperature", this->t_sensor_);
}
float MLX90393Cls::get_setup_priority() const { return setup_priority::DATA; }
void MLX90393Cls::update() {
MLX90393::txyz data;
if (this->mlx_.readData(data) == MLX90393::STATUS_OK) {
ESP_LOGD(TAG, "received %f %f %f", data.x, data.y, data.z);
if (this->x_sensor_ != nullptr) {
this->x_sensor_->publish_state(data.x);
}
if (this->y_sensor_ != nullptr) {
this->y_sensor_->publish_state(data.y);
}
if (this->z_sensor_ != nullptr) {
this->z_sensor_->publish_state(data.z);
}
if (this->t_sensor_ != nullptr) {
this->t_sensor_->publish_state(data.t);
}
this->status_clear_warning();
} else {
ESP_LOGE(TAG, "failed to read data");
this->status_set_warning();
}
}
} // namespace mlx90393
} // namespace esphome

View file

@ -0,0 +1,59 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
#include "esphome/core/hal.h"
#include <MLX90393.h>
#include <MLX90393Hal.h>
namespace esphome {
namespace mlx90393 {
class MLX90393Cls : public PollingComponent, public i2c::I2CDevice, public MLX90393Hal {
public:
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
void update() override;
void set_drdy_gpio(GPIOPin *pin) { drdy_pin_ = pin; }
void set_x_sensor(sensor::Sensor *x_sensor) { x_sensor_ = x_sensor; }
void set_y_sensor(sensor::Sensor *y_sensor) { y_sensor_ = y_sensor; }
void set_z_sensor(sensor::Sensor *z_sensor) { z_sensor_ = z_sensor; }
void set_t_sensor(sensor::Sensor *t_sensor) { t_sensor_ = t_sensor; }
void set_oversampling(uint8_t osr) { oversampling_ = osr; }
void set_t_oversampling(uint8_t osr2) { temperature_oversampling_ = osr2; }
void set_resolution(uint8_t xyz, uint8_t res) { resolutions_[xyz] = res; }
void set_filter(uint8_t filter) { filter_ = filter; }
void set_gain(uint8_t gain_sel) { gain_ = gain_sel; }
// overrides for MLX library
// disable lint because it keeps suggesting const uint8_t *response.
// this->read() writes data into response, so it can't be const
bool transceive(const uint8_t *request, size_t request_size, uint8_t *response,
size_t response_size) override; // NOLINT
bool has_drdy_pin() override;
bool read_drdy_pin() override;
void sleep_millis(uint32_t millis) override;
void sleep_micros(uint32_t micros) override;
protected:
MLX90393 mlx_;
sensor::Sensor *x_sensor_{nullptr};
sensor::Sensor *y_sensor_{nullptr};
sensor::Sensor *z_sensor_{nullptr};
sensor::Sensor *t_sensor_{nullptr};
uint8_t gain_;
uint8_t oversampling_;
uint8_t temperature_oversampling_ = 0;
uint8_t filter_;
uint8_t resolutions_[3] = {0};
GPIOPin *drdy_pin_ = nullptr;
};
} // namespace mlx90393
} // namespace esphome

View file

@ -38,6 +38,7 @@ lib_deps =
esphome/Improv@1.2.1 ; improv_serial / esp32_improv esphome/Improv@1.2.1 ; improv_serial / esp32_improv
bblanchon/ArduinoJson@6.18.5 ; json bblanchon/ArduinoJson@6.18.5 ; json
wjtje/qr-code-generator-library@1.7.0 ; qr_code wjtje/qr-code-generator-library@1.7.0 ; qr_code
functionpointer/arduino-MLX90393@1.0.0 ; mlx90393
build_flags = build_flags =
-DESPHOME_LOG_LEVEL=ESPHOME_LOG_LEVEL_VERY_VERBOSE -DESPHOME_LOG_LEVEL=ESPHOME_LOG_LEVEL_VERY_VERBOSE
src_filter = src_filter =

View file

@ -692,6 +692,20 @@ sensor:
name: 'testwave' name: 'testwave'
component_id: 2 component_id: 2
wave_channel_id: 1 wave_channel_id: 1
- platform: mlx90393
oversampling: 1
filter: 0
gain: "3X"
x_axis:
name: "mlxxaxis"
y_axis:
name: "mlxyaxis"
z_axis:
name: "mlxzaxis"
resolution: 17BIT
temperature:
name: "mlxtemp"
oversampling: 2
time: time:
- platform: homeassistant - platform: homeassistant