Skip to content

Commit 1c42ff0

Browse files
committed
Make PM correction applied for all model
1 parent 17d2e62 commit 1c42ff0

File tree

3 files changed

+55
-61
lines changed

3 files changed

+55
-61
lines changed

src/AgConfigure.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ bool Configuration::updateTempHumCorrection(JSONVar &json, TempHumCorrection &ta
238238

239239
JSONVar corrections = json[jprop_corrections];
240240
if (!corrections.hasOwnProperty(correctionName)) {
241-
Serial.println("pm02 not found");
242241
logWarning(String(correctionName) + " correction field not found on configuration");
243242
return false;
244243
}

src/AgValue.cpp

Lines changed: 53 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ float Measurements::getCorrectedTempHum(MeasurementType type, int ch, bool force
639639
return corrected;
640640
}
641641

642-
float Measurements::getCorrectedPM25(bool useAvg, int ch) {
642+
float Measurements::getCorrectedPM25(bool useAvg, int ch, bool forceCorrection) {
643643
float pm25;
644644
float corrected;
645645
float humidity;
@@ -659,10 +659,16 @@ float Measurements::getCorrectedPM25(bool useAvg, int ch) {
659659
Configuration::PMCorrection pmCorrection = config.getPMCorrection();
660660
switch (pmCorrection.algorithm) {
661661
case PMCorrectionAlgorithm::COR_ALGO_PM_UNKNOWN:
662-
case PMCorrectionAlgorithm::COR_ALGO_PM_NONE:
663-
// If correction is Unknown, then default is None
664-
corrected = pm25;
662+
case PMCorrectionAlgorithm::COR_ALGO_PM_NONE: {
663+
// If correction is Unknown or None, then default is None
664+
// Unless forceCorrection enabled
665+
if (forceCorrection) {
666+
corrected = ag->pms5003.compensate(pm25, humidity);
667+
} else {
668+
corrected = pm25;
669+
}
665670
break;
671+
}
666672
case PMCorrectionAlgorithm::COR_ALGO_PM_EPA_2021:
667673
corrected = ag->pms5003.compensate(pm25, humidity);
668674
break;
@@ -780,8 +786,8 @@ JSONVar Measurements::buildIndoor(bool localServer) {
780786
// buildPMS params:
781787
/// PMS channel 1 (indoor only have 1 PMS; hence allCh false)
782788
/// Not include temperature and humidity from PMS sensor
783-
/// Not include compensated calculation
784-
indoor = buildPMS(1, false, false, false);
789+
/// Include compensated calculation
790+
indoor = buildPMS(1, false, false, true);
785791
if (!localServer) {
786792
// Indoor is using PMS5003
787793
indoor[json_prop_pmFirmware] = this->pms5003FirmwareVersion(ag->pms5003.getFirmwareVersion());
@@ -805,15 +811,6 @@ JSONVar Measurements::buildIndoor(bool localServer) {
805811
}
806812
}
807813

808-
// Add pm25 compensated value only if PM2.5 and humidity value is valid
809-
if (config.hasSensorPMS1 && utils::isValidPm(_pm_25[0].update.avg)) {
810-
if (config.hasSensorSHT && utils::isValidHumidity(_humidity[0].update.avg)) {
811-
// Correction using moving average value
812-
float tmp = getCorrectedPM25(true);
813-
indoor[json_prop_pm25Compensated] = ag->round2(tmp);
814-
}
815-
}
816-
817814
return indoor;
818815
}
819816

@@ -826,58 +823,58 @@ JSONVar Measurements::buildPMS(int ch, bool allCh, bool withTempHum, bool compen
826823
validateChannel(ch);
827824

828825
// Follow array indexing just for get address of the value type
829-
ch = ch - 1;
826+
int chIndex = ch - 1;
830827

831-
if (utils::isValidPm(_pm_01[ch].update.avg)) {
832-
pms[json_prop_pm01Ae] = ag->round2(_pm_01[ch].update.avg);
828+
if (utils::isValidPm(_pm_01[chIndex].update.avg)) {
829+
pms[json_prop_pm01Ae] = ag->round2(_pm_01[chIndex].update.avg);
833830
}
834-
if (utils::isValidPm(_pm_25[ch].update.avg)) {
835-
pms[json_prop_pm25Ae] = ag->round2(_pm_25[ch].update.avg);
831+
if (utils::isValidPm(_pm_25[chIndex].update.avg)) {
832+
pms[json_prop_pm25Ae] = ag->round2(_pm_25[chIndex].update.avg);
836833
}
837-
if (utils::isValidPm(_pm_10[ch].update.avg)) {
838-
pms[json_prop_pm10Ae] = ag->round2(_pm_10[ch].update.avg);
834+
if (utils::isValidPm(_pm_10[chIndex].update.avg)) {
835+
pms[json_prop_pm10Ae] = ag->round2(_pm_10[chIndex].update.avg);
839836
}
840-
if (utils::isValidPm(_pm_01_sp[ch].update.avg)) {
841-
pms[json_prop_pm01Sp] = ag->round2(_pm_01_sp[ch].update.avg);
837+
if (utils::isValidPm(_pm_01_sp[chIndex].update.avg)) {
838+
pms[json_prop_pm01Sp] = ag->round2(_pm_01_sp[chIndex].update.avg);
842839
}
843-
if (utils::isValidPm(_pm_25_sp[ch].update.avg)) {
844-
pms[json_prop_pm25Sp] = ag->round2(_pm_25_sp[ch].update.avg);
840+
if (utils::isValidPm(_pm_25_sp[chIndex].update.avg)) {
841+
pms[json_prop_pm25Sp] = ag->round2(_pm_25_sp[chIndex].update.avg);
845842
}
846-
if (utils::isValidPm(_pm_10_sp[ch].update.avg)) {
847-
pms[json_prop_pm10Sp] = ag->round2(_pm_10_sp[ch].update.avg);
843+
if (utils::isValidPm(_pm_10_sp[chIndex].update.avg)) {
844+
pms[json_prop_pm10Sp] = ag->round2(_pm_10_sp[chIndex].update.avg);
848845
}
849-
if (utils::isValidPm03Count(_pm_03_pc[ch].update.avg)) {
850-
pms[json_prop_pm03Count] = ag->round2(_pm_03_pc[ch].update.avg);
846+
if (utils::isValidPm03Count(_pm_03_pc[chIndex].update.avg)) {
847+
pms[json_prop_pm03Count] = ag->round2(_pm_03_pc[chIndex].update.avg);
851848
}
852-
if (utils::isValidPm03Count(_pm_05_pc[ch].update.avg)) {
853-
pms[json_prop_pm05Count] = ag->round2(_pm_05_pc[ch].update.avg);
849+
if (utils::isValidPm03Count(_pm_05_pc[chIndex].update.avg)) {
850+
pms[json_prop_pm05Count] = ag->round2(_pm_05_pc[chIndex].update.avg);
854851
}
855-
if (utils::isValidPm03Count(_pm_01_pc[ch].update.avg)) {
856-
pms[json_prop_pm1Count] = ag->round2(_pm_01_pc[ch].update.avg);
852+
if (utils::isValidPm03Count(_pm_01_pc[chIndex].update.avg)) {
853+
pms[json_prop_pm1Count] = ag->round2(_pm_01_pc[chIndex].update.avg);
857854
}
858-
if (utils::isValidPm03Count(_pm_25_pc[ch].update.avg)) {
859-
pms[json_prop_pm25Count] = ag->round2(_pm_25_pc[ch].update.avg);
855+
if (utils::isValidPm03Count(_pm_25_pc[chIndex].update.avg)) {
856+
pms[json_prop_pm25Count] = ag->round2(_pm_25_pc[chIndex].update.avg);
860857
}
861-
if (_pm_5_pc[ch].listValues.empty() == false) {
858+
if (_pm_5_pc[chIndex].listValues.empty() == false) {
862859
// Only include pm5.0 count when values available on its list
863860
// If not, means no pm5_pc available from the sensor
864-
if (utils::isValidPm03Count(_pm_5_pc[ch].update.avg)) {
865-
pms[json_prop_pm5Count] = ag->round2(_pm_5_pc[ch].update.avg);
861+
if (utils::isValidPm03Count(_pm_5_pc[chIndex].update.avg)) {
862+
pms[json_prop_pm5Count] = ag->round2(_pm_5_pc[chIndex].update.avg);
866863
}
867864
}
868-
if (_pm_10_pc[ch].listValues.empty() == false) {
865+
if (_pm_10_pc[chIndex].listValues.empty() == false) {
869866
// Only include pm10 count when values available on its list
870867
// If not, means no pm10_pc available from the sensor
871-
if (utils::isValidPm03Count(_pm_10_pc[ch].update.avg)) {
872-
pms[json_prop_pm10Count] = ag->round2(_pm_10_pc[ch].update.avg);
868+
if (utils::isValidPm03Count(_pm_10_pc[chIndex].update.avg)) {
869+
pms[json_prop_pm10Count] = ag->round2(_pm_10_pc[chIndex].update.avg);
873870
}
874871
}
875872

876873
if (withTempHum) {
877874
float _vc;
878875
// Set temperature if valid
879-
if (utils::isValidTemperature(_temperature[ch].update.avg)) {
880-
pms[json_prop_temp] = ag->round2(_temperature[ch].update.avg);
876+
if (utils::isValidTemperature(_temperature[chIndex].update.avg)) {
877+
pms[json_prop_temp] = ag->round2(_temperature[chIndex].update.avg);
881878
// Compensate temperature when flag is set
882879
if (compensate) {
883880
_vc = getCorrectedTempHum(Temperature, ch, true);
@@ -887,8 +884,8 @@ JSONVar Measurements::buildPMS(int ch, bool allCh, bool withTempHum, bool compen
887884
}
888885
}
889886
// Set humidity if valid
890-
if (utils::isValidHumidity(_humidity[ch].update.avg)) {
891-
pms[json_prop_rhum] = ag->round2(_humidity[ch].update.avg);
887+
if (utils::isValidHumidity(_humidity[chIndex].update.avg)) {
888+
pms[json_prop_rhum] = ag->round2(_humidity[chIndex].update.avg);
892889
// Compensate relative humidity when flag is set
893890
if (compensate) {
894891
_vc = getCorrectedTempHum(Humidity, ch, true);
@@ -898,17 +895,14 @@ JSONVar Measurements::buildPMS(int ch, bool allCh, bool withTempHum, bool compen
898895
}
899896
}
900897

901-
// Add pm25 compensated value only if PM2.5 and humidity value is valid
902-
if (compensate) {
903-
if (utils::isValidPm(_pm_25[ch].update.avg) &&
904-
utils::isValidHumidity(_humidity[ch].update.avg)) {
905-
// Note: the pms5003t object is not matter either for channel 1 or 2, compensate points to
906-
// the same base function
907-
float pm25 = ag->pms5003t_1.compensate(_pm_25[ch].update.avg, _humidity[ch].update.avg);
908-
if (utils::isValidPm(pm25)) {
909-
pms[json_prop_pm25Compensated] = ag->round2(pm25);
910-
}
911-
}
898+
}
899+
900+
// Add pm25 compensated value only if PM2.5 and humidity value is valid
901+
if (compensate) {
902+
if (utils::isValidPm(_pm_25[chIndex].update.avg) &&
903+
utils::isValidHumidity(_humidity[chIndex].update.avg)) {
904+
float pm25 = getCorrectedPM25(true, ch, true);
905+
pms[json_prop_pm25Compensated] = ag->round2(pm25);
912906
}
913907
}
914908

@@ -1156,12 +1150,12 @@ JSONVar Measurements::buildPMS(int ch, bool allCh, bool withTempHum, bool compen
11561150
float pm25_comp2 = utils::getInvalidPmValue();
11571151
if (utils::isValidPm(_pm_25[0].update.avg) &&
11581152
utils::isValidHumidity(_humidity[0].update.avg)) {
1159-
pm25_comp1 = ag->pms5003t_1.compensate(_pm_25[0].update.avg, _humidity[0].update.avg);
1153+
pm25_comp1 = getCorrectedPM25(true, 1, true);
11601154
pms["channels"]["1"][json_prop_pm25Compensated] = ag->round2(pm25_comp1);
11611155
}
11621156
if (utils::isValidPm(_pm_25[1].update.avg) &&
11631157
utils::isValidHumidity(_humidity[1].update.avg)) {
1164-
pm25_comp2 = ag->pms5003t_2.compensate(_pm_25[1].update.avg, _humidity[1].update.avg);
1158+
pm25_comp2 = getCorrectedPM25(true, 2, true);
11651159
pms["channels"]["2"][json_prop_pm25Compensated] = ag->round2(pm25_comp2);
11661160
}
11671161

src/AgValue.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,10 @@ class Measurements {
144144
*
145145
* @param useAvg Use moving average value if true, otherwise use latest value
146146
* @param ch MeasurementType channel
147+
* @param forceCorrection force using correction even though config correction is not applied, default to EPA
147148
* @return float Corrected PM2.5 value
148149
*/
149-
float getCorrectedPM25(bool useAvg = false, int ch = 1);
150+
float getCorrectedPM25(bool useAvg = false, int ch = 1, bool forceCorrection = false);
150151

151152
/**
152153
* build json payload for every measurements

0 commit comments

Comments
 (0)