pmsx003: add standard particle, particle counts (#1694)

This commit is contained in:
John "Warthog9" Hawley
2021-08-02 01:32:08 -07:00
committed by GitHub
parent b58ca46a46
commit fb24e55c8d
5 changed files with 290 additions and 23 deletions
+91 -23
View File
@@ -6,9 +6,39 @@ namespace pmsx003 {
static const char *const TAG = "pmsx003";
void PMSX003Component::set_pm_1_0_std_sensor(sensor::Sensor *pm_1_0_std_sensor) {
pm_1_0_std_sensor_ = pm_1_0_std_sensor;
}
void PMSX003Component::set_pm_2_5_std_sensor(sensor::Sensor *pm_2_5_std_sensor) {
pm_2_5_std_sensor_ = pm_2_5_std_sensor;
}
void PMSX003Component::set_pm_10_0_std_sensor(sensor::Sensor *pm_10_0_std_sensor) {
pm_10_0_std_sensor_ = pm_10_0_std_sensor;
}
void PMSX003Component::set_pm_1_0_sensor(sensor::Sensor *pm_1_0_sensor) { pm_1_0_sensor_ = pm_1_0_sensor; }
void PMSX003Component::set_pm_2_5_sensor(sensor::Sensor *pm_2_5_sensor) { pm_2_5_sensor_ = pm_2_5_sensor; }
void PMSX003Component::set_pm_10_0_sensor(sensor::Sensor *pm_10_0_sensor) { pm_10_0_sensor_ = pm_10_0_sensor; }
void PMSX003Component::set_pm_particles_03um_sensor(sensor::Sensor *pm_particles_03um_sensor) {
pm_particles_03um_sensor_ = pm_particles_03um_sensor;
}
void PMSX003Component::set_pm_particles_05um_sensor(sensor::Sensor *pm_particles_05um_sensor) {
pm_particles_05um_sensor_ = pm_particles_05um_sensor;
}
void PMSX003Component::set_pm_particles_10um_sensor(sensor::Sensor *pm_particles_10um_sensor) {
pm_particles_10um_sensor_ = pm_particles_10um_sensor;
}
void PMSX003Component::set_pm_particles_25um_sensor(sensor::Sensor *pm_particles_25um_sensor) {
pm_particles_25um_sensor_ = pm_particles_25um_sensor;
}
void PMSX003Component::set_pm_particles_50um_sensor(sensor::Sensor *pm_particles_50um_sensor) {
pm_particles_50um_sensor_ = pm_particles_50um_sensor;
}
void PMSX003Component::set_pm_particles_100um_sensor(sensor::Sensor *pm_particles_100um_sensor) {
pm_particles_100um_sensor_ = pm_particles_100um_sensor;
}
void PMSX003Component::set_temperature_sensor(sensor::Sensor *temperature_sensor) {
temperature_sensor_ = temperature_sensor;
}
@@ -102,19 +132,68 @@ optional<bool> PMSX003Component::check_byte_() {
void PMSX003Component::parse_data_() {
switch (this->type_) {
case PMSX003_TYPE_5003ST: {
uint16_t formaldehyde = this->get_16_bit_uint_(28);
float temperature = this->get_16_bit_uint_(30) / 10.0f;
float humidity = this->get_16_bit_uint_(32) / 10.0f;
ESP_LOGD(TAG, "Got Temperature: %.1f°C, Humidity: %.1f%% Formaldehyde: %u µg/m^3", temperature, humidity,
formaldehyde);
if (this->temperature_sensor_ != nullptr)
this->temperature_sensor_->publish_state(temperature);
if (this->humidity_sensor_ != nullptr)
this->humidity_sensor_->publish_state(humidity);
if (this->formaldehyde_sensor_ != nullptr)
this->formaldehyde_sensor_->publish_state(formaldehyde);
// The rest of the PMS5003ST matches the PMS5003, continue on
}
case PMSX003_TYPE_X003: {
uint16_t pm_1_0_std_concentration = this->get_16_bit_uint_(4);
uint16_t pm_2_5_std_concentration = this->get_16_bit_uint_(6);
uint16_t pm_10_0_std_concentration = this->get_16_bit_uint_(8);
uint16_t pm_1_0_concentration = this->get_16_bit_uint_(10);
uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12);
uint16_t pm_10_0_concentration = this->get_16_bit_uint_(14);
uint16_t pm_particles_03um = this->get_16_bit_uint_(16);
uint16_t pm_particles_05um = this->get_16_bit_uint_(18);
uint16_t pm_particles_10um = this->get_16_bit_uint_(20);
uint16_t pm_particles_25um = this->get_16_bit_uint_(22);
uint16_t pm_particles_50um = this->get_16_bit_uint_(24);
uint16_t pm_particles_100um = this->get_16_bit_uint_(26);
ESP_LOGD(TAG,
"Got PM1.0 Concentration: %u µg/m^3, PM2.5 Concentration %u µg/m^3, PM10.0 Concentration: %u µg/m^3",
pm_1_0_concentration, pm_2_5_concentration, pm_10_0_concentration);
if (this->pm_1_0_std_sensor_ != nullptr)
this->pm_1_0_std_sensor_->publish_state(pm_1_0_std_concentration);
if (this->pm_2_5_std_sensor_ != nullptr)
this->pm_2_5_std_sensor_->publish_state(pm_2_5_std_concentration);
if (this->pm_10_0_std_sensor_ != nullptr)
this->pm_10_0_std_sensor_->publish_state(pm_10_0_std_concentration);
if (this->pm_1_0_sensor_ != nullptr)
this->pm_1_0_sensor_->publish_state(pm_1_0_concentration);
if (this->pm_2_5_sensor_ != nullptr)
this->pm_2_5_sensor_->publish_state(pm_2_5_concentration);
if (this->pm_10_0_sensor_ != nullptr)
this->pm_10_0_sensor_->publish_state(pm_10_0_concentration);
if (this->pm_particles_03um_sensor_ != nullptr)
this->pm_particles_03um_sensor_->publish_state(pm_particles_03um);
if (this->pm_particles_05um_sensor_ != nullptr)
this->pm_particles_05um_sensor_->publish_state(pm_particles_05um);
if (this->pm_particles_10um_sensor_ != nullptr)
this->pm_particles_10um_sensor_->publish_state(pm_particles_10um);
if (this->pm_particles_25um_sensor_ != nullptr)
this->pm_particles_25um_sensor_->publish_state(pm_particles_25um);
if (this->pm_particles_50um_sensor_ != nullptr)
this->pm_particles_50um_sensor_->publish_state(pm_particles_50um);
if (this->pm_particles_100um_sensor_ != nullptr)
this->pm_particles_100um_sensor_->publish_state(pm_particles_100um);
break;
}
case PMSX003_TYPE_5003T: {
@@ -131,29 +210,6 @@ void PMSX003Component::parse_data_() {
this->humidity_sensor_->publish_state(humidity);
break;
}
case PMSX003_TYPE_5003ST: {
uint16_t pm_1_0_concentration = this->get_16_bit_uint_(10);
uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12);
uint16_t pm_10_0_concentration = this->get_16_bit_uint_(14);
uint16_t formaldehyde = this->get_16_bit_uint_(28);
float temperature = this->get_16_bit_uint_(30) / 10.0f;
float humidity = this->get_16_bit_uint_(32) / 10.0f;
ESP_LOGD(TAG, "Got PM2.5 Concentration: %u µg/m^3, Temperature: %.1f°C, Humidity: %.1f%% Formaldehyde: %u µg/m^3",
pm_2_5_concentration, temperature, humidity, formaldehyde);
if (this->pm_1_0_sensor_ != nullptr)
this->pm_1_0_sensor_->publish_state(pm_1_0_concentration);
if (this->pm_2_5_sensor_ != nullptr)
this->pm_2_5_sensor_->publish_state(pm_2_5_concentration);
if (this->pm_10_0_sensor_ != nullptr)
this->pm_10_0_sensor_->publish_state(pm_10_0_concentration);
if (this->temperature_sensor_ != nullptr)
this->temperature_sensor_->publish_state(temperature);
if (this->humidity_sensor_ != nullptr)
this->humidity_sensor_->publish_state(humidity);
if (this->formaldehyde_sensor_ != nullptr)
this->formaldehyde_sensor_->publish_state(formaldehyde);
break;
}
}
this->status_clear_warning();
@@ -163,9 +219,21 @@ uint16_t PMSX003Component::get_16_bit_uint_(uint8_t start_index) {
}
void PMSX003Component::dump_config() {
ESP_LOGCONFIG(TAG, "PMSX003:");
LOG_SENSOR(" ", "PM1.0STD", this->pm_1_0_std_sensor_);
LOG_SENSOR(" ", "PM2.5STD", this->pm_2_5_std_sensor_);
LOG_SENSOR(" ", "PM10.0STD", this->pm_10_0_std_sensor_);
LOG_SENSOR(" ", "PM1.0", this->pm_1_0_sensor_);
LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_);
LOG_SENSOR(" ", "PM10.0", this->pm_10_0_sensor_);
LOG_SENSOR(" ", "PM0.3um", this->pm_particles_03um_sensor_);
LOG_SENSOR(" ", "PM0.5um", this->pm_particles_05um_sensor_);
LOG_SENSOR(" ", "PM1.0um", this->pm_particles_10um_sensor_);
LOG_SENSOR(" ", "PM2.5um", this->pm_particles_25um_sensor_);
LOG_SENSOR(" ", "PM5.0um", this->pm_particles_50um_sensor_);
LOG_SENSOR(" ", "PM10.0um", this->pm_particles_100um_sensor_);
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
LOG_SENSOR(" ", "Formaldehyde", this->formaldehyde_sensor_);
+29
View File
@@ -21,9 +21,22 @@ class PMSX003Component : public uart::UARTDevice, public Component {
void dump_config() override;
void set_type(PMSX003Type type) { type_ = type; }
void set_pm_1_0_std_sensor(sensor::Sensor *pm_1_0_std_sensor);
void set_pm_2_5_std_sensor(sensor::Sensor *pm_2_5_std_sensor);
void set_pm_10_0_std_sensor(sensor::Sensor *pm_10_0_std_sensor);
void set_pm_1_0_sensor(sensor::Sensor *pm_1_0_sensor);
void set_pm_2_5_sensor(sensor::Sensor *pm_2_5_sensor);
void set_pm_10_0_sensor(sensor::Sensor *pm_10_0_sensor);
void set_pm_particles_03um_sensor(sensor::Sensor *pm_particles_03um_sensor);
void set_pm_particles_05um_sensor(sensor::Sensor *pm_particles_05um_sensor);
void set_pm_particles_10um_sensor(sensor::Sensor *pm_particles_10um_sensor);
void set_pm_particles_25um_sensor(sensor::Sensor *pm_particles_25um_sensor);
void set_pm_particles_50um_sensor(sensor::Sensor *pm_particles_50um_sensor);
void set_pm_particles_100um_sensor(sensor::Sensor *pm_particles_100um_sensor);
void set_temperature_sensor(sensor::Sensor *temperature_sensor);
void set_humidity_sensor(sensor::Sensor *humidity_sensor);
void set_formaldehyde_sensor(sensor::Sensor *formaldehyde_sensor);
@@ -37,9 +50,25 @@ class PMSX003Component : public uart::UARTDevice, public Component {
uint8_t data_index_{0};
uint32_t last_transmission_{0};
PMSX003Type type_;
// "Standard Particle"
sensor::Sensor *pm_1_0_std_sensor_{nullptr};
sensor::Sensor *pm_2_5_std_sensor_{nullptr};
sensor::Sensor *pm_10_0_std_sensor_{nullptr};
// "Under Atmospheric Pressure"
sensor::Sensor *pm_1_0_sensor_{nullptr};
sensor::Sensor *pm_2_5_sensor_{nullptr};
sensor::Sensor *pm_10_0_sensor_{nullptr};
// Particle counts by size
sensor::Sensor *pm_particles_03um_sensor_{nullptr};
sensor::Sensor *pm_particles_05um_sensor_{nullptr};
sensor::Sensor *pm_particles_10um_sensor_{nullptr};
sensor::Sensor *pm_particles_25um_sensor_{nullptr};
sensor::Sensor *pm_particles_50um_sensor_{nullptr};
sensor::Sensor *pm_particles_100um_sensor_{nullptr};
sensor::Sensor *temperature_sensor_{nullptr};
sensor::Sensor *humidity_sensor_{nullptr};
sensor::Sensor *formaldehyde_sensor_{nullptr};
+100
View File
@@ -8,6 +8,15 @@ from esphome.const import (
CONF_PM_10_0,
CONF_PM_1_0,
CONF_PM_2_5,
CONF_PM_10_0_STD,
CONF_PM_1_0_STD,
CONF_PM_2_5_STD,
CONF_PM_0_3UM,
CONF_PM_0_5UM,
CONF_PM_1_0UM,
CONF_PM_2_5UM,
CONF_PM_5_0UM,
CONF_PM_10_0UM,
CONF_TEMPERATURE,
CONF_TYPE,
DEVICE_CLASS_HUMIDITY,
@@ -16,6 +25,7 @@ from esphome.const import (
STATE_CLASS_MEASUREMENT,
UNIT_MICROGRAMS_PER_CUBIC_METER,
UNIT_CELSIUS,
UNIT_COUNT_DECILITRE,
UNIT_PERCENT,
)
@@ -60,6 +70,24 @@ CONFIG_SCHEMA = (
{
cv.GenerateID(): cv.declare_id(PMSX003Component),
cv.Required(CONF_TYPE): cv.enum(PMSX003_TYPES, upper=True),
cv.Optional(CONF_PM_1_0_STD): sensor.sensor_schema(
UNIT_MICROGRAMS_PER_CUBIC_METER,
ICON_CHEMICAL_WEAPON,
0,
DEVICE_CLASS_EMPTY,
),
cv.Optional(CONF_PM_2_5_STD): sensor.sensor_schema(
UNIT_MICROGRAMS_PER_CUBIC_METER,
ICON_CHEMICAL_WEAPON,
0,
DEVICE_CLASS_EMPTY,
),
cv.Optional(CONF_PM_10_0_STD): sensor.sensor_schema(
UNIT_MICROGRAMS_PER_CUBIC_METER,
ICON_CHEMICAL_WEAPON,
0,
DEVICE_CLASS_EMPTY,
),
cv.Optional(CONF_PM_1_0): sensor.sensor_schema(
unit_of_measurement=UNIT_MICROGRAMS_PER_CUBIC_METER,
icon=ICON_CHEMICAL_WEAPON,
@@ -78,6 +106,42 @@ CONFIG_SCHEMA = (
accuracy_decimals=0,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_PM_0_3UM): sensor.sensor_schema(
UNIT_COUNT_DECILITRE,
ICON_CHEMICAL_WEAPON,
0,
DEVICE_CLASS_EMPTY,
),
cv.Optional(CONF_PM_0_5UM): sensor.sensor_schema(
UNIT_COUNT_DECILITRE,
ICON_CHEMICAL_WEAPON,
0,
DEVICE_CLASS_EMPTY,
),
cv.Optional(CONF_PM_1_0UM): sensor.sensor_schema(
UNIT_COUNT_DECILITRE,
ICON_CHEMICAL_WEAPON,
0,
DEVICE_CLASS_EMPTY,
),
cv.Optional(CONF_PM_2_5UM): sensor.sensor_schema(
UNIT_COUNT_DECILITRE,
ICON_CHEMICAL_WEAPON,
0,
DEVICE_CLASS_EMPTY,
),
cv.Optional(CONF_PM_5_0UM): sensor.sensor_schema(
UNIT_COUNT_DECILITRE,
ICON_CHEMICAL_WEAPON,
0,
DEVICE_CLASS_EMPTY,
),
cv.Optional(CONF_PM_10_0UM): sensor.sensor_schema(
UNIT_COUNT_DECILITRE,
ICON_CHEMICAL_WEAPON,
0,
DEVICE_CLASS_EMPTY,
),
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
@@ -110,6 +174,18 @@ async def to_code(config):
cg.add(var.set_type(config[CONF_TYPE]))
if CONF_PM_1_0_STD in config:
sens = await sensor.new_sensor(config[CONF_PM_1_0_STD])
cg.add(var.set_pm_1_0_std_sensor(sens))
if CONF_PM_2_5_STD in config:
sens = await sensor.new_sensor(config[CONF_PM_2_5_STD])
cg.add(var.set_pm_2_5_std_sensor(sens))
if CONF_PM_10_0_STD in config:
sens = await sensor.new_sensor(config[CONF_PM_10_0_STD])
cg.add(var.set_pm_10_0_std_sensor(sens))
if CONF_PM_1_0 in config:
sens = await sensor.new_sensor(config[CONF_PM_1_0])
cg.add(var.set_pm_1_0_sensor(sens))
@@ -122,6 +198,30 @@ async def to_code(config):
sens = await sensor.new_sensor(config[CONF_PM_10_0])
cg.add(var.set_pm_10_0_sensor(sens))
if CONF_PM_0_3UM in config:
sens = await sensor.new_sensor(config[CONF_PM_0_3UM])
cg.add(var.set_pm_particles_03um_sensor(sens))
if CONF_PM_0_5UM in config:
sens = await sensor.new_sensor(config[CONF_PM_0_5UM])
cg.add(var.set_pm_particles_05um_sensor(sens))
if CONF_PM_1_0UM in config:
sens = await sensor.new_sensor(config[CONF_PM_1_0UM])
cg.add(var.set_pm_particles_10um_sensor(sens))
if CONF_PM_2_5UM in config:
sens = await sensor.new_sensor(config[CONF_PM_2_5UM])
cg.add(var.set_pm_particles_25um_sensor(sens))
if CONF_PM_5_0UM in config:
sens = await sensor.new_sensor(config[CONF_PM_5_0UM])
cg.add(var.set_pm_particles_50um_sensor(sens))
if CONF_PM_10_0UM in config:
sens = await sensor.new_sensor(config[CONF_PM_10_0UM])
cg.add(var.set_pm_particles_100um_sensor(sens))
if CONF_TEMPERATURE in config:
sens = await sensor.new_sensor(config[CONF_TEMPERATURE])
cg.add(var.set_temperature_sensor(sens))