Skip to content

Commit

Permalink
feat(sen55): actually record VOC and NOx IAQ index
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw committed Jun 22, 2024
1 parent 6f0b898 commit 047dae2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/eclss-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct Metrics {
pub co2_ppm: heapless::Vec<Measurement, MAX_SENSORS>,
pub eco2_ppm: heapless::Vec<Measurement, MAX_SENSORS>,
pub tvoc_ppb: heapless::Vec<Measurement, MAX_SENSORS>,
pub tvoc_iaq_index: heapless::Vec<Measurement, MAX_SENSORS>,
pub nox_iaq_index: heapless::Vec<Measurement, MAX_SENSORS>,
pub pressure_hpa: heapless::Vec<Measurement, MAX_SENSORS>,
pub sensor_errors: heapless::Vec<Measurement, MAX_SENSORS>,
}
Expand Down
17 changes: 17 additions & 0 deletions lib/eclss/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pub struct SensorMetrics {
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_metric"))]
pub tvoc_ppb: GaugeFamily<'static, TVOC_METRICS, SensorName>,
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_metric"))]
pub tvoc_iaq_index: GaugeFamily<'static, TVOC_IAQ_METRICS, SensorName>,
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_metric"))]
pub nox_iaq_index: GaugeFamily<'static, NOX_IAQ_METRICS, SensorName>,
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_metric"))]
#[serde(skip)]
pub pm_conc: GaugeFamily<'static, PM_CONC_METRICS, DiameterLabel>,
// #[cfg_attr(feature = "serde", serde(serialize_with =
Expand Down Expand Up @@ -52,6 +56,9 @@ pub const HUMIDITY_METRICS: usize =
pub const PRESSURE_METRICS: usize = count_features!("bme680");
pub const VOC_RESISTANCE_METRICS: usize = count_features!("bme680");
pub const TVOC_METRICS: usize = count_features!("sgp30", "bme680", "ens160");
// IAQ from 1-500
pub const TVOC_IAQ_METRICS: usize = count_features!("sen55", "bme680", "sgp40");
pub const NOX_IAQ_METRICS: usize = count_features!("sen55");
pub const PM_CONC_METRICS: usize =
// PMSA003I exposes three particulate concentration metrics
(count_features!("pmsa003i") * 3)
Expand Down Expand Up @@ -103,6 +110,14 @@ impl SensorMetrics {
.with_help("Total Volatile Organic Compounds (VOC) in parts per billion (ppb)")
.with_unit("ppb")
.build_labeled::<_, SensorName, TVOC_METRICS>(),
tvoc_iaq_index: MetricBuilder::new("tvoc_iaq_index")
.with_help("Total Volatile Organic Compounds (VOC) Indoor Air Quality (IAQ) Index from 0-500")
.with_unit("IAQ index")
.build_labeled::<_, SensorName, TVOC_IAQ_METRICS>(),
nox_iaq_index: MetricBuilder::new("nox_iaq_index")
.with_help("Nitrogen Oxides (NOx) Indoor Air Quality (IAQ) Index from 0-500")
.with_unit("IAQ index")
.build_labeled::<_, SensorName, NOX_IAQ_METRICS>(),
pm_conc: MetricBuilder::new("pm_concentration_ug_m3")
.with_help("Particulate matter concentration in ug/m^3")
.with_unit("ug/m^3")
Expand All @@ -126,6 +141,8 @@ impl SensorMetrics {
self.pressure_hpa.fmt_metric(f)?;
self.gas_resistance.fmt_metric(f)?;
self.tvoc_ppb.fmt_metric(f)?;
self.tvoc_iaq_index.fmt_metric(f)?;
self.nox_iaq_index.fmt_metric(f)?;
self.pm_conc.fmt_metric(f)?;
self.pm_count.fmt_metric(f)?;
self.sensor_errors.fmt_metric(f)?;
Expand Down
29 changes: 18 additions & 11 deletions lib/eclss/src/sensor/sen55.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct Sen55<I: 'static, D> {
pm2_5: &'static Gauge,
pm4_0: &'static Gauge,
pm10_0: &'static Gauge,
nox_index: &'static Gauge,
voc_index: &'static Gauge,
delay: D,
abs_humidity_interval: usize,
polls: Wrapping<usize>,
Expand Down Expand Up @@ -53,6 +55,8 @@ where
pm2_5: metrics.pm_conc.register(diameter("2.5")).unwrap(),
pm4_0: metrics.pm_conc.register(diameter("4.0")).unwrap(),
pm10_0: metrics.pm_conc.register(diameter("10.0")).unwrap(),
nox_index: metrics.nox_iaq_index.register(NAME).unwrap(),
voc_index: metrics.tvoc_iaq_index.register(NAME).unwrap(),
delay,
polls: Wrapping(0),
abs_humidity_interval: 1,
Expand Down Expand Up @@ -109,11 +113,12 @@ where
.context("failed to read SEN5x measurement data")?;
let temp = measurement.temp_c();
let rel_humidity = measurement.relative_humidity();
let voc = measurement.voc_index();
let voc_index = measurement.voc_index();
let nox_index = measurement.nox_index();

debug!(
"{NAME}: Temp: {temp:?}°C, Humidity: {rel_humidity:?}, VOC: {voc:?}, NOx: {nox_index:?}, ready: {ready}"
"{NAME}: Temp: {temp:?}°C, Humidity: {rel_humidity:?}, \
VOC Index: {voc_index:?}, NOx Index: {nox_index:?}, ready: {ready}"
);
let pm1_0 = measurement.pm1_0();
let pm2_5 = measurement.pm2_5();
Expand All @@ -122,14 +127,7 @@ where
debug!("{NAME}: PM1.0: {pm1_0:?}, PM2.5: {pm2_5:?}, PM4.0: {pm4_0:?}, PM10.0: {pm10_0:?}, ready: {ready}");

if ready {
if let Some(humidity) = rel_humidity {
self.rel_humidity.set_value(humidity as f64);
}
if let Some(temp) = temp {
self.temp.set_value(temp as f64);
}

macro_rules! update_particulates {
macro_rules! update_metrics {
($($name:ident),+) => {
$(
if let Some(pm) = $name {
Expand All @@ -139,7 +137,16 @@ where
}
}

update_particulates!(pm1_0, pm2_5, pm4_0, pm10_0);
update_metrics!(
rel_humidity,
temp,
nox_index,
voc_index,
pm1_0,
pm2_5,
pm4_0,
pm10_0
);

if let (Some(temp), Some(humidity)) = (temp, rel_humidity) {
if self.polls.0 % self.abs_humidity_interval == 0 {
Expand Down

0 comments on commit 047dae2

Please sign in to comment.