Add Initial TE-M3200 pressure sensor support (#6862)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Ken Baker 2024-10-06 22:58:28 -04:00 committed by GitHub
parent 86a34f4b17
commit 5ad5ef5a42
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 278 additions and 0 deletions

View file

@ -406,6 +406,7 @@ esphome/components/tca9555/* @mobrembski
esphome/components/tcl112/* @glmnet
esphome/components/tee501/* @Stock-M
esphome/components/teleinfo/* @0hax
esphome/components/tem3200/* @bakerkj
esphome/components/template/alarm_control_panel/* @grahambrown11 @hwstar
esphome/components/template/datetime/* @rfdarter
esphome/components/template/event/* @nohat

View file

View file

@ -0,0 +1,55 @@
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,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
)
CODEOWNERS = ["@bakerkj"]
DEPENDENCIES = ["i2c"]
tem3200_ns = cg.esphome_ns.namespace("tem3200")
TEM3200Component = tem3200_ns.class_(
"TEM3200Component", cg.PollingComponent, i2c.I2CDevice
)
CONF_RAW_PRESSURE = "raw_pressure"
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(TEM3200Component),
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,
),
cv.Optional(CONF_RAW_PRESSURE): sensor.sensor_schema(
accuracy_decimals=0, state_class=STATE_CLASS_MEASUREMENT
),
}
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x28))
)
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 temperature_config := config.get(CONF_TEMPERATURE):
sens = await sensor.new_sensor(temperature_config)
cg.add(var.set_temperature_sensor(sens))
if raw_pressure_config := config.get(CONF_RAW_PRESSURE):
sens = await sensor.new_sensor(raw_pressure_config)
cg.add(var.set_raw_pressure_sensor(sens))

View file

@ -0,0 +1,151 @@
#include "tem3200.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
#include "esphome/core/hal.h"
namespace esphome {
namespace tem3200 {
static const char *const TAG = "tem3200";
enum ErrorCode {
NONE = 0,
RESERVED = 1,
STALE = 2,
FAULT = 3,
};
void TEM3200Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up TEM3200...");
uint8_t status(NONE);
uint16_t raw_temperature(0);
uint16_t raw_pressure(0);
i2c::ErrorCode err = this->read_(status, raw_temperature, raw_pressure);
if (err != i2c::ERROR_OK) {
ESP_LOGCONFIG(TAG, " I2C Communication Failed...");
this->mark_failed();
return;
}
switch (status) {
case RESERVED:
ESP_LOGE(TAG, "Invalid RESERVED Device Status");
this->mark_failed();
return;
case FAULT:
ESP_LOGE(TAG, "FAULT condition in the SSC or sensing element");
this->mark_failed();
return;
case STALE:
ESP_LOGE(TAG, "STALE data. Data has not been updated since last fetch");
this->status_set_warning();
break;
}
ESP_LOGCONFIG(TAG, " Success...");
}
void TEM3200Component::dump_config() {
ESP_LOGCONFIG(TAG, "TEM3200:");
LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(this);
LOG_SENSOR(" ", "Raw Pressure", this->raw_pressure_sensor_);
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
}
float TEM3200Component::get_setup_priority() const { return setup_priority::DATA; }
i2c::ErrorCode TEM3200Component::read_(uint8_t &status, uint16_t &raw_temperature, uint16_t &raw_pressure) {
uint8_t response[4] = {0x00, 0x00, 0x00, 0x00};
// initiate data read
i2c::ErrorCode err = this->read(response, 4);
if (err != i2c::ERROR_OK) {
return err;
}
// extract top 2 bits of first byte for status
status = (ErrorCode) (response[0] & 0xc0) >> 6;
if (status == RESERVED || status == FAULT) {
return i2c::ERROR_OK;
}
// if data is stale; reread
if (status == STALE) {
// wait for measurement 2ms
delay(2);
err = this->read(response, 4);
if (err != i2c::ERROR_OK) {
return err;
}
}
// extract top 2 bits of first byte for status
status = (ErrorCode) (response[0] & 0xc0) >> 6;
if (status == RESERVED || status == FAULT) {
return i2c::ERROR_OK;
}
// extract top 6 bits of first byte and all bits of second byte for pressure
raw_pressure = (((response[0] & 0x3f)) << 8 | response[1]);
// extract all bytes of 3rd byte and top 3 bits of fourth byte for temperature
raw_temperature = ((response[2] << 3) | (response[3] & 0xe0) >> 5);
return i2c::ERROR_OK;
}
inline float convert_temperature(uint16_t raw_temperature) {
const float temperature_bits_span = 2048;
const float temperature_max = 150;
const float temperature_min = -50;
const float temperature_span = temperature_max - temperature_min;
float temperature = (raw_temperature * temperature_span / temperature_bits_span) + temperature_min;
return temperature;
}
void TEM3200Component::update() {
uint8_t status(NONE);
uint16_t raw_temperature(0);
uint16_t raw_pressure(0);
i2c::ErrorCode err = this->read_(status, raw_temperature, raw_pressure);
if (err != i2c::ERROR_OK) {
ESP_LOGW(TAG, "I2C Communication Failed");
this->status_set_warning();
return;
}
switch (status) {
case RESERVED:
ESP_LOGE(TAG, "Failed: Device return RESERVED status");
this->status_set_warning();
return;
case FAULT:
ESP_LOGE(TAG, "Failed: FAULT condition in the SSC or sensing element");
this->mark_failed();
return;
case STALE:
ESP_LOGE(TAG, "Warning: STALE data. Data has not been updated since last fetch");
this->status_set_warning();
return;
}
float temperature = convert_temperature(raw_temperature);
ESP_LOGD(TAG, "Got raw pressure=%d, temperature=%.1f°C", raw_pressure, temperature);
if (this->temperature_sensor_ != nullptr)
this->temperature_sensor_->publish_state(temperature);
if (this->raw_pressure_sensor_ != nullptr)
this->raw_pressure_sensor_->publish_state(raw_pressure);
this->status_clear_warning();
}
} // namespace tem3200
} // namespace esphome

View 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 tem3200 {
/// This class implements support for the tem3200 pressure and temperature i2c sensors.
class TEM3200Component : public PollingComponent, public i2c::I2CDevice {
public:
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { this->temperature_sensor_ = temperature_sensor; }
void set_raw_pressure_sensor(sensor::Sensor *raw_pressure_sensor) {
this->raw_pressure_sensor_ = raw_pressure_sensor;
}
float get_setup_priority() const override;
void setup() override;
void dump_config() override;
void update() override;
protected:
i2c::ErrorCode read_(uint8_t &status, uint16_t &raw_temperature, uint16_t &raw_pressure);
sensor::Sensor *temperature_sensor_{nullptr};
sensor::Sensor *raw_pressure_sensor_{nullptr};
};
} // namespace tem3200
} // namespace esphome

View file

@ -0,0 +1,16 @@
i2c:
id: i2c_bus
scl: ${scl_pin}
sda: ${sda_pin}
frequency: 200kHz
sensor:
- platform: tem3200
update_interval: 1s
i2c_id: i2c_bus
temperature:
name: water temperature
raw_pressure:
name: water pressure

View file

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO22
sda_pin: GPIO21
<<: !include common.yaml

View file

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO22
sda_pin: GPIO21
<<: !include common.yaml

View file

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO40
sda_pin: GPIO41
<<: !include common.yaml

View file

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO40
sda_pin: GPIO41
<<: !include common.yaml

View file

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO05
sda_pin: GPIO04
<<: !include common.yaml