Compare commits

...

21 Commits

Author SHA1 Message Date
79fbd901bd Merge branch 'develop' 2024-11-19 18:45:31 +07:00
3644dc43fe Prepared to release 3.1.12 2024-11-19 18:44:23 +07:00
03fa62d8f0 Merge pull request #263 from airgradienthq/fix/correction
Fix EPA compensated on top of SLR correction
2024-11-19 02:20:51 +07:00
902a768f28 Handle parsing invalid json string 2024-11-19 01:45:50 +07:00
1de9344f43 Fix typo on docs 2024-11-18 22:50:36 +07:00
46f6309b77 Fix use the right function 2024-11-18 22:42:43 +07:00
a6b48acb41 CO2 led bar indicator sync fix 2024-11-18 21:24:47 +07:00
1b4d89e1a1 fix correction on top of compensation 2024-11-18 19:53:28 +07:00
0d2b0fb657 Fix typo on local-server.md
Fix curl correction command example typos
2024-11-17 11:18:39 +07:00
9f08af44b0 Prepared to release 3.1.11 2024-11-11 20:04:47 +07:00
6b661cdeb7 Merge pull request #261 from airgradienthq/feat/allavg
Values on display and led bar using measurement average values
2024-11-11 12:40:48 +07:00
dc299c4b54 Fix pm2.5 not using getAverage for ledbar 2024-11-10 19:20:09 +07:00
2f595b4e41 Update local server docs correction examples 2024-11-10 04:26:54 +07:00
a30535f75f Use avg value for display and led bar 2024-11-10 04:13:58 +07:00
a513943cba New agvalue member to get avg values 2024-11-09 23:27:29 +07:00
96bb6952fb correction function return raw if algorithm is none 2024-11-09 21:28:33 +07:00
10653bfe26 Fix pms5003 correction default value 2024-11-09 20:55:41 +07:00
c7f89fa7b7 Decrease period length moving average to 80% 2024-11-09 20:53:52 +07:00
b11c461b60 Merge pull request #260 from airgradienthq/fix/led-flicker
Fix flicker on led bar led state change
2024-11-09 20:42:01 +07:00
404c14aad2 Fix typo and comment 2024-11-07 22:08:36 +07:00
bfbae680fd Fix led bar flicker when state change
clear only neccessary led
2024-11-07 22:03:01 +07:00
10 changed files with 244 additions and 107 deletions

View File

