Skip to content

Commit f972637

Browse files
authored
Merge pull request #283 from airgradienthq/feat/update-pm-correction
Apply PM corrections to all models
2 parents b0f5263 + 4c7e72b commit f972637

File tree

5 files changed

+96
-99
lines changed

5 files changed

+96
-99
lines changed

examples/OneOpenAir/OneOpenAir.ino

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ static void sendDataToAg() {
613613
}
614614
stateMachine.handleLeds(AgStateMachineWiFiOkServerConnectFailed);
615615
}
616-
delay(DISPLAY_DELAY_SHOW_CONTENT_MS);
616+
617617
stateMachine.handleLeds(AgStateMachineNormal);
618618
}
619619

@@ -908,6 +908,11 @@ static void configurationUpdateSchedule(void) {
908908
return;
909909
}
910910

911+
if (wifiConnector.isConnected() == false) {
912+
Serial.println(" WiFi not connected, skipping fetch configuration from AG server");
913+
return;
914+
}
915+
911916
if (apiClient.fetchServerConfiguration()) {
912917
configUpdateHandle();
913918
}
@@ -1008,6 +1013,7 @@ static void updateDisplayAndLedBar(void) {
10081013
if (wifiConnector.isConnected() == false) {
10091014
stateMachine.displayHandle(AgStateMachineWiFiLost);
10101015
stateMachine.handleLeds(AgStateMachineWiFiLost);
1016+
return;
10111017
}
10121018

10131019
if (configuration.isCloudConnectionDisabled()) {

src/AgConfigure.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,10 @@ const char *LED_BAR_MODE_NAMES[] = {
2222
};
2323

2424
const char *PM_CORRECTION_ALGORITHM_NAMES[] = {
25-
[Unknown] = "-", // This is only to pass "non-trivial designated initializers" error
26-
[None] = "none",
27-
[EPA_2021] = "epa_2021",
28-
[SLR_PMS5003_20220802] = "slr_PMS5003_20220802",
29-
[SLR_PMS5003_20220803] = "slr_PMS5003_20220803",
30-
[SLR_PMS5003_20220824] = "slr_PMS5003_20220824",
31-
[SLR_PMS5003_20231030] = "slr_PMS5003_20231030",
32-
[SLR_PMS5003_20231218] = "slr_PMS5003_20231218",
33-
[SLR_PMS5003_20240104] = "slr_PMS5003_20240104",
25+
[COR_ALGO_PM_UNKNOWN] = "-", // This is only to pass "non-trivial designated initializers" error
26+
[COR_ALGO_PM_NONE] = "none",
27+
[COR_ALGO_PM_EPA_2021] = "epa_2021",
28+
[COR_ALGO_PM_SLR_CUSTOM] = "custom",
3429
};
3530

3631
const char *TEMP_HUM_CORRECTION_ALGORITHM_NAMES[] = {
@@ -115,8 +110,8 @@ PMCorrectionAlgorithm Configuration::matchPmAlgorithm(String algorithm) {
115110
// If the input string matches an algorithm name, return the corresponding enum value
116111
// Else return Unknown
117112

118-
const size_t enumSize = SLR_PMS5003_20240104 + 1; // Get the actual size of the enum
119-
PMCorrectionAlgorithm result = PMCorrectionAlgorithm::Unknown;
113+
const size_t enumSize = COR_ALGO_PM_SLR_CUSTOM + 1; // Get the actual size of the enum
114+
PMCorrectionAlgorithm result = COR_ALGO_PM_UNKNOWN;;
120115

121116
// Loop through enum values
122117
for (size_t enumVal = 0; enumVal < enumSize; enumVal++) {
@@ -125,6 +120,15 @@ PMCorrectionAlgorithm Configuration::matchPmAlgorithm(String algorithm) {
125120
}
126121
}
127122

123+
// If string not match from enum, check if correctionAlgorithm is one of the PM batch corrections
124+
if (result == COR_ALGO_PM_UNKNOWN) {
125+
// Check the substring "slr_PMS5003_xxxxxxxx"
126+
if (algorithm.substring(0, 11) == "slr_PMS5003") {
127+
// If it is, then its a custom correction
128+
result = COR_ALGO_PM_SLR_CUSTOM;
129+
}
130+
}
131+
128132
return result;
129133
}
130134

@@ -145,36 +149,34 @@ TempHumCorrectionAlgorithm Configuration::matchTempHumAlgorithm(String algorithm
145149

146150
bool Configuration::updatePmCorrection(JSONVar &json) {
147151
if (!json.hasOwnProperty("corrections")) {
148-
Serial.println("corrections not found");
152+
logInfo("corrections not found");
149153
return false;
150154
}
151155

152156
JSONVar corrections = json["corrections"];
153157
if (!corrections.hasOwnProperty("pm02")) {
154-
Serial.println("pm02 not found");
158+
logWarning("pm02 not found");
155159
return false;
156160
}
157161

158162
JSONVar pm02 = corrections["pm02"];
159163
if (!pm02.hasOwnProperty("correctionAlgorithm")) {
160-
Serial.println("correctionAlgorithm not found");
164+
logWarning("pm02 correctionAlgorithm not found");
161165
return false;
162166
}
163167

164-
// TODO: Need to have data type check, with error message response if invalid
165-
166168
// Check algorithm
167169
String algorithm = pm02["correctionAlgorithm"];
168170
PMCorrectionAlgorithm algo = matchPmAlgorithm(algorithm);
169-
if (algo == Unknown) {
170-
logInfo("Unknown algorithm");
171+
if (algo == COR_ALGO_PM_UNKNOWN) {
172+
logWarning("Unknown algorithm");
171173
return false;
172174
}
173175
logInfo("Correction algorithm: " + algorithm);
174176

175177
// If algo is None or EPA_2021, no need to check slr
176178
// But first check if pmCorrection different from algo
177-
if (algo == None || algo == EPA_2021) {
179+
if (algo == COR_ALGO_PM_NONE || algo == COR_ALGO_PM_EPA_2021) {
178180
if (pmCorrection.algorithm != algo) {
179181
// Deep copy corrections from root to jconfig, so it will be saved later
180182
jconfig[jprop_corrections]["pm02"]["correctionAlgorithm"] = algorithm;
@@ -191,7 +193,7 @@ bool Configuration::updatePmCorrection(JSONVar &json) {
191193

192194
// Check if pm02 has slr object
193195
if (!pm02.hasOwnProperty("slr")) {
194-
Serial.println("slr not found");
196+
logWarning("slr not found");
195197
return false;
196198
}
197199

@@ -200,7 +202,7 @@ bool Configuration::updatePmCorrection(JSONVar &json) {
200202
// Validate required slr properties exist
201203
if (!slr.hasOwnProperty("intercept") || !slr.hasOwnProperty("scalingFactor") ||
202204
!slr.hasOwnProperty("useEpa2021")) {
203-
Serial.println("Missing required slr properties");
205+
logWarning("Missing required slr properties");
204206
return false;
205207
}
206208

@@ -238,7 +240,6 @@ bool Configuration::updateTempHumCorrection(JSONVar &json, TempHumCorrection &ta
238240

239241
JSONVar corrections = json[jprop_corrections];
240242
if (!corrections.hasOwnProperty(correctionName)) {
241-
Serial.println("pm02 not found");
242243
logWarning(String(correctionName) + " correction field not found on configuration");
243244
return false;
244245
}
@@ -397,8 +398,8 @@ void Configuration::defaultConfig(void) {
397398
jconfig[jprop_offlineMode] = jprop_offlineMode_default;
398399
jconfig[jprop_monitorDisplayCompensatedValues] = jprop_monitorDisplayCompensatedValues_default;
399400

400-
// PM2.5 correction
401-
pmCorrection.algorithm = None;
401+
// PM2.5 default correction
402+
pmCorrection.algorithm = COR_ALGO_PM_NONE;
402403
pmCorrection.changed = false;
403404
pmCorrection.intercept = 0;
404405
pmCorrection.scalingFactor = 1;
@@ -1380,7 +1381,7 @@ void Configuration::toConfig(const char *buf) {
13801381

13811382
// PM2.5 correction
13821383
/// Set default first before parsing local config
1383-
pmCorrection.algorithm = PMCorrectionAlgorithm::None;
1384+
pmCorrection.algorithm = COR_ALGO_PM_NONE;
13841385
pmCorrection.intercept = 0;
13851386
pmCorrection.scalingFactor = 0;
13861387
pmCorrection.useEPA = false;
@@ -1526,8 +1527,8 @@ bool Configuration::isPMCorrectionChanged(void) {
15261527
*/
15271528
bool Configuration::isPMCorrectionEnabled(void) {
15281529
PMCorrection pmCorrection = getPMCorrection();
1529-
if (pmCorrection.algorithm == PMCorrectionAlgorithm::None ||
1530-
pmCorrection.algorithm == PMCorrectionAlgorithm::Unknown) {
1530+
if (pmCorrection.algorithm == COR_ALGO_PM_NONE ||
1531+
pmCorrection.algorithm == COR_ALGO_PM_UNKNOWN) {
15311532
return false;
15321533
}
15331534

src/AgValue.cpp

Lines changed: 55 additions & 61 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;
@@ -658,12 +658,18 @@ float Measurements::getCorrectedPM25(bool useAvg, int ch) {
658658

659659
Configuration::PMCorrection pmCorrection = config.getPMCorrection();
660660
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+
}
665670
break;
666-
case PMCorrectionAlgorithm::EPA_2021:
671+
}
672+
case PMCorrectionAlgorithm::COR_ALGO_PM_EPA_2021:
667673
corrected = ag->pms5003.compensate(pm25, humidity);
668674
break;
669675
default: {
@@ -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)