diff --git a/co2_sensor.cpp b/co2_sensor.cpp index f13957352a052aa992bfa4899940cc32981691ac..f800bd25cf3c9ec882e002e5e04c8d0e8e3ebf14 100644 --- a/co2_sensor.cpp +++ b/co2_sensor.cpp @@ -89,6 +89,7 @@ namespace sensor { stable_measurements = 0; waiting_color = color::red; } + previous_co2 = co2; } bool updateDataIfAvailable() { @@ -128,10 +129,10 @@ namespace sensor { } void getAndSendData() { - bool freshData = sensor::updateDataIfAvailable(); + bool freshData = updateDataIfAvailable(); //NOTE: Data is available, but it's sometimes erroneous: the sensor outputs zero ppm but non-zero temperature and non-zero humidity. - if (sensor::co2 <= 0) { + if (co2 <= 0) { // No measurement yet. Waiting. LedEffects::showWaitingLED(color::blue); return; @@ -142,20 +143,20 @@ namespace sensor { */ if (freshData) { countStableMeasurements(); - sensor::timestamp = ntp::getLocalTime(); - Serial.println(sensor::timestamp); + timestamp = ntp::getLocalTime(); + Serial.println(timestamp); Serial.print(F("co2(ppm): ")); - Serial.print(sensor::co2); + Serial.print(co2); Serial.print(F(" temp(C): ")); - Serial.print(sensor::temperature); + Serial.print(temperature); Serial.print(F(" humidity(%): ")); - Serial.println(sensor::humidity); + Serial.println(humidity); - csv_writer::logIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity); + csv_writer::logIfTimeHasCome(timestamp, co2, temperature, humidity); #ifdef MQTT - mqtt::publishIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity); + mqtt::publishIfTimeHasCome(timestamp, co2, temperature, humidity); #endif } @@ -167,7 +168,7 @@ namespace sensor { return; } - if (sensor::co2 < 250) { + if (co2 < 250) { // Sensor should be calibrated. LedEffects::showWaitingLED(color::magenta); return; @@ -178,9 +179,9 @@ namespace sensor { * Those effects include a short delay. */ - if (sensor::co2 < 2000) { - LedEffects::displayCO2color(sensor::co2); - LedEffects::breathe(sensor::co2); + if (co2 < 2000) { + LedEffects::displayCO2color(co2); + LedEffects::breathe(co2); } else { // >= 2000: entire ring blinks red LedEffects::redAlert(); }