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 all 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 @@ -256,6 +256,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"]
79 changes: 79 additions & 0 deletions esphome/components/mpl115a2/mpl115a2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#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 = encode_uint16(buffer[0], buffer[1]);
b1coeff = encode_uint16(buffer[2], buffer[3]);
b2coeff = encode_uint16(buffer[4], buffer[5]);
c12coeff = encode_uint16(buffer[6], buffer[7]) >> 2;

this->mpl115a2_a0_ = a0coeff / 8.0f;
this->mpl115a2_b1_ = b1coeff / 8192.0f;
this->mpl115a2_b2_ = b2coeff / 16384.0f;
this->mpl115a2_c12_ = c12coeff / 4194304.0f;
}

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() {
// Wait a bit for the conversion to complete (3ms max)
this->set_timeout(5, [this]() {
uint8_t cmd[2] = {MPL115A2_REGISTER_STARTCONVERSION, 0};
this->write(cmd, 2);

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

uint16_t pressure_in = encode_uint16(buffer[0], buffer[1]) >> 6;
uint16_t temp_in = encode_uint16(buffer[2], buffer[3]) >> 6;

// See datasheet p.6 for evaluation sequence
float 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);
});
}

} // 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) { this->temperature_ = temperature; }
void set_pressure(sensor::Sensor *pressure) { this->pressure_ = pressure; }

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 pressure_sensor := config.get(CONF_PRESSURE):
sens = await sensor.new_sensor(pressure_sensor)
cg.add(var.set_pressure(sens))

if temperature_sensor := config.get(CONF_TEMPERATURE):
sens = await sensor.new_sensor(temperature_sensor)
cg.add(var.set_temperature(sens))
12 changes: 12 additions & 0 deletions tests/components/mpl115a2/test.esp32-c3-idf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
i2c:
- id: i2c_mpl115a2
scl: 5
sda: 4

sensor:
- platform: mpl115a2
temperature:
name: MPL115A2 Temperature
pressure:
name: MPL115A2 Pressure
update_interval: 10s
12 changes: 12 additions & 0 deletions tests/components/mpl115a2/test.esp32-c3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
i2c:
- id: i2c_mpl115a2
scl: 5
sda: 4

sensor:
- platform: mpl115a2
temperature:
name: MPL115A2 Temperature
pressure:
name: MPL115A2 Pressure
update_interval: 10s
13 changes: 13 additions & 0 deletions tests/components/mpl115a2/test.esp32-idf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
i2c:
- id: i2c_mpl115a2
scl: 16
sda: 17

sensor:
- platform: mpl115a2
temperature:
name: MPL115A2 Temperature
pressure:
name: MPL115A2 Pressure
update_interval: 10s

13 changes: 13 additions & 0 deletions tests/components/mpl115a2/test.esp32.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
i2c:
- id: i2c_mpl115a2
scl: 16
sda: 17

sensor:
- platform: mpl115a2
temperature:
name: MPL115A2 Temperature
pressure:
name: MPL115A2 Pressure
update_interval: 10s

13 changes: 13 additions & 0 deletions tests/components/mpl115a2/test.esp8266.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
i2c:
- id: i2c_mpl115a2
scl: 5
sda: 4

sensor:
- platform: mpl115a2
temperature:
name: MPL115A2 Temperature
pressure:
name: MPL115A2 Pressure
update_interval: 10s

13 changes: 13 additions & 0 deletions tests/components/mpl115a2/test.rp2040.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
i2c:
- id: i2c_mpl115a2
scl: 5
sda: 4

sensor:
- platform: mpl115a2
temperature:
name: MPL115A2 Temperature
pressure:
name: MPL115A2 Pressure
update_interval: 10s

6 changes: 6 additions & 0 deletions tests/test1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,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