mirror of
https://github.com/esphome/esphome.git
synced 2024-11-30 18:54:14 +01:00
lps22: add a component
LPS22HB/HH is a barometric pressure sensor. It has pretty high accuracy, is somewhat less prone to measuring weird stuff compared to competing chips and is reasonably widely available for purchase. This adds a very basic one-shot based implementation for interacting with this sensor over an I2C bus. The chip also supports communication over SPI, but I don't have the means to test such configuration (for my PCB is baked for I2C) as well as measuring the conditions in a automatic mode. These are left as future extensions to the basic implementation seen here.
This commit is contained in:
parent
1cf4818640
commit
94327d9e3a
12 changed files with 199 additions and 0 deletions
|
@ -228,6 +228,7 @@ esphome/components/lightwaverf/* @max246
|
|||
esphome/components/lilygo_t5_47/touchscreen/* @jesserockz
|
||||
esphome/components/lock/* @esphome/core
|
||||
esphome/components/logger/* @esphome/core
|
||||
esphome/components/lps22/* @nagisa
|
||||
esphome/components/ltr390/* @latonita @sjtrny
|
||||
esphome/components/ltr501/* @latonita
|
||||
esphome/components/ltr_als_ps/* @latonita
|
||||
|
|
0
esphome/components/lps22/__init__.py
Normal file
0
esphome/components/lps22/__init__.py
Normal file
66
esphome/components/lps22/lps22.cpp
Normal file
66
esphome/components/lps22/lps22.cpp
Normal file
|
@ -0,0 +1,66 @@
|
|||
#include "lps22.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace lps22 {
|
||||
|
||||
static constexpr const char *const TAG = "lps22";
|
||||
|
||||
static constexpr uint8_t WHO_AM_I = 0x0F;
|
||||
static constexpr uint8_t LPS22HB_ID = 0xB1;
|
||||
static constexpr uint8_t LPS22HH_ID = 0xB3;
|
||||
static constexpr uint8_t CTRL_REG2 = 0x11;
|
||||
static constexpr uint8_t CTRL_REG2_ONE_SHOT_MASK = 0b1;
|
||||
static constexpr uint8_t STATUS = 0x27;
|
||||
static constexpr uint8_t STATUS_T_DA_MASK = 0b10;
|
||||
static constexpr uint8_t STATUS_P_DA_MASK = 0b01;
|
||||
static constexpr uint8_t TEMP_L = 0x2b;
|
||||
static constexpr uint8_t PRES_OUT_XL = 0x28;
|
||||
static constexpr uint8_t REF_P_XL = 0x28;
|
||||
static constexpr uint8_t READ_ATTEMPTS = 10;
|
||||
static constexpr uint8_t READ_INTERVAL = 5;
|
||||
static constexpr float PRESSURE_SCALE = 1. / 4096.;
|
||||
static constexpr float TEMPERATURE_SCALE = 0.01;
|
||||
|
||||
void LPS22Component::setup() {}
|
||||
|
||||
void LPS22Component::dump_config() { LOG_I2C_DEVICE(this); }
|
||||
|
||||
void LPS22Component::update() {
|
||||
uint8_t value = 0x00;
|
||||
this->read_register(WHO_AM_I, &value, 1);
|
||||
if (value != LPS22HB_ID && value != LPS22HH_ID) {
|
||||
ESP_LOGW(TAG, "device IDs as %02x, which isn't a known LPS22HB or LPS22HH ID", value);
|
||||
return;
|
||||
}
|
||||
|
||||
this->read_register(CTRL_REG2, &value, 1);
|
||||
value |= CTRL_REG2_ONE_SHOT_MASK;
|
||||
this->write_register(CTRL_REG2, &value, 1);
|
||||
|
||||
this->set_retry(READ_INTERVAL, READ_ATTEMPTS, [this](uint8_t _) { return this->try_read_(); });
|
||||
}
|
||||
|
||||
RetryResult LPS22Component::try_read_() {
|
||||
uint8_t value = 0x00;
|
||||
this->read_register(STATUS, &value, 1);
|
||||
const uint8_t expected_status_mask = STATUS_T_DA_MASK | STATUS_P_DA_MASK;
|
||||
if ((value & expected_status_mask) != expected_status_mask) {
|
||||
ESP_LOGD(TAG, "STATUS not ready: %x", value);
|
||||
return RetryResult::RETRY;
|
||||
}
|
||||
|
||||
uint8_t t_buf[2]{0};
|
||||
this->read_register(TEMP_L, t_buf, 2);
|
||||
float temp =
|
||||
TEMPERATURE_SCALE * static_cast<float>(static_cast<int16_t>(t_buf[1]) << 8 | static_cast<int16_t>(t_buf[0]));
|
||||
this->temperature_sensor_->publish_state(temp);
|
||||
uint8_t p_buf[3]{0};
|
||||
this->read_register(PRES_OUT_XL, p_buf, 3);
|
||||
uint32_t p_lsb =
|
||||
static_cast<uint32_t>(p_buf[2]) << 16 | static_cast<uint32_t>(p_buf[1]) << 8 | static_cast<uint32_t>(p_buf[0]);
|
||||
this->pressure_sensor_->publish_state(PRESSURE_SCALE * static_cast<float>(p_lsb));
|
||||
return RetryResult::DONE;
|
||||
}
|
||||
|
||||
} // namespace lps22
|
||||
} // namespace esphome
|
30
esphome/components/lps22/lps22.h
Normal file
30
esphome/components/lps22/lps22.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace lps22 {
|
||||
|
||||
class LPS22Component : public sensor::Sensor, public PollingComponent, public i2c::I2CDevice {
|
||||
public:
|
||||
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
|
||||
void set_pressure_sensor(sensor::Sensor *pressure_sensor) { pressure_sensor_ = pressure_sensor; }
|
||||
|
||||
LPS22Component() : PollingComponent(15000) {}
|
||||
|
||||
void setup() override;
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
sensor::Sensor *temperature_sensor_{nullptr};
|
||||
sensor::Sensor *pressure_sensor_{nullptr};
|
||||
|
||||
private:
|
||||
RetryResult try_read_();
|
||||
};
|
||||
|
||||
} // namespace lps22
|
||||
} // namespace esphome
|
58
esphome/components/lps22/sensor.py
Normal file
58
esphome/components/lps22/sensor.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_TEMPERATURE,
|
||||
CONF_PRESSURE,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_CELSIUS,
|
||||
UNIT_HECTOPASCAL,
|
||||
ICON_THERMOMETER,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
DEVICE_CLASS_PRESSURE,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@nagisa"]
|
||||
DEPENDENCIES = ["i2c"]
|
||||
|
||||
lps22 = cg.esphome_ns.namespace("lps22")
|
||||
|
||||
LPS22Component = lps22.class_("LPS22Component", cg.PollingComponent, i2c.I2CDevice)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(LPS22Component),
|
||||
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_CELSIUS,
|
||||
icon=ICON_THERMOMETER,
|
||||
accuracy_decimals=2,
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_HECTOPASCAL,
|
||||
accuracy_decimals=2,
|
||||
device_class=DEVICE_CLASS_PRESSURE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
.extend(i2c.i2c_device_schema(0x5D)) # can also be 0x5C
|
||||
)
|
||||
|
||||
|
||||
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_TEMPERATURE in config:
|
||||
sens = await sensor.new_sensor(config[CONF_TEMPERATURE])
|
||||
cg.add(var.set_temperature_sensor(sens))
|
||||
|
||||
if CONF_PRESSURE in config:
|
||||
sens = await sensor.new_sensor(config[CONF_PRESSURE])
|
||||
cg.add(var.set_pressure_sensor(sens))
|
8
tests/components/lps22/common.yaml
Normal file
8
tests/components/lps22/common.yaml
Normal file
|
@ -0,0 +1,8 @@
|
|||
sensor:
|
||||
- platform: lps22
|
||||
address: 0x5d
|
||||
update_interval: 10s
|
||||
temperature:
|
||||
name: "LPS22 Temperature"
|
||||
pressure:
|
||||
name: "LPS22 Pressure"
|
6
tests/components/lps22/test.esp32-ard.yaml
Normal file
6
tests/components/lps22/test.esp32-ard.yaml
Normal file
|
@ -0,0 +1,6 @@
|
|||
i2c:
|
||||
- id: i2c_lps22
|
||||
scl: 16
|
||||
sda: 17
|
||||
|
||||
<<: !include common.yaml
|
6
tests/components/lps22/test.esp32-c3-ard.yaml
Normal file
6
tests/components/lps22/test.esp32-c3-ard.yaml
Normal file
|
@ -0,0 +1,6 @@
|
|||
i2c:
|
||||
- id: i2c_lps22
|
||||
scl: 5
|
||||
sda: 4
|
||||
|
||||
<<: !include common.yaml
|
6
tests/components/lps22/test.esp32-c3-idf.yaml
Normal file
6
tests/components/lps22/test.esp32-c3-idf.yaml
Normal file
|
@ -0,0 +1,6 @@
|
|||
i2c:
|
||||
- id: i2c_lps22
|
||||
scl: 5
|
||||
sda: 4
|
||||
|
||||
<<: !include common.yaml
|
6
tests/components/lps22/test.esp32-idf.yaml
Normal file
6
tests/components/lps22/test.esp32-idf.yaml
Normal file
|
@ -0,0 +1,6 @@
|
|||
i2c:
|
||||
- id: i2c_lps22
|
||||
scl: 16
|
||||
sda: 17
|
||||
|
||||
<<: !include common.yaml
|
6
tests/components/lps22/test.esp8266-ard.yaml
Normal file
6
tests/components/lps22/test.esp8266-ard.yaml
Normal file
|
@ -0,0 +1,6 @@
|
|||
i2c:
|
||||
- id: i2c_lps22
|
||||
scl: 5
|
||||
sda: 4
|
||||
|
||||
<<: !include common.yaml
|
6
tests/components/lps22/test.rp2040-ard.yaml
Normal file
6
tests/components/lps22/test.rp2040-ard.yaml
Normal file
|
@ -0,0 +1,6 @@
|
|||
i2c:
|
||||
- id: i2c_lps22
|
||||
scl: 5
|
||||
sda: 4
|
||||
|
||||
<<: !include common.yaml
|
Loading…
Reference in a new issue