From cec051444449a0e63f0ac7cc6398f41b2f40bb81 Mon Sep 17 00:00:00 2001 From: samuelbles07 Date: Sun, 11 May 2025 14:10:51 +0700 Subject: [PATCH] print measurements on schedule --- examples/OneOpenAir/OneOpenAir.ino | 12 ++- src/AgValue.cpp | 157 ++++++++++++++++++++++++++++- src/AgValue.h | 4 + 3 files changed, 167 insertions(+), 6 deletions(-) diff --git a/examples/OneOpenAir/OneOpenAir.ino b/examples/OneOpenAir/OneOpenAir.ino index d418a1d..9c48d01 100644 --- a/examples/OneOpenAir/OneOpenAir.ino +++ b/examples/OneOpenAir/OneOpenAir.ino @@ -143,6 +143,7 @@ static void updatePm(void); static void sendDataToServer(void); static void tempHumUpdate(void); static void co2Update(void); +static void printMeasurements(); static void mdnsInit(void); static void createMqttTask(void); static void initMqtt(void); @@ -172,6 +173,7 @@ AgSchedule tvocSchedule(SENSOR_TVOC_UPDATE_INTERVAL, updateTvoc); AgSchedule watchdogFeedSchedule(60000, wdgFeedUpdate); AgSchedule checkForUpdateSchedule(FIRMWARE_CHECK_FOR_UPDATE_MS, checkForFirmwareUpdate); AgSchedule networkSignalCheckSchedule(10000, networkSignalCheck); +AgSchedule printMeasurementsSchedule(6000, printMeasurements); void setup() { /** Serial for print debug message */ @@ -218,9 +220,6 @@ void setup() { boardInit(); setMeasurementMaxPeriod(); - // Comment below line to disable debug measurement readings - measurements.setDebug(true); - bool connectToNetwork = true; if (ag->isOne()) { // Offline mode only available for indoor monitor /** Show message confirm offline mode, should me perform if LED bar button @@ -364,6 +363,9 @@ void loop() { } } + /* Run measurement schedule */ + printMeasurementsSchedule.run(); + /** factory reset handle */ factoryConfigReset(); @@ -385,6 +387,10 @@ static void co2Update(void) { } } +void printMeasurements() { + measurements.printCurrentAverage(); +} + static void mdnsInit(void) { if (!MDNS.begin(localServer.getHostname().c_str())) { Serial.println("Init mDNS failed"); diff --git a/src/AgValue.cpp b/src/AgValue.cpp index 854cc03..ca841d0 100644 --- a/src/AgValue.cpp +++ b/src/AgValue.cpp @@ -5,7 +5,7 @@ #include #include -#define json_prop_pmFirmware "firmware" +#define json_prop_pmFirmware "firmware" #define json_prop_pm01Ae "pm01" #define json_prop_pm25Ae "pm02" #define json_prop_pm10Ae "pm10" @@ -33,7 +33,7 @@ Measurements::Measurements(Configuration &config) : config(config) { #ifndef ESP8266 _resetReason = (int)ESP_RST_UNKNOWN; #endif - + /* Set invalid value for each measurements as default value when initialized*/ _temperature[0].update.avg = utils::getInvalidTemperature(); _temperature[1].update.avg = utils::getInvalidTemperature(); @@ -51,7 +51,7 @@ Measurements::Measurements(Configuration &config) : config(config) { _pm_05_pc[1].update.avg = utils::getInvalidPmValue(); _pm_5_pc[0].update.avg = utils::getInvalidPmValue(); _pm_5_pc[1].update.avg = utils::getInvalidPmValue(); - + _pm_01[0].update.avg = utils::getInvalidPmValue(); _pm_01_sp[0].update.avg = utils::getInvalidPmValue(); _pm_01_pc[0].update.avg = utils::getInvalidPmValue(); @@ -76,6 +76,86 @@ Measurements::Measurements(Configuration &config) : config(config) { void Measurements::setAirGradient(AirGradient *ag) { this->ag = ag; } +void Measurements::printCurrentAverage() { + Serial.println(); + if (config.hasSensorS8) { + if (utils::isValidCO2(_co2.update.avg)) { + Serial.printf("CO2 = %.2f ppm\n", _co2.update.avg); + } else { + Serial.printf("CO2 = -\n"); + } + } + + if (config.hasSensorSHT) { + if (utils::isValidTemperature(_temperature[0].update.avg)) { + Serial.printf("Temperature = %.2f C\n", _temperature[0].update.avg); + } else { + Serial.printf("Temperature = -\n"); + } + if (utils::isValidHumidity(_humidity[0].update.avg)) { + Serial.printf("Relative Humidity = %.2f\n", _humidity[0].update.avg); + } else { + Serial.printf("Relative Humidity = -\n"); + } + } + + if (config.hasSensorSGP) { + if (utils::isValidVOC(_tvoc.update.avg)) { + Serial.printf("TVOC Index = %.1f\n", _tvoc.update.avg); + } else { + Serial.printf("TVOC Index = -\n"); + } + if (utils::isValidVOC(_tvoc_raw.update.avg)) { + Serial.printf("TVOC Raw = %.1f\n", _tvoc_raw.update.avg); + } else { + Serial.printf("TVOC Raw = -\n"); + } + if (utils::isValidNOx(_nox.update.avg)) { + Serial.printf("NOx Index = %.1f\n", _nox.update.avg); + } else { + Serial.printf("NOx Index = -\n"); + } + if (utils::isValidNOx(_nox_raw.update.avg)) { + Serial.printf("NOx Raw = %.1f\n", _nox_raw.update.avg); + } else { + Serial.printf("NOx Raw = -\n"); + } + } + + if (config.hasSensorPMS1) { + printCurrentPMAverage(1); + if (!config.hasSensorSHT) { + if (utils::isValidTemperature(_temperature[0].update.avg)) { + Serial.printf("[1] Temperature = %.2f C\n", _temperature[0].update.avg); + } else { + Serial.printf("[1] Temperature = -\n"); + } + if (utils::isValidHumidity(_humidity[0].update.avg)) { + Serial.printf("[1] Relative Humidity = %.2f\n", _humidity[0].update.avg); + } else { + Serial.printf("[1] Relative Humidity = -\n"); + } + } + } + if (config.hasSensorPMS2) { + printCurrentPMAverage(2); + if (!config.hasSensorSHT) { + if (utils::isValidTemperature(_temperature[1].update.avg)) { + Serial.printf("[2] Temperature = %.2f C\n", _temperature[1].update.avg); + } else { + Serial.printf("[2] Temperature = -\n"); + } + if (utils::isValidHumidity(_humidity[1].update.avg)) { + Serial.printf("[2] Relative Humidity = %.2f\n", _humidity[1].update.avg); + } else { + Serial.printf("[2] Relative Humidity = -\n"); + } + } + } + + Serial.println(); +} + void Measurements::maxPeriod(MeasurementType type, int max) { switch (type) { case Temperature: @@ -570,6 +650,77 @@ String Measurements::measurementTypeStr(MeasurementType type) { return str; } +void Measurements::printCurrentPMAverage(int ch) { + int idx = ch - 1; + + if (utils::isValidPm(_pm_01[idx].update.avg)) { + Serial.printf("[%d] Atmospheric PM 1.0 = %.2f ug/m3\n", ch, _pm_01[idx].update.avg); + } else { + Serial.printf("[%d] Atmospheric PM 1.0 = -\n"); + } + if (utils::isValidPm(_pm_25[idx].update.avg)) { + Serial.printf("[%d] Atmospheric PM 2.5 = %.2f ug/m3\n", ch, _pm_25[idx].update.avg); + } else { + Serial.printf("[%d] Atmospheric PM 2.5 = -\n"); + } + if (utils::isValidPm(_pm_10[idx].update.avg)) { + Serial.printf("[%d] Atmospheric PM 10 = %.2f ug/m3\n", ch, _pm_10[idx].update.avg); + } else { + Serial.printf("[%d] Atmospheric PM 10 = -\n"); + } + if (utils::isValidPm(_pm_01_sp[idx].update.avg)) { + Serial.printf("[%d] Standard Particle PM 1.0 = %.2f ug/m3\n", ch, _pm_01_sp[idx].update.avg); + } else { + Serial.printf("[%d] Standard Particle PM 1.0 = -\n"); + } + if (utils::isValidPm(_pm_25_sp[idx].update.avg)) { + Serial.printf("[%d] Standard Particle PM 2.5 = %.2f ug/m3\n", ch, _pm_25_sp[idx].update.avg); + } else { + Serial.printf("[%d] Standard Particle PM 2.5 = -\n"); + } + if (utils::isValidPm(_pm_10_sp[idx].update.avg)) { + Serial.printf("[%d] Standard Particle PM 10 = %.2f ug/m3\n", ch, _pm_10_sp[idx].update.avg); + } else { + Serial.printf("[%d] Standard Particle PM 10 = -\n"); + } + if (utils::isValidPm03Count(_pm_03_pc[idx].update.avg)) { + Serial.printf("[%d] Particle Count 0.3 = %.1f\n", ch, _pm_03_pc[idx].update.avg); + } else { + Serial.printf("[%d] Particle Count 0.3 = -\n"); + } + if (utils::isValidPm03Count(_pm_05_pc[idx].update.avg)) { + Serial.printf("[%d] Particle Count 0.5 = %.1f\n", ch, _pm_05_pc[idx].update.avg); + } else { + Serial.printf("[%d] Particle Count 0.5 = -\n"); + } + if (utils::isValidPm03Count(_pm_01_pc[idx].update.avg)) { + Serial.printf("[%d] Particle Count 1.0 = %.1f\n", ch, _pm_01_pc[idx].update.avg); + } else { + Serial.printf("[%d] Particle Count 1.0 = -\n"); + } + if (utils::isValidPm03Count(_pm_25_pc[idx].update.avg)) { + Serial.printf("[%d] Particle Count 2.5 = %.1f\n", ch, _pm_25_pc[idx].update.avg); + } else { + Serial.printf("[%d] Particle Count 2.5 = -\n"); + } + + if (_pm_5_pc[idx].listValues.empty() == false) { + if (utils::isValidPm03Count(_pm_5_pc[idx].update.avg)) { + Serial.printf("[%d] Particle Count 5.0 = %.1f\n", ch, _pm_5_pc[idx].update.avg); + } else { + Serial.printf("[%d] Particle Count 5.0 = -\n"); + } + } + + if (_pm_10_pc[idx].listValues.empty() == false) { + if (utils::isValidPm03Count(_pm_10_pc[idx].update.avg)) { + Serial.printf("[%d] Particle Count 10 = %.1f\n", ch, _pm_10_pc[idx].update.avg); + } else { + Serial.printf("[%d] Particle Count 10 = -\n"); + } + } +} + 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); diff --git a/src/AgValue.h b/src/AgValue.h index 1ea3309..29ddd07 100644 --- a/src/AgValue.h +++ b/src/AgValue.h @@ -88,6 +88,8 @@ public: PM10_PC, // Particle 10 count }; + void printCurrentAverage(); + /** * @brief Set each MeasurementType maximum period length for moving average * @@ -258,6 +260,8 @@ private: */ void validateChannel(int ch); + void printCurrentPMAverage(int ch); + JSONVar buildOutdoor(bool localServer, AgFirmwareMode fwMode); JSONVar buildIndoor(bool localServer); JSONVar buildPMS(int ch, bool allCh, bool withTempHum, bool compensate);