mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 08:28:12 +01:00
Add support for MPL3115A2 Pressure/Altitude and Temperature Sensor (#3371)
* Add support for mpl3115a2 * Add codeowner * Linter/test updates * Minor changes * Made pressure/altitude exclusive Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
e3f2562047
commit
d66b2a1778
6 changed files with 290 additions and 0 deletions
|
@ -143,6 +143,7 @@ esphome/components/modbus_controller/switch/* @martgras
|
||||||
esphome/components/modbus_controller/text_sensor/* @martgras
|
esphome/components/modbus_controller/text_sensor/* @martgras
|
||||||
esphome/components/mopeka_ble/* @spbrogan
|
esphome/components/mopeka_ble/* @spbrogan
|
||||||
esphome/components/mopeka_pro_check/* @spbrogan
|
esphome/components/mopeka_pro_check/* @spbrogan
|
||||||
|
esphome/components/mpl3115a2/* @kbickar
|
||||||
esphome/components/mpu6886/* @fabaff
|
esphome/components/mpu6886/* @fabaff
|
||||||
esphome/components/network/* @esphome/core
|
esphome/components/network/* @esphome/core
|
||||||
esphome/components/nextion/* @senexcrenshaw
|
esphome/components/nextion/* @senexcrenshaw
|
||||||
|
|
0
esphome/components/mpl3115a2/__init__.py
Normal file
0
esphome/components/mpl3115a2/__init__.py
Normal file
99
esphome/components/mpl3115a2/mpl3115a2.cpp
Normal file
99
esphome/components/mpl3115a2/mpl3115a2.cpp
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
#include "mpl3115a2.h"
|
||||||
|
#include "esphome/core/hal.h"
|
||||||
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace mpl3115a2 {
|
||||||
|
|
||||||
|
static const char *const TAG = "mpl3115a2";
|
||||||
|
|
||||||
|
void MPL3115A2Component::setup() {
|
||||||
|
ESP_LOGCONFIG(TAG, "Setting up MPL3115A2...");
|
||||||
|
|
||||||
|
uint8_t whoami = 0xFF;
|
||||||
|
if (!this->read_byte(MPL3115A2_WHOAMI, &whoami, false)) {
|
||||||
|
this->error_code_ = COMMUNICATION_FAILED;
|
||||||
|
this->mark_failed();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (whoami != 0xC4) {
|
||||||
|
this->error_code_ = WRONG_ID;
|
||||||
|
this->mark_failed();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// reset
|
||||||
|
this->write_byte(MPL3115A2_CTRL_REG1, MPL3115A2_CTRL_REG1_RST);
|
||||||
|
delay(15);
|
||||||
|
|
||||||
|
// enable data ready events for pressure/altitude and temperature
|
||||||
|
this->write_byte(MPL3115A2_PT_DATA_CFG,
|
||||||
|
MPL3115A2_PT_DATA_CFG_TDEFE | MPL3115A2_PT_DATA_CFG_PDEFE | MPL3115A2_PT_DATA_CFG_DREM);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MPL3115A2Component::dump_config() {
|
||||||
|
ESP_LOGCONFIG(TAG, "MPL3115A2:");
|
||||||
|
LOG_I2C_DEVICE(this);
|
||||||
|
if (this->is_failed()) {
|
||||||
|
switch (this->error_code_) {
|
||||||
|
case COMMUNICATION_FAILED:
|
||||||
|
ESP_LOGE(TAG, "Communication with MPL3115A2 failed!");
|
||||||
|
break;
|
||||||
|
case WRONG_ID:
|
||||||
|
ESP_LOGE(TAG, "MPL3115A2 has invalid id");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ESP_LOGE(TAG, "Setting up MPL3115A2 registers failed!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LOG_UPDATE_INTERVAL(this);
|
||||||
|
LOG_SENSOR(" ", "Temperature", this->temperature_);
|
||||||
|
LOG_SENSOR(" ", "Pressure", this->pressure_);
|
||||||
|
LOG_SENSOR(" ", "Altitude", this->altitude_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MPL3115A2Component::update() {
|
||||||
|
uint8_t mode = MPL3115A2_CTRL_REG1_OS128;
|
||||||
|
this->write_byte(MPL3115A2_CTRL_REG1, mode, true);
|
||||||
|
// Trigger a new reading
|
||||||
|
mode |= MPL3115A2_CTRL_REG1_OST;
|
||||||
|
if (this->altitude_ != nullptr)
|
||||||
|
mode |= MPL3115A2_CTRL_REG1_ALT;
|
||||||
|
this->write_byte(MPL3115A2_CTRL_REG1, mode, true);
|
||||||
|
|
||||||
|
// Wait until status shows reading available
|
||||||
|
uint8_t status = 0;
|
||||||
|
if (!this->read_byte(MPL3115A2_REGISTER_STATUS, &status, false) || (status & MPL3115A2_REGISTER_STATUS_PDR) == 0) {
|
||||||
|
delay(10);
|
||||||
|
if (!this->read_byte(MPL3115A2_REGISTER_STATUS, &status, false) || (status & MPL3115A2_REGISTER_STATUS_PDR) == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t buffer[5] = {0, 0, 0, 0, 0};
|
||||||
|
this->read_register(MPL3115A2_REGISTER_PRESSURE_MSB, buffer, 5, false);
|
||||||
|
|
||||||
|
float altitude = 0, pressure = 0;
|
||||||
|
if (this->altitude_ != nullptr) {
|
||||||
|
int32_t alt = encode_uint32(buffer[0], buffer[1], buffer[2], 0);
|
||||||
|
altitude = float(alt) / 65536.0;
|
||||||
|
this->altitude_->publish_state(altitude);
|
||||||
|
} else {
|
||||||
|
uint32_t p = encode_uint32(0, buffer[0], buffer[1], buffer[2]);
|
||||||
|
pressure = float(p) / 6400.0;
|
||||||
|
if (this->pressure_ != nullptr)
|
||||||
|
this->pressure_->publish_state(pressure);
|
||||||
|
}
|
||||||
|
int16_t t = encode_uint16(buffer[3], buffer[4]);
|
||||||
|
float temperature = float(t) / 256.0;
|
||||||
|
if (this->temperature_ != nullptr)
|
||||||
|
this->temperature_->publish_state(temperature);
|
||||||
|
|
||||||
|
ESP_LOGD(TAG, "Got Temperature=%.1f°C Altitude=%.1f Pressure=%.1f", temperature, altitude, pressure);
|
||||||
|
|
||||||
|
this->status_clear_warning();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace mpl3115a2
|
||||||
|
} // namespace esphome
|
108
esphome/components/mpl3115a2/mpl3115a2.h
Normal file
108
esphome/components/mpl3115a2/mpl3115a2.h
Normal file
|
@ -0,0 +1,108 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "esphome/core/component.h"
|
||||||
|
#include "esphome/components/sensor/sensor.h"
|
||||||
|
#include "esphome/components/i2c/i2c.h"
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace mpl3115a2 {
|
||||||
|
|
||||||
|
// enums from https://github.com/adafruit/Adafruit_MPL3115A2_Library/
|
||||||
|
/** MPL3115A2 registers **/
|
||||||
|
enum {
|
||||||
|
MPL3115A2_REGISTER_STATUS = (0x00),
|
||||||
|
|
||||||
|
MPL3115A2_REGISTER_PRESSURE_MSB = (0x01),
|
||||||
|
MPL3115A2_REGISTER_PRESSURE_CSB = (0x02),
|
||||||
|
MPL3115A2_REGISTER_PRESSURE_LSB = (0x03),
|
||||||
|
|
||||||
|
MPL3115A2_REGISTER_TEMP_MSB = (0x04),
|
||||||
|
MPL3115A2_REGISTER_TEMP_LSB = (0x05),
|
||||||
|
|
||||||
|
MPL3115A2_REGISTER_DR_STATUS = (0x06),
|
||||||
|
|
||||||
|
MPL3115A2_OUT_P_DELTA_MSB = (0x07),
|
||||||
|
MPL3115A2_OUT_P_DELTA_CSB = (0x08),
|
||||||
|
MPL3115A2_OUT_P_DELTA_LSB = (0x09),
|
||||||
|
|
||||||
|
MPL3115A2_OUT_T_DELTA_MSB = (0x0A),
|
||||||
|
MPL3115A2_OUT_T_DELTA_LSB = (0x0B),
|
||||||
|
|
||||||
|
MPL3115A2_WHOAMI = (0x0C),
|
||||||
|
|
||||||
|
MPL3115A2_BAR_IN_MSB = (0x14),
|
||||||
|
MPL3115A2_BAR_IN_LSB = (0x15),
|
||||||
|
};
|
||||||
|
|
||||||
|
/** MPL3115A2 status register bits **/
|
||||||
|
enum {
|
||||||
|
MPL3115A2_REGISTER_STATUS_TDR = 0x02,
|
||||||
|
MPL3115A2_REGISTER_STATUS_PDR = 0x04,
|
||||||
|
MPL3115A2_REGISTER_STATUS_PTDR = 0x08,
|
||||||
|
};
|
||||||
|
|
||||||
|
/** MPL3115A2 PT DATA register bits **/
|
||||||
|
enum {
|
||||||
|
MPL3115A2_PT_DATA_CFG = 0x13,
|
||||||
|
MPL3115A2_PT_DATA_CFG_TDEFE = 0x01,
|
||||||
|
MPL3115A2_PT_DATA_CFG_PDEFE = 0x02,
|
||||||
|
MPL3115A2_PT_DATA_CFG_DREM = 0x04,
|
||||||
|
};
|
||||||
|
|
||||||
|
/** MPL3115A2 control registers **/
|
||||||
|
enum {
|
||||||
|
|
||||||
|
MPL3115A2_CTRL_REG1 = (0x26),
|
||||||
|
MPL3115A2_CTRL_REG2 = (0x27),
|
||||||
|
MPL3115A2_CTRL_REG3 = (0x28),
|
||||||
|
MPL3115A2_CTRL_REG4 = (0x29),
|
||||||
|
MPL3115A2_CTRL_REG5 = (0x2A),
|
||||||
|
};
|
||||||
|
|
||||||
|
/** MPL3115A2 control register bits **/
|
||||||
|
enum {
|
||||||
|
MPL3115A2_CTRL_REG1_SBYB = 0x01,
|
||||||
|
MPL3115A2_CTRL_REG1_OST = 0x02,
|
||||||
|
MPL3115A2_CTRL_REG1_RST = 0x04,
|
||||||
|
MPL3115A2_CTRL_REG1_RAW = 0x40,
|
||||||
|
MPL3115A2_CTRL_REG1_ALT = 0x80,
|
||||||
|
MPL3115A2_CTRL_REG1_BAR = 0x00,
|
||||||
|
};
|
||||||
|
|
||||||
|
/** MPL3115A2 oversample values **/
|
||||||
|
enum {
|
||||||
|
MPL3115A2_CTRL_REG1_OS1 = 0x00,
|
||||||
|
MPL3115A2_CTRL_REG1_OS2 = 0x08,
|
||||||
|
MPL3115A2_CTRL_REG1_OS4 = 0x10,
|
||||||
|
MPL3115A2_CTRL_REG1_OS8 = 0x18,
|
||||||
|
MPL3115A2_CTRL_REG1_OS16 = 0x20,
|
||||||
|
MPL3115A2_CTRL_REG1_OS32 = 0x28,
|
||||||
|
MPL3115A2_CTRL_REG1_OS64 = 0x30,
|
||||||
|
MPL3115A2_CTRL_REG1_OS128 = 0x38,
|
||||||
|
};
|
||||||
|
|
||||||
|
class MPL3115A2Component : public PollingComponent, public i2c::I2CDevice {
|
||||||
|
public:
|
||||||
|
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }
|
||||||
|
void set_altitude(sensor::Sensor *altitude) { altitude_ = altitude; }
|
||||||
|
void set_pressure(sensor::Sensor *pressure) { pressure_ = pressure; }
|
||||||
|
|
||||||
|
void setup() override;
|
||||||
|
void dump_config() override;
|
||||||
|
void update() override;
|
||||||
|
|
||||||
|
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
sensor::Sensor *temperature_{nullptr};
|
||||||
|
sensor::Sensor *altitude_{nullptr};
|
||||||
|
sensor::Sensor *pressure_{nullptr};
|
||||||
|
enum ErrorCode {
|
||||||
|
NONE = 0,
|
||||||
|
COMMUNICATION_FAILED,
|
||||||
|
WRONG_ID,
|
||||||
|
} error_code_{NONE};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace mpl3115a2
|
||||||
|
} // namespace esphome
|
75
esphome/components/mpl3115a2/sensor.py
Normal file
75
esphome/components/mpl3115a2/sensor.py
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
import esphome.codegen as cg
|
||||||
|
import esphome.config_validation as cv
|
||||||
|
from esphome.components import i2c, sensor
|
||||||
|
from esphome.const import (
|
||||||
|
CONF_ALTITUDE,
|
||||||
|
CONF_ID,
|
||||||
|
CONF_PRESSURE,
|
||||||
|
CONF_TEMPERATURE,
|
||||||
|
DEVICE_CLASS_PRESSURE,
|
||||||
|
DEVICE_CLASS_TEMPERATURE,
|
||||||
|
STATE_CLASS_MEASUREMENT,
|
||||||
|
UNIT_CELSIUS,
|
||||||
|
UNIT_HECTOPASCAL,
|
||||||
|
UNIT_METER,
|
||||||
|
)
|
||||||
|
|
||||||
|
CODEOWNERS = ["@kbickar"]
|
||||||
|
DEPENDENCIES = ["i2c"]
|
||||||
|
|
||||||
|
mpl3115a2_ns = cg.esphome_ns.namespace("mpl3115a2")
|
||||||
|
MPL3115A2Component = mpl3115a2_ns.class_(
|
||||||
|
"MPL3115A2Component", cg.PollingComponent, i2c.I2CDevice
|
||||||
|
)
|
||||||
|
|
||||||
|
CONFIG_SCHEMA = (
|
||||||
|
cv.Schema(
|
||||||
|
{
|
||||||
|
cv.GenerateID(): cv.declare_id(MPL3115A2Component),
|
||||||
|
cv.Exclusive(
|
||||||
|
CONF_PRESSURE,
|
||||||
|
"pressure",
|
||||||
|
f"{CONF_PRESSURE} and {CONF_ALTITUDE} can't be used together",
|
||||||
|
): sensor.sensor_schema(
|
||||||
|
unit_of_measurement=UNIT_HECTOPASCAL,
|
||||||
|
accuracy_decimals=1,
|
||||||
|
device_class=DEVICE_CLASS_PRESSURE,
|
||||||
|
state_class=STATE_CLASS_MEASUREMENT,
|
||||||
|
),
|
||||||
|
cv.Exclusive(
|
||||||
|
CONF_ALTITUDE,
|
||||||
|
"pressure",
|
||||||
|
f"{CONF_PRESSURE} and {CONF_ALTITUDE} can't be used together",
|
||||||
|
): sensor.sensor_schema(
|
||||||
|
unit_of_measurement=UNIT_METER,
|
||||||
|
accuracy_decimals=1,
|
||||||
|
state_class=STATE_CLASS_MEASUREMENT,
|
||||||
|
),
|
||||||
|
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
|
||||||
|
unit_of_measurement=UNIT_CELSIUS,
|
||||||
|
accuracy_decimals=1,
|
||||||
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
state_class=STATE_CLASS_MEASUREMENT,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.extend(cv.polling_component_schema("60s"))
|
||||||
|
.extend(i2c.i2c_device_schema(0x60))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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_PRESSURE in config:
|
||||||
|
sens = await sensor.new_sensor(config[CONF_PRESSURE])
|
||||||
|
cg.add(var.set_pressure(sens))
|
||||||
|
elif CONF_ALTITUDE in config:
|
||||||
|
sens = await sensor.new_sensor(config[CONF_ALTITUDE])
|
||||||
|
cg.add(var.set_altitude(sens))
|
||||||
|
|
||||||
|
if CONF_TEMPERATURE in config:
|
||||||
|
sens = await sensor.new_sensor(config[CONF_TEMPERATURE])
|
||||||
|
cg.add(var.set_temperature(sens))
|
|
@ -1167,6 +1167,13 @@ sensor:
|
||||||
temperature:
|
temperature:
|
||||||
name: Max9611 Temp
|
name: Max9611 Temp
|
||||||
update_interval: 1s
|
update_interval: 1s
|
||||||
|
- platform: mpl3115a2
|
||||||
|
i2c_id: i2c_bus
|
||||||
|
temperature:
|
||||||
|
name: "MPL3115A2 Temperature"
|
||||||
|
pressure:
|
||||||
|
name: "MPL3115A2 Pressure"
|
||||||
|
update_interval: 10s
|
||||||
|
|
||||||
esp32_touch:
|
esp32_touch:
|
||||||
setup_mode: false
|
setup_mode: false
|
||||||
|
|
Loading…
Reference in a new issue