@@ -639,7 +639,7 @@ float Measurements::getCorrectedTempHum(MeasurementType type, int ch, bool force
639
639
return corrected;
640
640
}
641
641
642
- float Measurements::getCorrectedPM25 (bool useAvg, int ch) {
642
+ float Measurements::getCorrectedPM25 (bool useAvg, int ch, bool forceCorrection ) {
643
643
float pm25;
644
644
float corrected;
645
645
float humidity;
@@ -658,12 +658,18 @@ float Measurements::getCorrectedPM25(bool useAvg, int ch) {
658
658
659
659
Configuration::PMCorrection pmCorrection = config.getPMCorrection ();
660
660
switch (pmCorrection.algorithm ) {
661
- case PMCorrectionAlgorithm::Unknown:
662
- case PMCorrectionAlgorithm::None:
663
- // If correction is Unknown, then default is None
664
- corrected = pm25;
661
+ case PMCorrectionAlgorithm::COR_ALGO_PM_UNKNOWN:
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
+ }
665
670
break ;
666
- case PMCorrectionAlgorithm::EPA_2021:
671
+ }
672
+ case PMCorrectionAlgorithm::COR_ALGO_PM_EPA_2021:
667
673
corrected = ag->pms5003 .compensate (pm25, humidity);
668
674
break ;
669
675
default : {
@@ -780,8 +786,8 @@ JSONVar Measurements::buildIndoor(bool localServer) {
780
786
// buildPMS params:
781
787
// / PMS channel 1 (indoor only have 1 PMS; hence allCh false)
782
788
// / 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 );
785
791
if (!localServer) {
786
792
// Indoor is using PMS5003
787
793
indoor[json_prop_pmFirmware] = this ->pms5003FirmwareVersion (ag->pms5003 .getFirmwareVersion ());
@@ -805,15 +811,6 @@ JSONVar Measurements::buildIndoor(bool localServer) {
805
811
}
806
812
}
807
813
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
-
817
814
return indoor;
818
815
}
819
816
@@ -826,58 +823,58 @@ JSONVar Measurements::buildPMS(int ch, bool allCh, bool withTempHum, bool compen
826
823
validateChannel (ch);
827
824
828
825
// Follow array indexing just for get address of the value type
829
- ch = ch - 1 ;
826
+ int chIndex = ch - 1 ;
830
827
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 );
833
830
}
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 );
836
833
}
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 );
839
836
}
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 );
842
839
}
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 );
845
842
}
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 );
848
845
}
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 );
851
848
}
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 );
854
851
}
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 );
857
854
}
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 );
860
857
}
861
- if (_pm_5_pc[ch ].listValues .empty () == false ) {
858
+ if (_pm_5_pc[chIndex ].listValues .empty () == false ) {
862
859
// Only include pm5.0 count when values available on its list
863
860
// 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 );
866
863
}
867
864
}
868
- if (_pm_10_pc[ch ].listValues .empty () == false ) {
865
+ if (_pm_10_pc[chIndex ].listValues .empty () == false ) {
869
866
// Only include pm10 count when values available on its list
870
867
// 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 );
873
870
}
874
871
}
875
872
876
873
if (withTempHum) {
877
874
float _vc;
878
875
// 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 );
881
878
// Compensate temperature when flag is set
882
879
if (compensate) {
883
880
_vc = getCorrectedTempHum (Temperature, ch, true );
@@ -887,8 +884,8 @@ JSONVar Measurements::buildPMS(int ch, bool allCh, bool withTempHum, bool compen
887
884
}
888
885
}
889
886
// 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 );
892
889
// Compensate relative humidity when flag is set
893
890
if (compensate) {
894
891
_vc = getCorrectedTempHum (Humidity, ch, true );
@@ -898,17 +895,14 @@ JSONVar Measurements::buildPMS(int ch, bool allCh, bool withTempHum, bool compen
898
895
}
899
896
}
900
897
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);
912
906
}
913
907
}
914
908
@@ -1156,12 +1150,12 @@ JSONVar Measurements::buildPMS(int ch, bool allCh, bool withTempHum, bool compen
1156
1150
float pm25_comp2 = utils::getInvalidPmValue ();
1157
1151
if (utils::isValidPm (_pm_25[0 ].update .avg ) &&
1158
1152
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 );
1160
1154
pms[" channels" ][" 1" ][json_prop_pm25Compensated] = ag->round2 (pm25_comp1);
1161
1155
}
1162
1156
if (utils::isValidPm (_pm_25[1 ].update .avg ) &&
1163
1157
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 );
1165
1159
pms[" channels" ][" 2" ][json_prop_pm25Compensated] = ag->round2 (pm25_comp2);
1166
1160
}
1167
1161
0 commit comments