From 4ff6b183e24d543f4fe5e1f6b264766c8c07a3b8 Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Sat, 17 Apr 2021 11:28:29 +0200
Subject: [PATCH] Removing lots of stuff

---
 ampel-firmware/ampel-firmware.ino | 130 +-----------------------------
 ampel-firmware/co2_sensor.cpp     |  37 +--------
 ampel-firmware/config.public.h    |   8 +-
 ampel-firmware/led_effects.cpp    |   2 +-
 4 files changed, 10 insertions(+), 167 deletions(-)

diff --git a/ampel-firmware/ampel-firmware.ino b/ampel-firmware/ampel-firmware.ino
index 00a8986..aa6e876 100644
--- a/ampel-firmware/ampel-firmware.ino
+++ b/ampel-firmware/ampel-firmware.ino
@@ -55,7 +55,6 @@
  * and define your credentials and parameters in 'config.h'.
  */
 
-
 /*****************************************************************
  * PreInit                                                       *
  *****************************************************************/
@@ -67,60 +66,12 @@ void preinit() {
 #endif
 }
 
-
 /*****************************************************************
  * Setup                                                         *
  *****************************************************************/
 void setup() {
-  led_effects::setupOnBoardLED();
-  led_effects::onBoardLEDOff();
-
   Serial.begin(BAUDS);
-
-  pinMode(0, INPUT); // Flash button (used for forced calibration)
-
-  led_effects::setupRing();
-
   sensor::initialize();
-
-  Serial.print(F("Sensor ID: "));
-  Serial.println(SENSOR_ID);
-  Serial.print(F("Board    : "));
-  Serial.println(BOARD);
-
-#ifdef AMPEL_WIFI
-  WiFiConnect(SENSOR_ID);
-
-  Serial.print(F("WiFi - Status: "));
-  Serial.println(WiFi.status());
-
-  if (WiFi.status() == WL_CONNECTED) {
-#  ifdef AMPEL_HTTP
-    web_server::initialize();
-#  endif
-
-    ntp::initialize();
-
-    if (MDNS.begin(SENSOR_ID.c_str())) { // Start the mDNS responder for SENSOR_ID.local
-      MDNS.addService("http", "tcp", 80);
-      Serial.println(F("mDNS responder started"));
-    } else {
-      Serial.println(F("Error setting up MDNS responder!"));
-    }
-
-#  ifdef AMPEL_MQTT
-    mqtt::initialize("CO2sensors/" + SENSOR_ID);
-#  endif
-  }
-#endif
-
-#ifdef AMPEL_CSV
-  csv_writer::initialize();
-#endif
-
-#if defined(AMPEL_LORAWAN) && defined(ESP32)
-  lorawan::initialize();
-#endif
 }
 
 /*****************************************************************
@@ -128,85 +79,8 @@ void setup() {
  *****************************************************************/
 
 void loop() {
-#if defined(AMPEL_LORAWAN) && defined(ESP32)
-  //LMIC Library seems to be very sensitive to timing issues, so run it first.
-  lorawan::process();
-
-  if (lorawan::waiting_for_confirmation) {
-    // If node is waiting for join confirmation from Gateway, nothing else should run.
-    return;
-  }
-#endif
   //NOTE: Loop should never take more than 1000ms. Split in smaller methods and logic if needed.
   //NOTE: Only use millis() for duration comparison, not timestamps comparison. Otherwise, problems happen when millis roll over.
-  uint32_t t0 = millis();
-
-  keepServicesAlive();
-
-  // Short press for night mode, Long press for calibration.
-  checkFlashButton();
-
-  if (sensor::processData()) {
-#ifdef AMPEL_CSV
-    csv_writer::logIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
-#endif
-
-#if defined(AMPEL_WIFI) && defined(AMPEL_MQTT)
-    mqtt::publishIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
-#endif
-
-#if defined(AMPEL_LORAWAN) && defined(ESP32)
-    lorawan::preparePayloadIfTimeHasCome(sensor::co2, sensor::temperature, sensor::humidity);
-#endif
-  }
-
-  uint32_t duration = millis() - t0;
-  if (duration > max_loop_duration) {
-    max_loop_duration = duration;
-    Serial.print(F("Debug - Max loop duration : "));
-    Serial.print(max_loop_duration);
-    Serial.println(" ms.");
-  }
-}
-
-/**
- * Checks if flash button has been pressed:
- *   If not, do nothing.
- *   If short press, toggle LED display.
- *   If long press, start calibration process.
- */
-void checkFlashButton() {
-  if (!digitalRead(0)) { // Button has been pressed
-    led_effects::onBoardLEDOn();
-    delay(300);
-    if (digitalRead(0)) {
-      Serial.println(F("Flash has been pressed for a short time. Should toggle night mode."));
-      led_effects::toggleNightMode();
-    } else {
-      Serial.println(F("Flash has been pressed for a long time. Keep it pressed for calibration."));
-      if (led_effects::countdownToZero() < 0) {
-        sensor::startCalibrationProcess();
-      }
-    }
-    led_effects::onBoardLEDOff();
-  }
-}
-
-void keepServicesAlive() {
-#ifdef AMPEL_WIFI
-  if (WiFi.status() == WL_CONNECTED) {
-#  if defined(ESP8266)
-    //NOTE: Sadly, there seems to be a bug in the current MDNS implementation.
-    // It stops working after 2 minutes. And forcing a restart leads to a memory leak.
-    MDNS.update();
-#  endif
-    ntp::update(); // NTP client has its own timer. It will connect to NTP server every 60s.
-#  ifdef AMPEL_HTTP
-    web_server::update();
-#  endif
-#  ifdef AMPEL_MQTT
-    mqtt::keepConnection(); // MQTT client has its own timer. It will keep alive every 15s.
-#  endif
-  }
-#endif
+//  sensor::processData();
+  int t = 3;
 }
