Skip to content

Commit 9ee3534

Browse files
authored
Merge pull request #318 from airgradienthq/feat/improve-measure-logs
Improve measurements logging
2 parents 174ec65 + cec0514 commit 9ee3534

File tree

3 files changed

+167
-6
lines changed

3 files changed

+167
-6
lines changed

examples/OneOpenAir/OneOpenAir.ino

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ static void updatePm(void);
143143
static void sendDataToServer(void);
144144
static void tempHumUpdate(void);
145145
static void co2Update(void);
146+
static void printMeasurements();
146147
static void mdnsInit(void);
147148
static void createMqttTask(void);
148149
static void initMqtt(void);
@@ -172,6 +173,7 @@ AgSchedule tvocSchedule(SENSOR_TVOC_UPDATE_INTERVAL, updateTvoc);
172173
AgSchedule watchdogFeedSchedule(60000, wdgFeedUpdate);
173174
AgSchedule checkForUpdateSchedule(FIRMWARE_CHECK_FOR_UPDATE_MS, checkForFirmwareUpdate);
174175
AgSchedule networkSignalCheckSchedule(10000, networkSignalCheck);
176+
AgSchedule printMeasurementsSchedule(6000, printMeasurements);
175177

176178
void setup() {
177179
/** Serial for print debug message */
@@ -218,9 +220,6 @@ void setup() {
218220
boardInit();
219221
setMeasurementMaxPeriod();
220222

221-
// Comment below line to disable debug measurement readings
222-
measurements.setDebug(true);
223-
224223
bool connectToNetwork = true;
225224
if (ag->isOne()) { // Offline mode only available for indoor monitor
226225
/** Show message confirm offline mode, should me perform if LED bar button
@@ -364,6 +363,9 @@ void loop() {
364363
}
365364
}
366365

366+
/* Run measurement schedule */
367+
printMeasurementsSchedule.run();
368+
367369
/** factory reset handle */
368370
factoryConfigReset();
369371

@@ -385,6 +387,10 @@ static void co2Update(void) {
385387
}
386388
}
387389

390+
void printMeasurements() {
391+
measurements.printCurrentAverage();
392+
}
393+
388394
static void mdnsInit(void) {
389395
if (!MDNS.begin(localServer.getHostname().c_str())) {
390396
Serial.println("Init mDNS failed");

src/AgValue.cpp

Lines changed: 154 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <cmath>
66
#include <sstream>
77

8-
#define json_prop_pmFirmware "firmware"
8+
#define json_prop_pmFirmware "firmware"
99
#define json_prop_pm01Ae "pm01"
1010
#define json_prop_pm25Ae "pm02"
1111
#define json_prop_pm10Ae "pm10"
@@ -33,7 +33,7 @@ Measurements::Measurements(Configuration &config) : config(config) {
3333
#ifndef ESP8266
3434
_resetReason = (int)ESP_RST_UNKNOWN;
3535
#endif
36-
36+
3737
/* Set invalid value for each measurements as default value when initialized*/
3838
_temperature[0].update.avg = utils::getInvalidTemperature();
3939
_temperature[1].update.avg = utils::getInvalidTemperature();
@@ -51,7 +51,7 @@ Measurements::Measurements(Configuration &config) : config(config) {
5151
_pm_05_pc[1].update.avg = utils::getInvalidPmValue();
5252
_pm_5_pc[0].update.avg = utils::getInvalidPmValue();
5353
_pm_5_pc[1].update.avg = utils::getInvalidPmValue();
54-
54+
5555
_pm_01[0].update.avg = utils::getInvalidPmValue();
5656
_pm_01_sp[0].update.avg = utils::getInvalidPmValue();
5757
_pm_01_pc[0].update.avg = utils::getInvalidPmValue();
@@ -76,6 +76,86 @@ Measurements::Measurements(Configuration &config) : config(config) {
7676

7777
void Measurements::setAirGradient(AirGradient *ag) { this->ag = ag; }
7878

79+
void Measurements::printCurrentAverage() {
80+
Serial.println();
81+
if (config.hasSensorS8) {
82+
if (utils::isValidCO2(_co2.update.avg)) {
83+
Serial.printf("CO2 = %.2f ppm\n", _co2.update.avg);
84+
} else {
85+
Serial.printf("CO2 = -\n");
86+
}
87+
}
88+
89+
if (config.hasSensorSHT) {
90+
if (utils::isValidTemperature(_temperature[0].update.avg)) {
91+
Serial.printf("Temperature = %.2f C\n", _temperature[0].update.avg);
92+
} else {
93+
Serial.printf("Temperature = -\n");
94+
}
95+
if (utils::isValidHumidity(_humidity[0].update.avg)) {
96+
Serial.printf("Relative Humidity = %.2f\n", _humidity[0].update.avg);
97+
} else {
98+
Serial.printf("Relative Humidity = -\n");
99+
}
100+
}
101+
102+
if (config.hasSensorSGP) {
103+
if (utils::isValidVOC(_tvoc.update.avg)) {
104+
Serial.printf("TVOC Index = %.1f\n", _tvoc.update.avg);
105+
} else {
106+
Serial.printf("TVOC Index = -\n");
107+
}
108+
if (utils::isValidVOC(_tvoc_raw.update.avg)) {
109+
Serial.printf("TVOC Raw = %.1f\n", _tvoc_raw.update.avg);
110+
} else {
111+
Serial.printf("TVOC Raw = -\n");
112+
}
113+
if (utils::isValidNOx(_nox.update.avg)) {
114+
Serial.printf("NOx Index = %.1f\n", _nox.update.avg);
115+
} else {
116+
Serial.printf("NOx Index = -\n");
117+
}
118+
if (utils::isValidNOx(_nox_raw.update.avg)) {
119+
Serial.printf("NOx Raw = %.1f\n", _nox_raw.update.avg);
120+
} else {
121+
Serial.printf("NOx Raw = -\n");
122+
}
123+
}
124+
125+
if (config.hasSensorPMS1) {
126+
printCurrentPMAverage(1);
127+
if (!config.hasSensorSHT) {
128+
if (utils::isValidTemperature(_temperature[0].update.avg)) {
129+
Serial.printf("[1] Temperature = %.2f C\n", _temperature[0].update.avg);
130+
} else {
131+
Serial.printf("[1] Temperature = -\n");
132+
}
133+
if (utils::isValidHumidity(_humidity[0].update.avg)) {
134+
Serial.printf("[1] Relative Humidity = %.2f\n", _humidity[0].update.avg);
135+
} else {
136+
Serial.printf("[1] Relative Humidity = -\n");
137+
}
138+
}
139+
}
140+
if (config.hasSensorPMS2) {
141+
printCurrentPMAverage(2);
142+
if (!config.hasSensorSHT) {
143+
if (utils::isValidTemperature(_temperature[1].update.avg)) {
144+
Serial.printf("[2] Temperature = %.2f C\n", _temperature[1].update.avg);
145+
} else {
146+
Serial.printf("[2] Temperature = -\n");
147+
}
148+
if (utils::isValidHumidity(_humidity[1].update.avg)) {
149+
Serial.printf("[2] Relative Humidity = %.2f\n", _humidity[1].update.avg);
150+
} else {
151+
Serial.printf("[2] Relative Humidity = -\n");
152+
}
153+
}
154+
}
155+
156+
Serial.println();
157+
}
158+
79159
void Measurements::maxPeriod(MeasurementType type, int max) {
80160
switch (type) {
81161
case Temperature:
@@ -570,6 +650,77 @@ String Measurements::measurementTypeStr(MeasurementType type) {
570650
return str;
571651
}
572652

653+
void Measurements::printCurrentPMAverage(int ch) {
654+
int idx = ch - 1;
655+
656+
if (utils::isValidPm(_pm_01[idx].update.avg)) {
657+
Serial.printf("[%d] Atmospheric PM 1.0 = %.2f ug/m3\n", ch, _pm_01[idx].update.avg);
658+
} else {
659+
Serial.printf("[%d] Atmospheric PM 1.0 = -\n");
660+
}
661+
if (utils::isValidPm(_pm_25[idx].update.avg)) {
662+
Serial.printf("[%d] Atmospheric PM 2.5 = %.2f ug/m3\n", ch, _pm_25[idx].update.avg);
663+
} else {
664+
Serial.printf("[%d] Atmospheric PM 2.5 = -\n");
665+
}
666+
if (utils::isValidPm(_pm_10[idx].update.avg)) {
667+
Serial.printf("[%d] Atmospheric PM 10 = %.2f ug/m3\n", ch, _pm_10[idx].update.avg);
668+
} else {
669+
Serial.printf("[%d] Atmospheric PM 10 = -\n");
670+
}
671+
if (utils::isValidPm(_pm_01_sp[idx].update.avg)) {
672+
Serial.printf("[%d] Standard Particle PM 1.0 = %.2f ug/m3\n", ch, _pm_01_sp[idx].update.avg);
673+
} else {
674+
Serial.printf("[%d] Standard Particle PM 1.0 = -\n");
675+
}
676+
if (utils::isValidPm(_pm_25_sp[idx].update.avg)) {
677+
Serial.printf("[%d] Standard Particle PM 2.5 = %.2f ug/m3\n", ch, _pm_25_sp[idx].update.avg);
678+
} else {
679+
Serial.printf("[%d] Standard Particle PM 2.5 = -\n");
680+
}
681+
if (utils::isValidPm(_pm_10_sp[idx].update.avg)) {
682+
Serial.printf("[%d] Standard Particle PM 10 = %.2f ug/m3\n", ch, _pm_10_sp[idx].update.avg);
683+
} else {
684+
Serial.printf("[%d] Standard Particle PM 10 = -\n");
685+
}
686+
if (utils::isValidPm03Count(_pm_03_pc[idx].update.avg)) {
687+
Serial.printf("[%d] Particle Count 0.3 = %.1f\n", ch, _pm_03_pc[idx].update.avg);
688+
} else {
689+
Serial.printf("[%d] Particle Count 0.3 = -\n");
690+
}
691+
if (utils::isValidPm03Count(_pm_05_pc[idx].update.avg)) {
692+
Serial.printf("[%d] Particle Count 0.5 = %.1f\n", ch, _pm_05_pc[idx].update.avg);
693+
} else {
694+
Serial.printf("[%d] Particle Count 0.5 = -\n");
695+
}
696+
if (utils::isValidPm03Count(_pm_01_pc[idx].update.avg)) {
697+
Serial.printf("[%d] Particle Count 1.0 = %.1f\n", ch, _pm_01_pc[idx].update.avg);
698+
} else {
699+
Serial.printf("[%d] Particle Count 1.0 = -\n");
700+
}
701+
if (utils::isValidPm03Count(_pm_25_pc[idx].update.avg)) {
702+
Serial.printf("[%d] Particle Count 2.5 = %.1f\n", ch, _pm_25_pc[idx].update.avg);
703+
} else {
704+
Serial.printf("[%d] Particle Count 2.5 = -\n");
705+
}
706+
707+
if (_pm_5_pc[idx].listValues.empty() == false) {
708+
if (utils::isValidPm03Count(_pm_5_pc[idx].update.avg)) {
709+
Serial.printf("[%d] Particle Count 5.0 = %.1f\n", ch, _pm_5_pc[idx].update.avg);
710+
} else {
711+
Serial.printf("[%d] Particle Count 5.0 = -\n");
712+
}
713+
}
714+
715+
if (_pm_10_pc[idx].listValues.empty() == false) {
716+
if (utils::isValidPm03Count(_pm_10_pc[idx].update.avg)) {
717+
Serial.printf("[%d] Particle Count 10 = %.1f\n", ch, _pm_10_pc[idx].update.avg);
718+
} else {
719+
Serial.printf("[%d] Particle Count 10 = -\n");
720+
}
721+
}
722+
}
723+
573724
void Measurements::validateChannel(int ch) {
574725
if (ch != 1 && ch != 2) {
575726
Serial.printf("ERROR! Channel %d is undefined. Only channel 1 or 2 is the optional value!", ch);

src/AgValue.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class Measurements {
8888
PM10_PC, // Particle 10 count
8989
};
9090

91+
void printCurrentAverage();
92+
9193
/**
9294
* @brief Set each MeasurementType maximum period length for moving average
9395
*
@@ -258,6 +260,8 @@ class Measurements {
258260
*/
259261
void validateChannel(int ch);
260262

263+
void printCurrentPMAverage(int ch);
264+
261265
JSONVar buildOutdoor(bool localServer, AgFirmwareMode fwMode);
262266
JSONVar buildIndoor(bool localServer);
263267
JSONVar buildPMS(int ch, bool allCh, bool withTempHum, bool compensate);

0 commit comments

Comments
 (0)