Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MPL115A2 Barometric Pressure/Temperature Sensor #6584

Open
wants to merge 29 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9021d7f
Initial renaming and quick port of Adafruits library
unic8s Apr 18, 2024
fa1479a
Fix variable names
unic8s Apr 18, 2024
85e8a64
Renamed in/out variables
unic8s Apr 18, 2024
c57d5f5
Add coefficients on setup
unic8s Apr 18, 2024
c6fd84d
Remove error status reading
unic8s Apr 18, 2024
a492b68
Add into test yaml
unic8s Apr 18, 2024
250be4f
Merge branch 'dev' into dev
unic8s Apr 18, 2024
a548fe3
Remove unused unit
unic8s Apr 18, 2024
127cdb8
Merge branch 'dev' of github.com:unic8s/esphome into dev
unic8s Apr 18, 2024
3382ff1
Removed whitespace
unic8s Apr 19, 2024
fe9742e
Add codeowners to python
unic8s Apr 19, 2024
18c45b5
Updated codeowners file
unic8s Apr 19, 2024
107d794
Fix black formatting
unic8s Apr 19, 2024
187a7c0
Fix code style
unic8s Apr 19, 2024
772563a
Fix variable naming
unic8s Apr 19, 2024
0abbc73
Merge branch 'dev' into dev
unic8s Apr 23, 2024
507bb46
Merge branch 'dev' into dev
unic8s Apr 23, 2024
2948cc5
Merge branch 'dev' into dev
unic8s Apr 23, 2024
c473a43
Merge branch 'dev' into dev
unic8s Apr 24, 2024
4a6b090
Merge branch 'dev' into dev
unic8s Apr 25, 2024
79360f1
Merge branch 'dev' into dev
unic8s Apr 26, 2024
78993ed
Merge branch 'dev' into dev
unic8s May 5, 2024
66cc37a
Apply suggestions from code review
unic8s May 6, 2024
ada1026
Added component tests
unic8s May 6, 2024
d2478d0
Fixed formatting
unic8s May 6, 2024
8fad61e
Fix by moving the cmd inside the timeout
unic8s May 6, 2024
75233bb
Merge branch 'dev' into dev
unic8s May 6, 2024
9fae68f
Merge branch 'esphome:dev' into dev
unic8s May 21, 2024
37d27a7
Merge branch 'dev' into dev
unic8s Jun 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ esphome/components/modbus_controller/text_sensor/* @martgras
esphome/components/mopeka_ble/* @Fabian-Schmidt @spbrogan
esphome/components/mopeka_pro_check/* @spbrogan
esphome/components/mopeka_std_check/* @Fabian-Schmidt
esphome/components/mpl115a2/* @unic8s
esphome/components/mpl3115a2/* @kbickar
esphome/components/mpu6886/* @fabaff
esphome/components/ms8607/* @e28eta
Expand Down
1 change: 1 addition & 0 deletions esphome/components/mpl115a2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CODEOWNERS = ["@unic8s"]
85 changes: 85 additions & 0 deletions esphome/components/mpl115a2/mpl115a2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "mpl115a2.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"

namespace esphome {
namespace mpl115a2 {

static const char *const TAG = "mpl115a2";

void MPL115A2Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up MPL115A2...");

this->read_coefficients_();
}

void MPL115A2Component::read_coefficients_() {
int16_t a0coeff;
int16_t b1coeff;
int16_t b2coeff;
int16_t c12coeff;

uint8_t cmd;
uint8_t buffer[8];

cmd = MPL115A2_REGISTER_A0_COEFF_MSB;
this->write(&cmd, 1);
this->read(buffer, 8);

a0coeff = (((uint16_t) buffer[0] << 8) | buffer[1]);
b1coeff = (((uint16_t) buffer[2] << 8) | buffer[3]);
b2coeff = (((uint16_t) buffer[4] << 8) | buffer[5]);
c12coeff = (((uint16_t) buffer[6] << 8) | buffer[7]) >> 2;
unic8s marked this conversation as resolved.
Show resolved Hide resolved

mpl115a2_a0_ = (float) a0coeff / 8;
mpl115a2_b1_ = (float) b1coeff / 8192;
mpl115a2_b2_ = (float) b2coeff / 16384;
mpl115a2_c12_ = (float) c12coeff;
mpl115a2_c12_ /= 4194304.0;
unic8s marked this conversation as resolved.
Show resolved Hide resolved
}

void MPL115A2Component::dump_config() {
ESP_LOGCONFIG(TAG, "MPL115A2:");
LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(this);
LOG_SENSOR(" ", "Temperature", this->temperature_);
LOG_SENSOR(" ", "Pressure", this->pressure_);
}

void MPL115A2Component::update() {
uint16_t pressure_in, temp_in;
float pressure_comp;

uint8_t cmd[2] = {MPL115A2_REGISTER_STARTCONVERSION, 0};
uint8_t buffer[4];

unic8s marked this conversation as resolved.
Show resolved Hide resolved
this->write(cmd, 2);

// Wait a bit for the conversion to complete (3ms max)
delay(5);

cmd[0] = MPL115A2_REGISTER_PRESSURE_MSB;
this->write(cmd, 1);
this->read(buffer, 4);

pressure_in = (((uint16_t) buffer[0] << 8) | buffer[1]) >> 6;
temp_in = (((uint16_t) buffer[2] << 8) | buffer[3]) >> 6;

// See datasheet p.6 for evaluation sequence
pressure_comp = mpl115a2_a0_ + (mpl115a2_b1_ + mpl115a2_c12_ * temp_in) * pressure_in + mpl115a2_b2_ * temp_in;

float pressure_out = ((65.0F / 1023.0F) * pressure_comp) + 50.0F;
if (this->pressure_ != nullptr)
this->pressure_->publish_state(pressure_out);

float temperature_out = ((float) temp_in - 498.0F) / -5.35F + 25.0F;
if (this->temperature_ != nullptr)
this->temperature_->publish_state(temperature_out);

ESP_LOGD(TAG, "Got Temperature=%.1f°C Pressure=%.1f", temperature_out, pressure_out);
unic8s marked this conversation as resolved.
Show resolved Hide resolved

this->status_clear_warning();
unic8s marked this conversation as resolved.
Show resolved Hide resolved
}

} // namespace mpl115a2
} // namespace esphome
49 changes: 49 additions & 0 deletions esphome/components/mpl115a2/mpl115a2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"

namespace esphome {
namespace mpl115a2 {

// enums from https://github.com/adafruit/Adafruit_MPL115A2/
/** MPL115A2 registers **/
enum {
MPL115A2_REGISTER_PRESSURE_MSB = (0x00),
MPL115A2_REGISTER_PRESSURE_LSB = (0x01),

MPL115A2_REGISTER_TEMP_MSB = (0x02),
MPL115A2_REGISTER_TEMP_LSB = (0x03),

MPL115A2_REGISTER_A0_COEFF_MSB = (0x04),

MPL115A2_REGISTER_STARTCONVERSION = (0x12),
};