diff --git a/ampel-firmware/co2_sensor.cpp b/ampel-firmware/co2_sensor.cpp
index 0420890..fb0e0df 100644
--- a/ampel-firmware/co2_sensor.cpp
+++ b/ampel-firmware/co2_sensor.cpp
@@ -138,7 +138,7 @@ namespace sensor {
      */
     if (co2 < 2000) {
       led_effects::displayCO2color(co2);
-      delay(100);
+      // delay(100);
     } else {
       // >= 2000: entire ring blinks red
       led_effects::redAlert();
@@ -149,42 +149,11 @@ namespace sensor {
    * Returns true if fresh data is available, for further processing (e.g. MQTT, CSV or LoRa)
    */
   bool processData() {
-    bool freshData = scd30.dataAvailable();
-
-    if (freshData) {
-      // checkTimerDeviation();
-      timestamp = ntp::getLocalTime();
+    if (scd30.dataAvailable()) {
       co2 = scd30.getCO2();
       temperature = scd30.getTemperature();
       humidity = scd30.getHumidity();
     }
-
-    //NOTE: Data is available, but it's sometimes erroneous: the sensor outputs zero ppm but non-zero temperature and non-zero humidity.
-    if (co2 <= 0) {
-      // No measurement yet. Waiting.
-      led_effects::showWaitingLED(color::blue);
-      return false;
-    }
-
-    /**
-     * Fresh data. Log it and send it if needed.
-     */
-    if (freshData) {
-      if (should_calibrate) {
-        countStableMeasurements();
-      }
-      logToSerial();
-    }
-
-    if (should_calibrate) {
-      if (stable_measurements == 60) {
-        calibrateAndRestart();
-      }
-      led_effects::showWaitingLED(waiting_color);
-      return false;
-    }
-
-    displayCO2OnLedRing();
-    return freshData;
+    return 0;
   }
 }
diff --git a/ampel-firmware/config.public.h b/ampel-firmware/config.public.h
index feb7f4e..3c039d9 100644
--- a/ampel-firmware/config.public.h
+++ b/ampel-firmware/config.public.h
@@ -8,10 +8,10 @@
  */
 
 // Comment or remove those lines if you want to disable the corresponding services
-#  define AMPEL_WIFI    // Should ESP connect to WiFi? It allows the Ampel to get time from an NTP server.
-#  define AMPEL_HTTP    // Should HTTP web server be started? (AMPEL_WIFI should be enabled too)
-#  define AMPEL_MQTT    // Should data be sent over MQTT? (AMPEL_WIFI should be enabled too)
-#  define AMPEL_CSV     // Should data be logged as CSV, on the ESP flash memory?
+//#  define AMPEL_WIFI    // Should ESP connect to WiFi? It allows the Ampel to get time from an NTP server.
+//#  define AMPEL_HTTP    // Should HTTP web server be started? (AMPEL_WIFI should be enabled too)
+//#  define AMPEL_MQTT    // Should data be sent over MQTT? (AMPEL_WIFI should be enabled too)
+//#  define AMPEL_CSV     // Should data be logged as CSV, on the ESP flash memory?
 // #  define AMPEL_LORAWAN // Should data be sent over LoRaWAN? (Requires ESP32 + LoRa modem, and "MCCI LoRaWAN LMIC library")
 
 /**
diff --git a/ampel-firmware/led_effects.cpp b/ampel-firmware/led_effects.cpp
index 51ba98b..e146f72 100644
--- a/ampel-firmware/led_effects.cpp
+++ b/ampel-firmware/led_effects.cpp
@@ -13,7 +13,7 @@ namespace config {
   const int kitt_tail = 3; // How many dimmer LEDs follow in K.I.T.T. wheel
   const uint16_t poor_air_quality_ppm = 1600; // Above this threshold, LED breathing effect is faster.
   //NOTE: Use a class instead? NightMode could then be another state.
-  bool night_mode = false;
+  bool night_mode = true;
 }
 
 #if defined(ESP8266)
-- 
GitLab