Files
airgradient/src/AgValue.cpp
T

733 lines
23 KiB
C++
Raw Normal View History

2024-04-07 16:39:01 +07:00
#include "AgValue.h"
#include "AgConfigure.h"
#include "AirGradient.h"
2024-08-15 08:04:30 +07:00
#define json_prop_pmFirmware "firmware"
2024-10-21 00:22:50 +07:00
void Measurements::maxPeriod(MeasurementType type, int max) {
switch (type) {
2024-10-19 01:32:41 +07:00
case Temperature:
2024-10-13 23:30:03 +07:00
_temperature[0].update.max = max;
_temperature[1].update.max = max;
break;
2024-10-19 01:32:41 +07:00
case Humidity:
2024-10-13 23:30:03 +07:00
_humidity[0].update.max = max;
_humidity[1].update.max = max;
break;
2024-10-19 01:32:41 +07:00
case CO2:
_co2.update.max = max;
break;
2024-10-19 01:32:41 +07:00
case TVOC:
_tvoc.update.max = max;
break;
2024-10-19 01:32:41 +07:00
case TVOCRaw:
_tvoc_raw.update.max = max;
break;
2024-10-19 01:32:41 +07:00
case NOx:
_nox.update.max = max;
break;
2024-10-19 01:32:41 +07:00
case NOxRaw:
_nox_raw.update.max = max;
break;
2024-10-19 01:32:41 +07:00
case PM25:
2024-10-13 23:30:03 +07:00
_pm_25[0].update.max = max;
_pm_25[1].update.max = max;
break;
2024-10-19 01:32:41 +07:00
case PM01:
2024-10-13 23:30:03 +07:00
_pm_01[0].update.max = max;
_pm_01[1].update.max = max;
break;
2024-10-19 01:32:41 +07:00
case PM10:
2024-10-13 23:30:03 +07:00
_pm_10[0].update.max = max;
_pm_10[1].update.max = max;
break;
2024-10-19 01:32:41 +07:00
case PM03_PC:
2024-10-13 23:30:03 +07:00
_pm_03_pc[0].update.max = max;
_pm_03_pc[1].update.max = max;
break;
};
}
2024-10-19 01:32:41 +07:00
bool Measurements::update(MeasurementType type, int val, int ch) {
2024-10-14 01:25:35 +07:00
// Sanity check to validate channel, assert if invalid
validateChannel(ch);
2024-10-13 23:30:03 +07:00
// Follow array indexing just for get address of the value type
ch = ch - 1;
// Define data point source
IntegerValue *temporary = nullptr;
float invalidValue = 0;
switch (type) {
2024-10-19 01:32:41 +07:00
case CO2:
temporary = &_co2;
invalidValue = utils::getInvalidCO2();
break;
2024-10-19 01:32:41 +07:00
case TVOC:
temporary = &_tvoc;
invalidValue = utils::getInvalidVOC();
break;
2024-10-19 01:32:41 +07:00
case TVOCRaw:
temporary = &_tvoc_raw;
invalidValue = utils::getInvalidVOC();
break;
2024-10-19 01:32:41 +07:00
case NOx:
temporary = &_nox;
invalidValue = utils::getInvalidNOx();
break;
2024-10-19 01:32:41 +07:00
case NOxRaw:
temporary = &_nox_raw;
invalidValue = utils::getInvalidNOx();
break;
2024-10-19 01:32:41 +07:00
case PM25:
2024-10-13 23:30:03 +07:00
temporary = &_pm_25[ch];
invalidValue = utils::getInvalidPmValue();
break;
2024-10-19 01:32:41 +07:00
case PM01:
2024-10-13 23:30:03 +07:00
temporary = &_pm_01[ch];
invalidValue = utils::getInvalidPmValue();
break;
2024-10-19 01:32:41 +07:00
case PM10:
2024-10-13 23:30:03 +07:00
temporary = &_pm_10[ch];
invalidValue = utils::getInvalidPmValue();
break;
2024-10-19 01:32:41 +07:00
case PM03_PC:
2024-10-13 23:30:03 +07:00
temporary = &_pm_03_pc[ch];
invalidValue = utils::getInvalidPmValue();
break;
default:
break;
};
2024-10-19 01:32:41 +07:00
// Sanity check if measurement type is defined for integer data type or not
if (temporary == nullptr) {
2024-10-19 01:32:41 +07:00
Serial.printf("%s is not defined for integer data type\n", measurementTypeStr(type));
// TODO: Just assert?
2024-10-13 23:30:03 +07:00
return false;
}
2024-10-13 23:30:03 +07:00
// Restore channel value for debugging purpose
ch = ch + 1;
2024-10-20 19:01:41 +07:00
if (val == invalidValue) {
temporary->update.invalidCounter++;
// TODO: Need to check if its more than threshold, to create some indication. Maybe reference to
// max element?
return false;
}
2024-10-20 19:01:41 +07:00
// Reset invalid counter when update new valid value
temporary->update.invalidCounter = 0;
2024-10-20 19:01:41 +07:00
// Add new value to the end of the list
temporary->listValues.push_back(val);
// Sum the new value
temporary->sumValues = temporary->sumValues + val;
// Remove the oldest value on the list when the list exceed max elements
if (temporary->listValues.size() > temporary->update.max) {
auto it = temporary->listValues.begin();
temporary->sumValues = temporary->sumValues - *it; // subtract the oldest value from sum
temporary->listValues.erase(it); // And remove it from the list
}
2024-10-20 19:01:41 +07:00
// Calculate average based on how many elements on the list
2024-10-20 19:20:17 +07:00
temporary->update.avg = temporary->sumValues / (float)temporary->listValues.size();
if (_debug) {
Serial.printf("%s{%d}: %.2f\n", measurementTypeStr(type), ch, temporary->update.avg);
}
2024-10-20 19:01:41 +07:00
return true;
}
2024-10-19 01:32:41 +07:00
bool Measurements::update(MeasurementType type, float val, int ch) {
2024-10-14 01:25:35 +07:00
// Sanity check to validate channel, assert if invalid
validateChannel(ch);
2024-10-13 23:30:03 +07:00
// Follow array indexing just for get address of the value type
ch = ch - 1;
// Define data point source
FloatValue *temporary = nullptr;
float invalidValue = 0;
switch (type) {
2024-10-19 01:32:41 +07:00
case Temperature:
2024-10-13 23:30:03 +07:00
temporary = &_temperature[ch];
invalidValue = utils::getInvalidTemperature();
break;
2024-10-19 01:32:41 +07:00
case Humidity:
2024-10-13 23:30:03 +07:00
temporary = &_humidity[ch];
invalidValue = utils::getInvalidHumidity();
break;
default:
break;
}
2024-10-19 01:32:41 +07:00
// Sanity check if measurement type is defined for float data type or not
if (temporary == nullptr) {
2024-10-19 01:32:41 +07:00
Serial.printf("%s is not defined for float data type\n", measurementTypeStr(type));
// TODO: Just assert?
2024-10-13 23:30:03 +07:00
return false;
}
2024-10-13 23:30:03 +07:00
// Restore channel value for debugging purpose
ch = ch + 1;
2024-10-20 19:01:41 +07:00
if (val == invalidValue) {
temporary->update.invalidCounter++;
// TODO: Need to check if its more than threshold, to create some indication. Maybe reference to
// max element?
return false;
}
2024-10-20 19:01:41 +07:00
// Reset invalid counter when update new valid value
temporary->update.invalidCounter = 0;
2024-10-20 19:01:41 +07:00
// Add new value to the end of the list
temporary->listValues.push_back(val);
// Sum the new value
temporary->sumValues = temporary->sumValues + val;
// Remove the oldest value on the list when the list exceed max elements
if (temporary->listValues.size() > temporary->update.max) {
auto it = temporary->listValues.begin();
temporary->sumValues = temporary->sumValues - *it; // subtract the oldest value from sum
temporary->listValues.erase(it); // And remove it from the list
}
2024-10-20 19:01:41 +07:00
// Calculate average based on how many elements on the list
2024-10-20 19:20:17 +07:00
temporary->update.avg = temporary->sumValues / (float)temporary->listValues.size();
if (_debug) {
Serial.printf("%s{%d}: %.2f\n", measurementTypeStr(type), ch, temporary->update.avg);
}
2024-10-20 19:01:41 +07:00
return true;
}
int Measurements::get(MeasurementType type, int ch) {
2024-10-14 01:25:35 +07:00
// Sanity check to validate channel, assert if invalid
validateChannel(ch);
2024-10-14 01:22:44 +07:00
// Follow array indexing just for get address of the value type
ch = ch - 1;
// Define data point source
IntegerValue *temporary = nullptr;
float invalidValue = 0;
switch (type) {
2024-10-19 01:32:41 +07:00
case CO2:
2024-10-14 01:22:44 +07:00
temporary = &_co2;
break;
2024-10-19 01:32:41 +07:00
case TVOC:
2024-10-14 01:22:44 +07:00
temporary = &_tvoc;
break;
2024-10-19 01:32:41 +07:00
case TVOCRaw:
2024-10-14 01:22:44 +07:00
temporary = &_tvoc_raw;
break;
2024-10-19 01:32:41 +07:00
case NOx:
2024-10-14 01:22:44 +07:00
temporary = &_nox;
break;
2024-10-19 01:32:41 +07:00
case NOxRaw:
2024-10-14 01:22:44 +07:00
temporary = &_nox_raw;
break;
2024-10-19 01:32:41 +07:00
case PM25:
2024-10-14 01:22:44 +07:00
temporary = &_pm_25[ch];
break;
2024-10-19 01:32:41 +07:00
case PM01:
2024-10-14 01:22:44 +07:00
temporary = &_pm_01[ch];
break;
2024-10-19 01:32:41 +07:00
case PM10:
2024-10-14 01:22:44 +07:00
temporary = &_pm_10[ch];
break;
2024-10-19 01:32:41 +07:00
case PM03_PC:
2024-10-14 01:22:44 +07:00
temporary = &_pm_03_pc[ch];
break;
default:
break;
};
2024-10-19 01:32:41 +07:00
// Sanity check if measurement type is defined for integer data type or not
2024-10-14 01:22:44 +07:00
if (temporary == nullptr) {
2024-10-19 01:32:41 +07:00
Serial.printf("%s is not defined for integer data type\n", measurementTypeStr(type));
2024-10-14 01:22:44 +07:00
// TODO: Just assert?
return false;
}
2024-10-20 19:01:41 +07:00
if (temporary->listValues.empty()) {
// Values still empty, return 0
return 0;
}
return temporary->listValues.back();
2024-10-14 01:22:44 +07:00
}
float Measurements::getFloat(MeasurementType type, int ch) {
2024-10-14 01:25:35 +07:00
// Sanity check to validate channel, assert if invalid
validateChannel(ch);
2024-10-19 01:32:41 +07:00
// Follow array indexing just for get address of the value type
ch = ch - 1;
2024-10-14 01:22:44 +07:00
// Define data point source
FloatValue *temporary = nullptr;
switch (type) {
2024-10-19 01:32:41 +07:00
case Temperature:
2024-10-14 01:22:44 +07:00
temporary = &_temperature[ch];
break;
2024-10-19 01:32:41 +07:00
case Humidity:
2024-10-14 01:22:44 +07:00
temporary = &_humidity[ch];
break;
default:
break;
}
2024-10-19 01:32:41 +07:00
// Sanity check if measurement type is defined for float data type or not
2024-10-14 01:22:44 +07:00
if (temporary == nullptr) {
2024-10-19 01:32:41 +07:00
Serial.printf("%s is not defined for float data type\n", measurementTypeStr(type));
2024-10-14 01:22:44 +07:00
// TODO: Just assert?
return false;
}
2024-10-20 19:01:41 +07:00
if (temporary->listValues.empty()) {
// Values still empty, return 0
return 0;
}
return temporary->listValues.back();
2024-10-14 01:22:44 +07:00
}
2024-10-11 22:11:26 +07:00
String Measurements::pms5003FirmwareVersion(int fwCode) {
return pms5003FirmwareVersionBase("PMS5003x", fwCode);
}
String Measurements::pms5003TFirmwareVersion(int fwCode) {
return pms5003FirmwareVersionBase("PMS5003x", fwCode);
}
String Measurements::pms5003FirmwareVersionBase(String prefix, int fwCode) {
return prefix + String("-") + String(fwCode);
}
2024-10-19 01:32:41 +07:00
String Measurements::measurementTypeStr(MeasurementType type) {
2024-10-11 22:11:26 +07:00
String str;
switch (type) {
2024-10-19 01:32:41 +07:00
case Temperature:
2024-10-11 22:11:26 +07:00
str = "Temperature";
break;
2024-10-19 01:32:41 +07:00
case Humidity:
2024-10-11 22:11:26 +07:00
str = "Humidity";
break;
2024-10-19 01:32:41 +07:00
case CO2:
2024-10-11 22:11:26 +07:00
str = "CO2";
break;
2024-10-19 01:32:41 +07:00
case TVOC:
2024-10-11 22:11:26 +07:00
str = "TVOC";
break;
2024-10-19 01:32:41 +07:00
case TVOCRaw:
2024-10-11 22:11:26 +07:00
str = "TVOCRaw";
break;
2024-10-19 01:32:41 +07:00
case NOx:
2024-10-11 22:11:26 +07:00
str = "NOx";
break;
2024-10-19 01:32:41 +07:00
case NOxRaw:
2024-10-11 22:11:26 +07:00
str = "NOxRaw";
break;
2024-10-19 01:32:41 +07:00
case PM25:
2024-10-11 22:11:26 +07:00
str = "PM25";
break;
2024-10-19 01:32:41 +07:00
case PM01:
2024-10-11 22:11:26 +07:00
str = "PM01";
break;
2024-10-19 01:32:41 +07:00
case PM10:
2024-10-11 22:11:26 +07:00
str = "PM10";
break;
2024-10-19 01:32:41 +07:00
case PM03_PC:
2024-10-11 22:11:26 +07:00
str = "PM03";
break;
default:
break;
};
return str;
2024-10-14 01:54:55 +07:00
}
void Measurements::validateChannel(int ch) {
if (ch != 1 && ch != 2) {
Serial.printf("ERROR! Channel %d is undefined. Only channel 1 or 2 is the optional value!", ch);
delay(1000);
assert(0);
}
2024-10-17 00:53:49 +07:00
}
2024-10-19 01:32:41 +07:00
String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi, AirGradient &ag,
Configuration &config) {
2024-10-17 00:53:49 +07:00
JSONVar root;
if (ag.isOne() || (ag.isPro4_2()) || ag.isPro3_3() || ag.isBasic()) {
root = buildIndoor(localServer, ag, config);
} else {
root = buildOutdoor(localServer, fwMode, ag, config);
}
// CO2
2024-10-20 19:20:17 +07:00
if (config.hasSensorS8 && utils::isValidCO2(_co2.update.avg)) {
root["rco2"] = ag.round2(_co2.update.avg);
2024-10-17 00:53:49 +07:00
}
/// TVOx and NOx
if (config.hasSensorSGP) {
2024-10-20 19:20:17 +07:00
if (utils::isValidVOC(_tvoc.update.avg)) {
root["tvocIndex"] = ag.round2(_tvoc.update.avg);
2024-10-17 00:53:49 +07:00
}
2024-10-20 19:20:17 +07:00
if (utils::isValidVOC(_tvoc_raw.update.avg)) {
root["tvocRaw"] = ag.round2(_tvoc_raw.update.avg);
2024-10-17 00:53:49 +07:00
}
2024-10-20 19:20:17 +07:00
if (utils::isValidNOx(_nox.update.avg)) {
root["noxIndex"] = ag.round2(_nox.update.avg);
2024-10-17 00:53:49 +07:00
}
2024-10-20 19:20:17 +07:00
if (utils::isValidNOx(_nox_raw.update.avg)) {
root["noxRaw"] = ag.round2(_nox_raw.update.avg);
2024-10-17 00:53:49 +07:00
}
}
root["boot"] = bootCount;
root["bootCount"] = bootCount;
root["wifi"] = rssi;
if (localServer) {
if (ag.isOne()) {
root["ledMode"] = config.getLedBarModeName();
}
root["serialno"] = ag.deviceId();
root["firmware"] = ag.getVersion();
root["model"] = AgFirmwareModeName(fwMode);
}
String result = JSON.stringify(root);
Serial.printf("\n---- PAYLOAD\n %s \n-----\n", result.c_str());
2024-10-17 00:53:49 +07:00
return result;
}
JSONVar Measurements::buildOutdoor(bool localServer, AgFirmwareMode fwMode, AirGradient &ag,
Configuration &config) {
JSONVar outdoor;
if (fwMode == FW_MODE_O_1P || fwMode == FW_MODE_O_1PS || fwMode == FW_MODE_O_1PST) {
// buildPMS params:
/// Because only have 1 PMS, allCh is set to false
/// But enable temp hum from PMS
/// compensated values if requested by local server
/// Set ch based on hasSensorPMSx
if (config.hasSensorPMS1) {
outdoor = buildPMS(ag, 1, false, true, localServer);
if (!localServer) {
outdoor[json_prop_pmFirmware] = pms5003TFirmwareVersion(ag.pms5003t_1.getFirmwareVersion());
}
} else {
outdoor = buildPMS(ag, 2, false, true, localServer);
if (!localServer) {
outdoor[json_prop_pmFirmware] = pms5003TFirmwareVersion(ag.pms5003t_2.getFirmwareVersion());
}
}
} else {
// FW_MODE_O_1PPT && FW_MODE_O_1PP: Outdoor monitor that have 2 PMS sensor
// buildPMS params:
/// Have 2 PMS sensor, allCh is set to true (ch params ignored)
/// Enable temp hum from PMS
/// compensated values if requested by local server
outdoor = buildPMS(ag, 1, true, true, localServer);
// PMS5003T version
if (!localServer) {
outdoor["channels"]["1"][json_prop_pmFirmware] =
pms5003TFirmwareVersion(ag.pms5003t_1.getFirmwareVersion());
outdoor["channels"]["2"][json_prop_pmFirmware] =
pms5003TFirmwareVersion(ag.pms5003t_2.getFirmwareVersion());
}
}
return outdoor;
}
JSONVar Measurements::buildIndoor(bool localServer, AirGradient &ag, Configuration &config) {
JSONVar indoor;
if (config.hasSensorPMS1) {
// buildPMS params:
/// PMS channel 1 (indoor only have 1 PMS; hence allCh false)
/// Not include temperature and humidity from PMS sensor
/// Not include compensated calculation
indoor = buildPMS(ag, 1, false, false, false);
if (!localServer) {
// Indoor is using PMS5003
indoor[json_prop_pmFirmware] = this->pms5003FirmwareVersion(ag.pms5003.getFirmwareVersion());
}
}
if (config.hasSensorSHT) {
// Add temperature
2024-10-20 19:20:17 +07:00
if (utils::isValidTemperature(_temperature[0].update.avg)) {
indoor["atmp"] = ag.round2(_temperature[0].update.avg);
2024-10-17 00:53:49 +07:00
if (localServer) {
2024-10-20 19:20:17 +07:00
indoor["atmpCompensated"] = ag.round2(_temperature[0].update.avg);
2024-10-17 00:53:49 +07:00
}
}
// Add humidity
2024-10-20 19:20:17 +07:00
if (utils::isValidHumidity(_humidity[0].update.avg)) {
indoor["rhum"] = ag.round2(_humidity[0].update.avg);
2024-10-17 00:53:49 +07:00
if (localServer) {
2024-10-20 19:20:17 +07:00
indoor["rhumCompensated"] = ag.round2(_humidity[0].update.avg);
2024-10-17 00:53:49 +07:00
}
}
}
// Add pm25 compensated value only if PM2.5 and humidity value is valid
2024-10-20 19:20:17 +07:00
if (config.hasSensorPMS1 && utils::isValidPm(_pm_25[0].update.avg)) {
if (config.hasSensorSHT && utils::isValidHumidity(_humidity[0].update.avg)) {
2024-10-21 01:49:01 +07:00
float pm25 = ag.pms5003.compensate(_pm_25[0].update.avg, _humidity[0].update.avg);
2024-10-17 00:53:49 +07:00
if (utils::isValidPm(pm25)) {
2024-10-21 01:49:01 +07:00
indoor["pm02Compensated"] = ag.round2(pm25);
2024-10-17 00:53:49 +07:00
}
}
}
return indoor;
}
JSONVar Measurements::buildPMS(AirGradient &ag, int ch, bool allCh, bool withTempHum,
bool compensate) {
JSONVar pms;
// When only one of the channel
if (allCh == false) {
// Sanity check to validate channel, assert if invalid
validateChannel(ch);
// Follow array indexing just for get address of the value type
ch = ch - 1;
2024-10-20 19:20:17 +07:00
if (utils::isValidPm(_pm_01[ch].update.avg)) {
pms["pm01"] = ag.round2(_pm_01[ch].update.avg);
2024-10-17 00:53:49 +07:00
}
2024-10-20 19:20:17 +07:00
if (utils::isValidPm(_pm_25[ch].update.avg)) {
pms["pm02"] = ag.round2(_pm_25[ch].update.avg);
2024-10-17 00:53:49 +07:00
}
2024-10-20 19:20:17 +07:00
if (utils::isValidPm(_pm_10[ch].update.avg)) {
pms["pm10"] = ag.round2(_pm_10[ch].update.avg);
2024-10-17 00:53:49 +07:00
}
2024-10-20 19:20:17 +07:00
if (utils::isValidPm03Count(_pm_03_pc[ch].update.avg)) {
pms["pm003Count"] = ag.round2(_pm_03_pc[ch].update.avg);
2024-10-17 00:53:49 +07:00
}
if (withTempHum) {
float _vc;
// Set temperature if valid
2024-10-20 19:20:17 +07:00
if (utils::isValidTemperature(_temperature[ch].update.avg)) {
pms["atmp"] = ag.round2(_temperature[ch].update.avg);
2024-10-17 00:53:49 +07:00
// Compensate temperature when flag is set
if (compensate) {
2024-10-20 19:20:17 +07:00
_vc = ag.pms5003t_1.compensateTemp(_temperature[ch].update.avg);
2024-10-17 00:53:49 +07:00
if (utils::isValidTemperature(_vc)) {
pms["atmpCompensated"] = ag.round2(_vc);
}
}
}
// Set humidity if valid
2024-10-20 19:20:17 +07:00
if (utils::isValidHumidity(_humidity[ch].update.avg)) {
pms["rhum"] = ag.round2(_humidity[ch].update.avg);
2024-10-17 00:53:49 +07:00
// Compensate relative humidity when flag is set
if (compensate) {
2024-10-20 19:20:17 +07:00
_vc = ag.pms5003t_1.compensateHum(_humidity[ch].update.avg);
2024-10-17 00:53:49 +07:00
if (utils::isValidTemperature(_vc)) {
pms["rhumCompensated"] = ag.round2(_vc);
}
}
}
// Add pm25 compensated value only if PM2.5 and humidity value is valid
if (compensate) {
2024-10-20 19:20:17 +07:00
if (utils::isValidPm(_pm_25[ch].update.avg) &&
utils::isValidHumidity(_humidity[ch].update.avg)) {
2024-10-17 00:53:49 +07:00
// Note: the pms5003t object is not matter either for channel 1 or 2, compensate points to
// the same base function
2024-10-21 01:49:01 +07:00
float pm25 = ag.pms5003t_1.compensate(_pm_25[ch].update.avg, _humidity[ch].update.avg);
2024-10-17 00:53:49 +07:00
if (utils::isValidPm(pm25)) {
2024-10-21 01:49:01 +07:00
pms["pm02Compensated"] = ag.round2(pm25);
2024-10-17 00:53:49 +07:00
}
}
}
}
// Directly return the json object
return pms;
};
// Handle both channel with average, if one of the channel not valid, use another one
/// PM01
2024-10-20 19:20:17 +07:00
if (utils::isValidPm(_pm_01[0].update.avg) && utils::isValidPm(_pm_01[1].update.avg)) {
float avg = (_pm_01[0].update.avg + _pm_01[1].update.avg) / 2.0f;
2024-10-17 00:53:49 +07:00
pms["pm01"] = ag.round2(avg);
2024-10-20 19:20:17 +07:00
pms["channels"]["1"]["pm01"] = ag.round2(_pm_01[0].update.avg);
pms["channels"]["2"]["pm01"] = ag.round2(_pm_01[1].update.avg);
} else if (utils::isValidPm(_pm_01[0].update.avg)) {
pms["pm01"] = ag.round2(_pm_01[0].update.avg);
pms["channels"]["1"]["pm01"] = ag.round2(_pm_01[0].update.avg);
} else if (utils::isValidPm(_pm_01[1].update.avg)) {
pms["pm01"] = ag.round2(_pm_01[1].update.avg);
pms["channels"]["2"]["pm01"] = ag.round2(_pm_01[1].update.avg);
2024-10-17 00:53:49 +07:00
}
/// PM2.5
2024-10-20 19:20:17 +07:00
if (utils::isValidPm(_pm_25[0].update.avg) && utils::isValidPm(_pm_25[1].update.avg)) {
float avg = (_pm_25[0].update.avg + _pm_25[1].update.avg) / 2.0f;
2024-10-17 00:53:49 +07:00
pms["pm02"] = ag.round2(avg);
2024-10-20 19:20:17 +07:00
pms["channels"]["1"]["pm02"] = ag.round2(_pm_25[0].update.avg);
pms["channels"]["2"]["pm02"] = ag.round2(_pm_25[1].update.avg);
} else if (utils::isValidPm(_pm_25[0].update.avg)) {
pms["pm02"] = ag.round2(_pm_25[0].update.avg);
pms["channels"]["1"]["pm02"] = ag.round2(_pm_25[0].update.avg);
} else if (utils::isValidPm(_pm_25[1].update.avg)) {
pms["pm02"] = ag.round2(_pm_25[1].update.avg);
pms["channels"]["2"]["pm02"] = ag.round2(_pm_25[1].update.avg);
2024-10-17 00:53:49 +07:00
}
/// PM10
2024-10-20 19:20:17 +07:00
if (utils::isValidPm(_pm_10[0].update.avg) && utils::isValidPm(_pm_10[1].update.avg)) {
float avg = (_pm_10[0].update.avg + _pm_10[1].update.avg) / 2.0f;
2024-10-17 00:53:49 +07:00
pms["pm10"] = ag.round2(avg);
2024-10-20 19:20:17 +07:00
pms["channels"]["1"]["pm10"] = ag.round2(_pm_10[0].update.avg);
pms["channels"]["2"]["pm10"] = ag.round2(_pm_10[1].update.avg);
} else if (utils::isValidPm(_pm_10[0].update.avg)) {
pms["pm10"] = ag.round2(_pm_10[0].update.avg);
pms["channels"]["1"]["pm10"] = ag.round2(_pm_10[0].update.avg);
} else if (utils::isValidPm(_pm_10[1].update.avg)) {
pms["pm10"] = ag.round2(_pm_10[1].update.avg);
pms["channels"]["2"]["pm10"] = ag.round2(_pm_10[1].update.avg);
2024-10-17 00:53:49 +07:00
}
/// PM03 particle count
2024-10-20 19:20:17 +07:00
if (utils::isValidPm03Count(_pm_03_pc[0].update.avg) &&
utils::isValidPm03Count(_pm_03_pc[1].update.avg)) {
float avg = (_pm_03_pc[0].update.avg + _pm_03_pc[1].update.avg) / 2.0f;
2024-10-17 00:53:49 +07:00
pms["pm003Count"] = ag.round2(avg);
2024-10-20 19:20:17 +07:00
pms["channels"]["1"]["pm003Count"] = ag.round2(_pm_03_pc[0].update.avg);
pms["channels"]["2"]["pm003Count"] = ag.round2(_pm_03_pc[1].update.avg);
} else if (utils::isValidPm(_pm_03_pc[0].update.avg)) {
pms["pm003Count"] = ag.round2(_pm_03_pc[0].update.avg);
pms["channels"]["1"]["pm003Count"] = ag.round2(_pm_03_pc[0].update.avg);
} else if (utils::isValidPm(_pm_03_pc[1].update.avg)) {
pms["pm003Count"] = ag.round2(_pm_03_pc[1].update.avg);
pms["channels"]["2"]["pm003Count"] = ag.round2(_pm_03_pc[1].update.avg);
2024-10-17 00:53:49 +07:00
}
if (withTempHum) {
/// Temperature
2024-10-20 19:20:17 +07:00
if (utils::isValidTemperature(_temperature[0].update.avg) &&
utils::isValidTemperature(_temperature[1].update.avg)) {
2024-10-17 00:53:49 +07:00
2024-10-20 19:20:17 +07:00
float temperature = (_temperature[0].update.avg + _temperature[1].update.avg) / 2.0f;
2024-10-17 00:53:49 +07:00
pms["atmp"] = ag.round2(temperature);
2024-10-20 19:20:17 +07:00
pms["channels"]["1"]["atmp"] = ag.round2(_temperature[0].update.avg);
pms["channels"]["2"]["atmp"] = ag.round2(_temperature[1].update.avg);
2024-10-17 00:53:49 +07:00
if (compensate) {
// Compensate both temperature channel
float temp = ag.pms5003t_1.compensateTemp(temperature);
2024-10-20 19:20:17 +07:00
float temp1 = ag.pms5003t_1.compensateTemp(_temperature[0].update.avg);
float temp2 = ag.pms5003t_2.compensateTemp(_temperature[1].update.avg);
2024-10-17 00:53:49 +07:00
pms["atmpCompensated"] = ag.round2(temp);
pms["channels"]["1"]["atmpCompensated"] = ag.round2(temp1);
pms["channels"]["2"]["atmpCompensated"] = ag.round2(temp2);
}
2024-10-20 19:20:17 +07:00
} else if (utils::isValidTemperature(_temperature[0].update.avg)) {
pms["atmp"] = ag.round2(_temperature[0].update.avg);
pms["channels"]["1"]["atmp"] = ag.round2(_temperature[0].update.avg);
2024-10-17 00:53:49 +07:00
if (compensate) {
// Compensate channel 1
2024-10-20 19:20:17 +07:00
float temp1 = ag.pms5003t_1.compensateTemp(_temperature[0].update.avg);
2024-10-17 00:53:49 +07:00
pms["atmpCompensated"] = ag.round2(temp1);
pms["channels"]["1"]["atmpCompensated"] = ag.round2(temp1);
}
2024-10-20 19:20:17 +07:00
} else if (utils::isValidTemperature(_temperature[1].update.avg)) {
pms["atmp"] = ag.round2(_temperature[1].update.avg);
pms["channels"]["2"]["atmp"] = ag.round2(_temperature[1].update.avg);
2024-10-17 00:53:49 +07:00
if (compensate) {
// Compensate channel 2
2024-10-20 19:20:17 +07:00
float temp2 = ag.pms5003t_2.compensateTemp(_temperature[1].update.avg);
2024-10-17 00:53:49 +07:00
pms["atmpCompensated"] = ag.round2(temp2);
pms["channels"]["2"]["atmpCompensated"] = ag.round2(temp2);
}
}
/// Relative humidity
2024-10-20 19:20:17 +07:00
if (utils::isValidHumidity(_humidity[0].update.avg) &&
utils::isValidHumidity(_humidity[1].update.avg)) {
float humidity = (_humidity[0].update.avg + _humidity[1].update.avg) / 2.0f;
2024-10-17 00:53:49 +07:00
pms["rhum"] = ag.round2(humidity);
2024-10-20 19:20:17 +07:00
pms["channels"]["1"]["rhum"] = ag.round2(_humidity[0].update.avg);
pms["channels"]["2"]["rhum"] = ag.round2(_humidity[1].update.avg);
2024-10-17 00:53:49 +07:00
if (compensate) {
// Compensate both humidity channel
float hum = ag.pms5003t_1.compensateHum(humidity);
2024-10-20 19:20:17 +07:00
float hum1 = ag.pms5003t_1.compensateHum(_humidity[0].update.avg);
float hum2 = ag.pms5003t_2.compensateHum(_humidity[1].update.avg);
2024-10-17 00:53:49 +07:00
pms["rhumCompensated"] = ag.round2(hum);
pms["channels"]["1"]["rhumCompensated"] = ag.round2(hum1);
pms["channels"]["2"]["rhumCompensated"] = ag.round2(hum2);
}
2024-10-20 19:20:17 +07:00
} else if (utils::isValidHumidity(_humidity[0].update.avg)) {
pms["rhum"] = ag.round2(_humidity[0].update.avg);
pms["channels"]["1"]["rhum"] = ag.round2(_humidity[0].update.avg);
2024-10-17 00:53:49 +07:00
if (compensate) {
// Compensate humidity channel 1
2024-10-20 19:20:17 +07:00
float hum1 = ag.pms5003t_1.compensateHum(_humidity[0].update.avg);
2024-10-17 00:53:49 +07:00
pms["rhumCompensated"] = ag.round2(hum1);
pms["channels"]["1"]["rhumCompensated"] = ag.round2(hum1);
}
2024-10-20 19:20:17 +07:00
} else if (utils::isValidHumidity(_humidity[1].update.avg)) {
pms["rhum"] = ag.round2(_humidity[1].update.avg);
pms["channels"]["2"]["rhum"] = ag.round2(_humidity[1].update.avg);
2024-10-17 00:53:49 +07:00
if (compensate) {
// Compensate humidity channel 2
2024-10-20 19:20:17 +07:00
float hum2 = ag.pms5003t_2.compensateHum(_humidity[1].update.avg);
2024-10-17 00:53:49 +07:00
pms["rhumCompensated"] = ag.round2(hum2);
pms["channels"]["2"]["rhumCompensated"] = ag.round2(hum2);
}
}
if (compensate) {
// Add pm25 compensated value
/// First get both channel compensated value
2024-10-21 01:49:01 +07:00
float pm25_comp1 = utils::getInvalidPmValue();
float pm25_comp2 = utils::getInvalidPmValue();
2024-10-20 19:20:17 +07:00
if (utils::isValidPm(_pm_25[0].update.avg) &&
utils::isValidHumidity(_humidity[0].update.avg)) {
pm25_comp1 = ag.pms5003t_1.compensate(_pm_25[0].update.avg, _humidity[0].update.avg);
2024-10-21 01:49:01 +07:00
pms["channels"]["1"]["pm02Compensated"] = ag.round2(pm25_comp1);
2024-10-17 00:53:49 +07:00
}
2024-10-20 19:20:17 +07:00
if (utils::isValidPm(_pm_25[1].update.avg) &&
utils::isValidHumidity(_humidity[1].update.avg)) {
pm25_comp2 = ag.pms5003t_2.compensate(_pm_25[1].update.avg, _humidity[1].update.avg);
2024-10-21 01:49:01 +07:00
pms["channels"]["2"]["pm02Compensated"] = ag.round2(pm25_comp2);
2024-10-17 00:53:49 +07:00
}
/// Get average or one of the channel compensated value if only one channel is valid
if (utils::isValidPm(pm25_comp1) && utils::isValidPm(pm25_comp2)) {
2024-10-21 22:00:47 +07:00
pms["pm02Compensated"] = ag.round2((pm25_comp1 + pm25_comp2) / 2.0f);
2024-10-17 00:53:49 +07:00
} else if (utils::isValidPm(pm25_comp1)) {
2024-10-21 01:49:01 +07:00
pms["pm02Compensated"] = ag.round2(pm25_comp1);
2024-10-17 00:53:49 +07:00
} else if (utils::isValidPm(pm25_comp2)) {
2024-10-21 01:49:01 +07:00
pms["pm02Compensated"] = ag.round2(pm25_comp2);
2024-10-17 00:53:49 +07:00
}
}
}
return pms;
}
void Measurements::setDebug(bool debug) { _debug = debug; }