class MPL115A2Component : public PollingComponent, public i2c::I2CDevice {
public:
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }
void set_pressure(sensor::Sensor *pressure) { pressure_ = pressure; }
unic8s marked this conversation as resolved.
Show resolved Hide resolved

void setup() override;
void dump_config() override;
void update() override;

float get_setup_priority() const override { return setup_priority::DATA; }

private:
float mpl115a2_a0_;
float mpl115a2_b1_;
float mpl115a2_b2_;
float mpl115a2_c12_;

void read_coefficients_();

protected:
sensor::Sensor *temperature_{nullptr};
sensor::Sensor *pressure_{nullptr};
};

} // namespace mpl115a2
} // namespace esphome
57 changes: 57 additions & 0 deletions esphome/components/mpl115a2/sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import (
CONF_ID,
CONF_PRESSURE,
CONF_TEMPERATURE,
DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
UNIT_HECTOPASCAL,
)

CODEOWNERS = ["@unic8s"]
DEPENDENCIES = ["i2c"]

mpl115a2_ns = cg.esphome_ns.namespace("mpl115a2")
MPL115A2Component = mpl115a2_ns.class_(
"MPL115A2Component", cg.PollingComponent, i2c.I2CDevice
)

CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(MPL115A2Component),
cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
unit_of_measurement=UNIT_HECTOPASCAL,
accuracy_decimals=1,
device_class=DEVICE_CLASS_PRESSURE,
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])
unic8s marked this conversation as resolved.
Show resolved Hide resolved
cg.add(var.set_pressure(sens))

if CONF_TEMPERATURE in config:
sens = await sensor.new_sensor(config[CONF_TEMPERATURE])
unic8s marked this conversation as resolved.
Show resolved Hide resolved
cg.add(var.set_temperature(sens))
6 changes: 6 additions & 0 deletions tests/test1.yaml
unic8s marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,12 @@ sensor:
object:
name: Object
emissivity: 1.0
- platform: mpl115a2
i2c_id: i2c_bus
temperature:
name: "MPL115A2 Temperature"
pressure:
name: "MPL115A2 Pressure"
- platform: mpl3115a2
i2c_id: i2c_bus
temperature:
Expand Down