@ -156,13 +156,13 @@ If the monitor is set up on the AirGradient dashboard, it will also receive the
| `tvocLearningOffset` | Set VOC learning gain offset. | Number | 0-720 (default 12) | `{"tvocLearningOffset": 12}` |
| `offlineMode` | Set monitor to run without WiFi. | Boolean | `false`: Disabled (default) <br> `true`: Enabled | `{"offlineMode": true}` |
| `monitorDisplayCompensatedValues` | Set the display show the PM value with/without compensate value (only on [3.1.9]()) | Boolean | `false`: Without compensate (default) <br> `true`: with compensate | `{"monitorDisplayCompensatedValues": false }` |
| `corrections` | Sets correction options to display and measurement values on local server response. | Object | _see corretions section_ | _see corretions section_ |
| `corrections` | Sets correction options to display and measurement values on local server response. (version >= [3.1.11]()) | Object | _see corrections section_ | _see corrections section_ |
#### Corrections
The `corrections` object allows configuring PM2.5 correction algorithms and parameters. This affects both the display and local server response values.
The `corrections` object allows configuring PM2.5 correction algorithms and parameters locally. This affects both the display and local server response values.
Example correction configuration:
@ -189,13 +189,28 @@ Example correction configuration:
| PMS5003_20231218 | `"slr_PMS5003_20231218"` | Correction for PMS5003 sensor batch 20231218| Yes |
| PMS5003_20231030 | `"slr_PMS5003_20231030"` | Correction for PMS5003 sensor batch 20231030| Yes |
**Notes**:
**NOTES**:
- Set `useEpa2021` to true if want to apply EPA 2021 correction factors on top of SLR correction value.
- Set `useEpa2021` to `true` if want to apply EPA 2021 correction factors on top of SLR correction value, otherwise `false`
- `intercept` and `scalingFactor` values can be obtained from [this article](https://www.airgradient.com/blog/low-readings-from-pms5003/)
- If `configurationControl` is set to `local` (eg. when using Home Assistant), correction need to be set manually, see examples below
**Example**:
**Examples**:
- PMS5003_20231030
```bash
curl --location -X PUT 'http://airgradient_84fce612eff4.local/config' --header 'Content-Type: application/json' --data '{"corrections":{"pm02":{"correctionAlgorithm":"slr_PMS5003_20231030","slr":{"intercept":0,"scalingFactor":0.02838,"useEpa2021":false}}}}'
```
curl --location -X PUT 'http://airgradient_84fce612eff4.local/config' --header 'Content-Type: application/json' --data '{"corrections":{"pm02":{"correctionAlgorithm":"slr_PMS5003_20231030","slr":{"intercept":0,"scalingFactor":0.02838,"useEpa2021":true}}}}'
```
- PMS5003_20231218
```bash
curl --location -X PUT 'http://airgradient_84fce612eff4.local/config' --header 'Content-Type: application/json' --data '{"corrections":{"pm02":{"correctionAlgorithm":"slr_PMS5003_20231218","slr":{"intercept":0,"scalingFactor":0.03525,"useEpa2021":true}}}}'
```
- PMS5003_20240104
```bash
curl --location -X PUT 'http://airgradient_84fce612eff4.local/config' --header 'Content-Type: application/json' --data '{"corrections":{"pm02":{"correctionAlgorithm":"slr_PMS5003_20240104","slr":{"intercept":0,"scalingFactor":0.02896,"useEpa2021":true}}}}'
```

View File

@ -168,7 +168,7 @@ void setup() {
boardInit();
setMeasurementMaxPeriod();
// Uncomment below line to print every measurements reading update
// Comment below line to disable debug measurement readings
measurements.setDebug(true);
/** Connecting wifi */
@ -1216,6 +1216,6 @@ void setMeasurementMaxPeriod() {
}
int calculateMaxPeriod(int updateInterval) {
// 0.5 is 50% reduced interval for max period
return (SERVER_SYNC_INTERVAL - (SERVER_SYNC_INTERVAL * 0.5)) / updateInterval;
// 0.8 is 80% reduced interval for max period
return (SERVER_SYNC_INTERVAL - (SERVER_SYNC_INTERVAL * 0.8)) / updateInterval;
}

View File

@ -1,5 +1,5 @@
name=AirGradient Air Quality Sensor
version=3.1.10
version=3.1.12
author=AirGradient <support@airgradient.com>
maintainer=AirGradient <support@airgradient.com>
sentence=ESP32-C3 / ESP8266 library for air quality monitor measuring PM, CO2, Temperature, TVOC and Humidity with OLED display.

View File

@ -292,8 +292,8 @@ void Configuration::defaultConfig(void) {
// PM2.5 correction
pmCorrection.algorithm = None;
pmCorrection.changed = false;
pmCorrection.intercept = -1;
pmCorrection.scalingFactor = -1;
pmCorrection.intercept = 0;
pmCorrection.scalingFactor = 1;
pmCorrection.useEPA = false;
saveConfig();
@ -354,16 +354,16 @@ bool Configuration::begin(void) {
* @return false Failure
*/
bool Configuration::parse(String data, bool isLocal) {
logInfo("Parse configure: " + data);
logInfo("Parsing configuration: " + data);
JSONVar root = JSON.parse(data);
failedMessage = "";
if (root == undefined) {
if (root == undefined || JSONVar::typeof_(root) != "object") {
logError("Parse configuration failed, JSON invalid (" + JSONVar::typeof_(root) + ")");
failedMessage = "JSON invalid";
logError(failedMessage);
return false;
}
logInfo("Parse configure success");
logInfo("Parse configuration success");
/** Is configuration changed */
bool changed = false;
@ -1369,7 +1369,12 @@ bool Configuration::isPMCorrectionChanged(void) {
*/
bool Configuration::isPMCorrectionEnabled(void) {
PMCorrection pmCorrection = getPMCorrection();
return pmCorrection.algorithm != PMCorrectionAlgorithm::None;
if (pmCorrection.algorithm == PMCorrectionAlgorithm::None ||
pmCorrection.algorithm == PMCorrectionAlgorithm::Unknown) {
return false;
}
return true;
}
Configuration::PMCorrection Configuration::getPMCorrection(void) {

View File

@ -12,7 +12,7 @@
*/
void OledDisplay::showTempHum(bool hasStatus, char *buf, int buf_size) {
/** Temperature */
float temp = value.getFloat(Measurements::Temperature);
float temp = value.getAverage(Measurements::Temperature);
if (utils::isValidTemperature(temp)) {
float t = 0.0f;
if (config.isTemperatureUnitInF()) {
@ -44,7 +44,7 @@ void OledDisplay::showTempHum(bool hasStatus, char *buf, int buf_size) {
DISP()->drawUTF8(1, 10, buf);
/** Show humidity */
int rhum = (int)value.getFloat(Measurements::Humidity);
int rhum = round(value.getAverage(Measurements::Humidity));
if (utils::isValidHumidity(rhum)) {
snprintf(buf, buf_size, "%d%%", rhum);
} else {
@ -292,7 +292,7 @@ void OledDisplay::showDashboard(const char *status) {
DISP()->drawUTF8(1, 27, "CO2");
DISP()->setFont(u8g2_font_t0_22b_tf);
int co2 = value.get(Measurements::CO2);
int co2 = round(value.getAverage(Measurements::CO2));
if (utils::isValidCO2(co2)) {
sprintf(strBuf, "%d", co2);
} else {
@ -313,11 +313,10 @@ void OledDisplay::showDashboard(const char *status) {
DISP()->drawStr(55, 27, "PM2.5");
/** Draw PM2.5 value */
int pm25 = value.get(Measurements::PM25);
int pm25 = round(value.getAverage(Measurements::PM25));
if (utils::isValidPm(pm25)) {
if (config.hasSensorSHT && config.isPMCorrectionEnabled()) {
pm25 = (int)value.getCorrectedPM25(*ag, config);
pm25 = round(value.getCorrectedPM25(*ag, config, true));
}
if (config.isPmStandardInUSAQI()) {
sprintf(strBuf, "%d", ag->pms5003.convertPm25ToUsAqi(pm25));
@ -343,7 +342,7 @@ void OledDisplay::showDashboard(const char *status) {
DISP()->drawStr(100, 27, "VOC:");
/** Draw tvocIndexvalue */
int tvoc = value.get(Measurements::TVOC);
int tvoc = round(value.getAverage(Measurements::TVOC));
if (utils::isValidVOC(tvoc)) {
sprintf(strBuf, "%d", tvoc);
} else {
@ -352,7 +351,7 @@ void OledDisplay::showDashboard(const char *status) {
DISP()->drawStr(100, 39, strBuf);
/** Draw NOx label */
int nox = value.get(Measurements::NOx);
int nox = round(value.getAverage(Measurements::NOx));
DISP()->drawStr(100, 53, "NOx:");
if (utils::isValidNOx(nox)) {
sprintf(strBuf, "%d", nox);
@ -365,7 +364,7 @@ void OledDisplay::showDashboard(const char *status) {
ag->display.clear();
/** Set CO2 */
int co2 = value.get(Measurements::CO2);
int co2 = round(value.getAverage(Measurements::CO2));
if (utils::isValidCO2(co2)) {
snprintf(strBuf, sizeof(strBuf), "CO2:%d", co2);
} else {
@ -376,9 +375,9 @@ void OledDisplay::showDashboard(const char *status) {
ag->display.setText(strBuf);
/** Set PM */
int pm25 = value.get(Measurements::PM25);
int pm25 = round(value.getAverage(Measurements::PM25));
if (config.hasSensorSHT && config.isPMCorrectionEnabled()) {
pm25 = (int)value.getCorrectedPM25(*ag, config);
pm25 = round(value.getCorrectedPM25(*ag, config, true));
}
ag->display.setCursor(0, 12);
@ -390,7 +389,7 @@ void OledDisplay::showDashboard(const char *status) {
ag->display.setText(strBuf);
/** Set temperature and humidity */
float temp = value.getFloat(Measurements::Temperature);
float temp = value.getAverage(Measurements::Temperature);
if (utils::isValidTemperature(temp)) {
if (config.isTemperatureUnitInF()) {
snprintf(strBuf, sizeof(strBuf), "T:%0.1f F", utils::degreeC_To_F(temp));
@ -408,7 +407,7 @@ void OledDisplay::showDashboard(const char *status) {
ag->display.setCursor(0, 24);
ag->display.setText(strBuf);
int rhum = (int)value.getFloat(Measurements::Humidity);
int rhum = round(value.getAverage(Measurements::Humidity));
if (utils::isValidHumidity(rhum)) {
snprintf(strBuf, sizeof(strBuf), "H:%d %%", rhum);
} else {

View File

@ -13,6 +13,7 @@
#define RGB_COLOR_Y 255, 150, 0 /** Yellow */
#define RGB_COLOR_O 255, 40, 0 /** Orange */
#define RGB_COLOR_P 180, 0, 255 /** Purple */
#define RGB_COLOR_CLEAR 0, 0, 0 /** No color */
/**
* @brief Animation LED bar with color
@ -47,55 +48,76 @@ void StateMachine::ledStatusBlinkDelay(uint32_t ms) {
}
/**
* @brief Led bar show led color status
* @brief Led bar show PM or CO2 led color status
*
* @return true if all led bar are used, false othwerwise
*/
void StateMachine::sensorhandleLeds(void) {
bool StateMachine::sensorhandleLeds(void) {
int totalLedUsed = 0;
switch (config.getLedBarMode()) {
case LedBarMode::LedBarModeCO2:
co2handleLeds();
totalLedUsed = co2handleLeds();
break;
case LedBarMode::LedBarModePm:
pm25handleLeds();
totalLedUsed = pm25handleLeds();
break;
default:
ag->ledBar.clear();
break;
}
if (totalLedUsed == ag->ledBar.getNumberOfLeds()) {
return true;
}
// Clear the rest of unused led
int startIndex = totalLedUsed + 1;
for (int i = startIndex; i <= ag->ledBar.getNumberOfLeds(); i++) {
ag->ledBar.setColor(RGB_COLOR_CLEAR, ag->ledBar.getNumberOfLeds() - i);
}
return false;
}
/**
* @brief Show CO2 LED status
*
* @return return total number of led that are used on the monitor
*/
void StateMachine::co2handleLeds(void) {
int co2Value = value.get(Measurements::CO2);
if (co2Value <= 700) {
int StateMachine::co2handleLeds(void) {
int totalUsed = ag->ledBar.getNumberOfLeds();
int co2Value = round(value.getAverage(Measurements::CO2));
if (co2Value <= 600) {
/** G; 1 */
ag->ledBar.setColor(RGB_COLOR_G, ag->ledBar.getNumberOfLeds() - 1);
} else if (co2Value <= 1000) {
totalUsed = 1;
} else if (co2Value <= 800) {
/** GG; 2 */
ag->ledBar.setColor(RGB_COLOR_G, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_G, ag->ledBar.getNumberOfLeds() - 2);
} else if (co2Value <= 1333) {
totalUsed = 2;
} else if (co2Value <= 1000) {
/** YYY; 3 */
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 2);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 3);
} else if (co2Value <= 1666) {
totalUsed = 3;
} else if (co2Value <= 1250) {
/** OOOO; 4 */
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 2);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 3);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 4);
} else if (co2Value <= 2000) {
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 2);
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 3);
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 4);
totalUsed = 4;
} else if (co2Value <= 1500) {
/** OOOOO; 5 */
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 2);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 3);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 4);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 5);
} else if (co2Value <= 2666) {
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 2);
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 3);
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 4);
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 5);
totalUsed = 5;
} else if (co2Value <= 1750) {
/** RRRRRR; 6 */
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 2);
@ -103,7 +125,8 @@ void StateMachine::co2handleLeds(void) {
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 4);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 5);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 6);
} else if (co2Value <= 3333) {
totalUsed = 6;
} else if (co2Value <= 2000) {
/** RRRRRRR; 7 */
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 2);
@ -112,17 +135,19 @@ void StateMachine::co2handleLeds(void) {
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 5);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 6);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 7);
} else if (co2Value <= 4000) {
/** RRRRRRRR; 8 */
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 2);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 3);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 4);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 5);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 6);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 7);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 8);
} else { /** > 4000 */
totalUsed = 7;
} else if (co2Value <= 3000) {
/** PPPPPPPP; 8 */
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 2);
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 3);
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 4);
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 5);
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 6);
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 7);
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 8);
totalUsed = 8;
} else { /** > 3000 */
/* PRPRPRPRP; 9 */
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 2);
@ -133,37 +158,47 @@ void StateMachine::co2handleLeds(void) {
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 7);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 8);
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 9);
totalUsed = 9;
}
return totalUsed;
}
/**
* @brief Show PM2.5 LED status
*
*
* @return return total number of led that are used on the monitor
*/
void StateMachine::pm25handleLeds(void) {
int pm25Value = value.get(Measurements::PM25);
int StateMachine::pm25handleLeds(void) {
int totalUsed = ag->ledBar.getNumberOfLeds();
int pm25Value = round(value.getAverage(Measurements::PM25));
if (config.hasSensorSHT && config.isPMCorrectionEnabled()) {
pm25Value = (int)value.getCorrectedPM25(*ag, config);
pm25Value = round(value.getCorrectedPM25(*ag, config, true));
}
if (pm25Value <= 5) {
/** G; 1 */
ag->ledBar.setColor(RGB_COLOR_G, ag->ledBar.getNumberOfLeds() - 1);
totalUsed = 1;
} else if (pm25Value <= 9) {
/** GG; 2 */
ag->ledBar.setColor(RGB_COLOR_G, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_G, ag->ledBar.getNumberOfLeds() - 2);
totalUsed = 2;
} else if (pm25Value <= 20) {
/** YYY; 3 */
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 2);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 3);
totalUsed = 3;
} else if (pm25Value <= 35) {
/** YYYY; 4 */
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 1);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 2);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 3);
ag->ledBar.setColor(RGB_COLOR_Y, ag->ledBar.getNumberOfLeds() - 4);
totalUsed = 4;
} else if (pm25Value <= 45) {
/** OOOOO; 5 */
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 1);
@ -171,6 +206,7 @@ void StateMachine::pm25handleLeds(void) {
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 3);
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 4);
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 5);
totalUsed = 5;
} else if (pm25Value <= 55) {
/** OOOOOO; 6 */
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 1);
@ -179,6 +215,7 @@ void StateMachine::pm25handleLeds(void) {
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 4);
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 5);
ag->ledBar.setColor(RGB_COLOR_O, ag->ledBar.getNumberOfLeds() - 6);
totalUsed = 6;
} else if (pm25Value <= 100) {
/** RRRRRRR; 7 */
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 1);
@ -188,6 +225,7 @@ void StateMachine::pm25handleLeds(void) {
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 5);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 6);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 7);
totalUsed = 7;
} else if (pm25Value <= 125) {
/** RRRRRRRR; 8 */
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 1);
@ -198,6 +236,7 @@ void StateMachine::pm25handleLeds(void) {
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 6);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 7);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 8);
totalUsed = 8;
} else if (pm25Value <= 225) {
/** PPPPPPPPP; 9 */
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 1);
@ -209,6 +248,7 @@ void StateMachine::pm25handleLeds(void) {
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 7);
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 8);
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 9);
totalUsed = 9;
} else { /** > 225 */
/* PRPRPRPRP; 9 */
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 1);
@ -220,7 +260,10 @@ void StateMachine::pm25handleLeds(void) {
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 7);
ag->ledBar.setColor(RGB_COLOR_R, ag->ledBar.getNumberOfLeds() - 8);
ag->ledBar.setColor(RGB_COLOR_P, ag->ledBar.getNumberOfLeds() - 9);
totalUsed = 9;
}
return totalUsed;
}
void StateMachine::co2Calibration(void) {
@ -311,6 +354,7 @@ void StateMachine::co2Calibration(void) {
void StateMachine::ledBarTest(void) {
if (config.isLedBarTestRequested()) {
if (ag->isOne()) {
ag->ledBar.clear();
if (config.getCountry() == "TH") {
uint32_t tstart = millis();
logInfo("Start run LED test for 2 min");
@ -332,7 +376,12 @@ void StateMachine::ledBarTest(void) {
}
}
void StateMachine::ledBarPowerUpTest(void) { ledBarRunTest(); }
void StateMachine::ledBarPowerUpTest(void) {
if (ag->isOne()) {
ag->ledBar.clear();
}
ledBarRunTest();
}
void StateMachine::ledBarRunTest(void) {
if (ag->isOne()) {
@ -585,15 +634,13 @@ void StateMachine::handleLeds(AgStateMachineState state) {
}
ledState = state;
if (ag->isOne()) {
ag->ledBar.clear(); // Set all LED OFF
}
switch (state) {
case AgStateMachineWiFiManagerMode: {
/** In WiFi Manager Mode */
/** Turn LED OFF */
/** Turn middle LED Color */
if (ag->isOne()) {
ag->ledBar.clear();
ag->ledBar.setColor(0, 0, 255, ag->ledBar.getNumberOfLeds() / 2);
} else {
ag->statusLed.setToggle();
@ -603,6 +650,7 @@ void StateMachine::handleLeds(AgStateMachineState state) {
case AgStateMachineWiFiManagerPortalActive: {
/** WiFi Manager has connected to mobile phone */
if (ag->isOne()) {
ag->ledBar.clear();
ag->ledBar.setColor(0, 0, 255);
} else {
ag->statusLed.setOn();
@ -613,6 +661,7 @@ void StateMachine::handleLeds(AgStateMachineState state) {
/** after SSID and PW entered and OK clicked, connection to WiFI network is
* attempted */
if (ag->isOne()) {
ag->ledBar.clear();
ledBarSingleLedAnimation(255, 255, 255);
} else {
ag->statusLed.setOff();
@ -622,6 +671,7 @@ void StateMachine::handleLeds(AgStateMachineState state) {
case AgStateMachineWiFiManagerStaConnected: {
/** Connecting to WiFi worked */
if (ag->isOne()) {
ag->ledBar.clear();
ag->ledBar.setColor(255, 255, 255);
} else {
ag->statusLed.setOff();
@ -631,6 +681,7 @@ void StateMachine::handleLeds(AgStateMachineState state) {
case AgStateMachineWiFiOkServerConnecting: {
/** once connected to WiFi an attempt to reach the server is performed */
if (ag->isOne()) {
ag->ledBar.clear();
ledBarSingleLedAnimation(0, 255, 0);
} else {
ag->statusLed.setOff();
@ -640,6 +691,7 @@ void StateMachine::handleLeds(AgStateMachineState state) {
case AgStateMachineWiFiOkServerConnected: {
/** Server is reachable, all fine */
if (ag->isOne()) {
ag->ledBar.clear();
ag->ledBar.setColor(0, 255, 0);
} else {
ag->statusLed.setOff();
@ -656,6 +708,7 @@ void StateMachine::handleLeds(AgStateMachineState state) {
case AgStateMachineWiFiManagerConnectFailed: {
/** Cannot connect to WiFi (e.g. wrong password, WPA Enterprise etc.) */
if (ag->isOne()) {
ag->ledBar.clear();
ag->ledBar.setColor(255, 0, 0);
} else {
ag->statusLed.setOff();
@ -674,6 +727,7 @@ void StateMachine::handleLeds(AgStateMachineState state) {
/** Connected to WiFi but server not reachable, e.g. firewall block/
* whitelisting needed etc. */
if (ag->isOne()) {
ag->ledBar.clear();
ag->ledBar.setColor(233, 183, 54); /** orange */
} else {
ag->statusLed.setOff();
@ -690,6 +744,7 @@ void StateMachine::handleLeds(AgStateMachineState state) {
case AgStateMachineWiFiOkServerOkSensorConfigFailed: {
/** Server reachable but sensor not configured correctly */
if (ag->isOne()) {
ag->ledBar.clear();
ag->ledBar.setColor(139, 24, 248); /** violet */
} else {
ag->statusLed.setOff();
@ -707,11 +762,10 @@ void StateMachine::handleLeds(AgStateMachineState state) {
/** Connection to WiFi network failed credentials incorrect encryption not
* supported etc. */
if (ag->isOne()) {
/** WIFI failed status LED color */
ag->ledBar.setColor(255, 0, 0, 0);
/** Show CO2 or PM color status */
// sensorLedColorHandler();
sensorhandleLeds();
bool allUsed = sensorhandleLeds();
if (allUsed == false) {
ag->ledBar.setColor(255, 0, 0, 0);
}
} else {
ag->statusLed.setOff();
}
@ -721,11 +775,10 @@ void StateMachine::handleLeds(AgStateMachineState state) {
/** Connected to WiFi network but the server cannot be reached through the
* internet, e.g. blocked by firewall */
if (ag->isOne()) {
ag->ledBar.setColor(233, 183, 54, 0);
/** Show CO2 or PM color status */
sensorhandleLeds();
// sensorLedColorHandler();
bool allUsed = sensorhandleLeds();
if (allUsed == false) {
ag->ledBar.setColor(233, 183, 54, 0);
}
} else {
ag->statusLed.setOff();
}
@ -735,10 +788,10 @@ void StateMachine::handleLeds(AgStateMachineState state) {
/** Server is reachable but there is some configuration issue to be fixed on
* the server side */
if (ag->isOne()) {
ag->ledBar.setColor(139, 24, 248, 0);
/** Show CO2 or PM color status */
sensorhandleLeds();
bool allUsed = sensorhandleLeds();
if (allUsed == false) {
ag->ledBar.setColor(139, 24, 248, 0);
}
} else {
ag->statusLed.setOff();
}

View File

@ -24,9 +24,9 @@ private:
void ledBarSingleLedAnimation(uint8_t r, uint8_t g, uint8_t b);
void ledStatusBlinkDelay(uint32_t delay);
void sensorhandleLeds(void);
void co2handleLeds(void);
void pm25handleLeds(void);
bool sensorhandleLeds(void);
int co2handleLeds(void);
int pm25handleLeds(void);
void co2Calibration(void);
void ledBarTest(void);
void ledBarPowerUpTest(void);

View File

@ -396,6 +396,52 @@ float Measurements::getFloat(MeasurementType type, int ch) {
return temporary->listValues.back();
}
float Measurements::getAverage(MeasurementType type, int ch) {
// Sanity check to validate channel, assert if invalid
validateChannel(ch);
// Follow array indexing just for get address of the value type
ch = ch - 1;
// Define data point source. Data type doesn't matter because only to get the average value
FloatValue *temporary = nullptr;
Update update;
float measurementAverage;
switch (type) {
case CO2:
measurementAverage = _co2.update.avg;
break;
case TVOC:
measurementAverage = _tvoc.update.avg;
break;
case NOx:
measurementAverage = _nox.update.avg;
break;
case PM25:
measurementAverage = _pm_25[ch].update.avg;
break;
case Temperature:
measurementAverage = _temperature[ch].update.avg;
break;
case Humidity:
measurementAverage = _humidity[ch].update.avg;
break;
default:
// Invalidate, measurements type not handled
measurementAverage = -1000;
break;
};
// Sanity check if measurement type is not defined
if (measurementAverage == -1000) {
Serial.printf("ERROR! %s is not defined on get average value function\n", measurementTypeStr(type));
delay(1000);
assert(0);
}
return measurementAverage;
}
String Measurements::pms5003FirmwareVersion(int fwCode) {
return pms5003FirmwareVersionBase("PMS5003x", fwCode);
}
@ -485,11 +531,12 @@ void Measurements::validateChannel(int ch) {
float Measurements::getCorrectedPM25(AirGradient &ag, Configuration &config, bool useAvg, int ch) {
float pm25;
float corrected;
float humidity;
float pm003Count;
int channel = ch - 1; // Array index
if (useAvg) {
// Directly call from the index
int channel = ch - 1; // Array index
pm25 = _pm_25[channel].update.avg;
humidity = _humidity[channel].update.avg;
pm003Count = _pm_03_pc[channel].update.avg;
@ -500,19 +547,27 @@ float Measurements::getCorrectedPM25(AirGradient &ag, Configuration &config, boo
}
Configuration::PMCorrection pmCorrection = config.getPMCorrection();
if (pmCorrection.algorithm == PMCorrectionAlgorithm::EPA_2021) {
// EPA correction directly applied
pm25 = ag.pms5003.compensate(pm25, humidity);
} else {
// SLR correction, this is assumes before calling this function, correction algorithm is not None
pm25 = ag.pms5003.slrCorrection(pm25, pm003Count, pmCorrection.scalingFactor, pmCorrection.intercept);
switch (pmCorrection.algorithm) {
case PMCorrectionAlgorithm::Unknown:
case PMCorrectionAlgorithm::None:
// If correction is Unknown, then default is None
corrected = pm25;
break;
case PMCorrectionAlgorithm::EPA_2021:
corrected = ag.pms5003.compensate(pm25, humidity);
break;
default: {
// All SLR correction using the same flow, hence default condition
corrected = ag.pms5003.slrCorrection(pm25, pm003Count, pmCorrection.scalingFactor,
pmCorrection.intercept);
if (pmCorrection.useEPA) {
// Add EPA compensation on top of SLR
pm25 = ag.pms5003.compensate(pm25, humidity);
corrected = ag.pms5003.compensate(corrected, humidity);
}
}
}
return pm25;
return corrected;
}
String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi, AirGradient &ag,
@ -841,10 +896,10 @@ JSONVar Measurements::buildPMS(AirGradient &ag, int ch, bool allCh, bool withTem
pms[json_prop_pm03Count] = ag.round2(avg);
pms["channels"]["1"][json_prop_pm03Count] = ag.round2(_pm_03_pc[0].update.avg);
pms["channels"]["2"][json_prop_pm03Count] = ag.round2(_pm_03_pc[1].update.avg);
} else if (utils::isValidPm(_pm_03_pc[0].update.avg)) {
} else if (utils::isValidPm03Count(_pm_03_pc[0].update.avg)) {
pms[json_prop_pm03Count] = ag.round2(_pm_03_pc[0].update.avg);
pms["channels"]["1"][json_prop_pm03Count] = ag.round2(_pm_03_pc[0].update.avg);
} else if (utils::isValidPm(_pm_03_pc[1].update.avg)) {
} else if (utils::isValidPm03Count(_pm_03_pc[1].update.avg)) {
pms[json_prop_pm03Count] = ag.round2(_pm_03_pc[1].update.avg);
pms["channels"]["2"][json_prop_pm03Count] = ag.round2(_pm_03_pc[1].update.avg);
}
@ -856,10 +911,10 @@ JSONVar Measurements::buildPMS(AirGradient &ag, int ch, bool allCh, bool withTem
pms[json_prop_pm05Count] = ag.round2(avg);
pms["channels"]["1"][json_prop_pm05Count] = ag.round2(_pm_05_pc[0].update.avg);
pms["channels"]["2"][json_prop_pm05Count] = ag.round2(_pm_05_pc[1].update.avg);
} else if (utils::isValidPm(_pm_05_pc[0].update.avg)) {
} else if (utils::isValidPm03Count(_pm_05_pc[0].update.avg)) {
pms[json_prop_pm05Count] = ag.round2(_pm_05_pc[0].update.avg);
pms["channels"]["1"][json_prop_pm05Count] = ag.round2(_pm_05_pc[0].update.avg);
} else if (utils::isValidPm(_pm_05_pc[1].update.avg)) {
} else if (utils::isValidPm03Count(_pm_05_pc[1].update.avg)) {
pms[json_prop_pm05Count] = ag.round2(_pm_05_pc[1].update.avg);
pms["channels"]["2"][json_prop_pm05Count] = ag.round2(_pm_05_pc[1].update.avg);
}
@ -870,10 +925,10 @@ JSONVar Measurements::buildPMS(AirGradient &ag, int ch, bool allCh, bool withTem
pms[json_prop_pm1Count] = ag.round2(avg);
pms["channels"]["1"][json_prop_pm1Count] = ag.round2(_pm_01_pc[0].update.avg);
pms["channels"]["2"][json_prop_pm1Count] = ag.round2(_pm_01_pc[1].update.avg);
} else if (utils::isValidPm(_pm_01_pc[0].update.avg)) {
} else if (utils::isValidPm03Count(_pm_01_pc[0].update.avg)) {
pms[json_prop_pm1Count] = ag.round2(_pm_01_pc[0].update.avg);
pms["channels"]["1"][json_prop_pm1Count] = ag.round2(_pm_01_pc[0].update.avg);
} else if (utils::isValidPm(_pm_01_pc[1].update.avg)) {
} else if (utils::isValidPm03Count(_pm_01_pc[1].update.avg)) {
pms[json_prop_pm1Count] = ag.round2(_pm_01_pc[1].update.avg);
pms["channels"]["2"][json_prop_pm1Count] = ag.round2(_pm_01_pc[1].update.avg);
}
@ -885,10 +940,10 @@ JSONVar Measurements::buildPMS(AirGradient &ag, int ch, bool allCh, bool withTem
pms[json_prop_pm25Count] = ag.round2(avg);
pms["channels"]["1"][json_prop_pm25Count] = ag.round2(_pm_25_pc[0].update.avg);
pms["channels"]["2"][json_prop_pm25Count] = ag.round2(_pm_25_pc[1].update.avg);
} else if (utils::isValidPm(_pm_25_pc[0].update.avg)) {
} else if (utils::isValidPm03Count(_pm_25_pc[0].update.avg)) {
pms[json_prop_pm25Count] = ag.round2(_pm_25_pc[0].update.avg);
pms["channels"]["1"][json_prop_pm25Count] = ag.round2(_pm_25_pc[0].update.avg);
} else if (utils::isValidPm(_pm_25_pc[1].update.avg)) {
} else if (utils::isValidPm03Count(_pm_25_pc[1].update.avg)) {
pms[json_prop_pm25Count] = ag.round2(_pm_25_pc[1].update.avg);
pms["channels"]["2"][json_prop_pm25Count] = ag.round2(_pm_25_pc[1].update.avg);
}

View File

@ -114,9 +114,19 @@ public:
*/
float getFloat(MeasurementType type, int ch = 1);
/**
* @brief Get the target measurement average value
*
* @param type measurement type that will be retrieve
* @param ch target type value channel
* @return moving average value of target measurements type
*/
float getAverage(MeasurementType type, int ch = 1);
/**
* @brief Get the Corrected PM25 object based on the correction algorithm from configuration
*
* If correction is not enabled, then will return the raw value (either average or last value)
*
* @param ag AirGradient instance
* @param config Configuration instance

View File

@ -15,7 +15,7 @@
#include "Main/utils.h"
#ifndef GIT_VERSION
#define GIT_VERSION "3.1.10-snap"
#define GIT_VERSION "3.1.12-snap"
#endif
/**