From a9ddfba060fcfddb823ba14212e295d69edc57db Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Sat, 26 Dec 2020 21:39:49 +0100
Subject: [PATCH] CSV_WRITER can be disabled if desired

---
 ampel-firmware.h   | 12 +++++++-----
 ampel-firmware.ino |  3 ++-
 co2_sensor.cpp     |  3 +++
 config.public.h    |  3 +++
 web_server.cpp     | 22 +++++++++++++++++-----
 5 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/ampel-firmware.h b/ampel-firmware.h
index 530f686..cd211f4 100644
--- a/ampel-firmware.h
+++ b/ampel-firmware.h
@@ -8,23 +8,25 @@
 #ifndef MEASUREMENT_TIMESTEP
 #  error Missing config.h file. Please copy config.example.h to config.h.
 #endif
+
+#ifdef CSV_WRITER
+#  include "csv_writer.h"
+#endif
 #ifdef MQTT
 #  include "mqtt.h"
 #endif
 #ifdef LORAWAN
 #  include "lorawan.h"
 #endif
+#ifdef HTTP
+#  include "web_server.h"
+#endif
 
 #include "util.h"
 #include "wifi_util.h"
 #include "co2_sensor.h"
 
-#ifdef HTTP
-#  include "web_server.h"
-#endif
-
 #include "led_effects.h"
-#include "csv_writer.h"
 
 #if defined(ESP8266)
 //allows sensor to be seen as SENSOR_ID.local, from the local network. For example : espd03cc5.local
diff --git a/ampel-firmware.ino b/ampel-firmware.ino
index 526e5d8..bdce462 100644
--- a/ampel-firmware.ino
+++ b/ampel-firmware.ino
@@ -100,8 +100,9 @@ void setup() {
 #endif
   }
 
-  //TODO: Add a config to deactivate CSV logging
+#ifdef CSV_WRITER
   csv_writer::initialize();
+#endif
 
 #if defined(LORAWAN) && defined(ESP32)
   lorawan::initialize();
diff --git a/co2_sensor.cpp b/co2_sensor.cpp
index 86fd8b4..6e3dec3 100644
--- a/co2_sensor.cpp
+++ b/co2_sensor.cpp
@@ -178,7 +178,10 @@ namespace sensor {
       logToSerial();
 
       //TODO: Move the 3 back to ampel-firmware.ino and remove headers from co2_sensor.h
+
+#ifdef CSV_WRITER
       csv_writer::logIfTimeHasCome(timestamp, co2, temperature, humidity);
+#endif
 
 #ifdef MQTT
       mqtt::publishIfTimeHasCome(timestamp, co2, temperature, humidity);
diff --git a/config.public.h b/config.public.h
index 861375c..d3c9c50 100644
--- a/config.public.h
+++ b/config.public.h
@@ -26,6 +26,9 @@
 // #  define MQTT_SENDING_INTERVAL MEASUREMENT_TIMESTEP * 5 // [s]
 #  define MQTT_SENDING_INTERVAL 300 // [s]
 
+// Should data be written to the sensor Flash memory? Writing too often might damage the ESP memory
+#  define CSV_WRITER // Comment or remove this line if you want to disable CSV logging.
+
 // How often should measurements be appended to CSV ?
 // Probably a good idea to use a multiple of MEASUREMENT_TIMESTEP, so that averages can be calculated
 // Set to 0 if you want to send values after each measurement
diff --git a/web_server.cpp b/web_server.cpp
index d2458f5..4890628 100644
--- a/web_server.cpp
+++ b/web_server.cpp
@@ -57,11 +57,13 @@ namespace web_server {
 
             "<div class='pure-g'><div class='pure-u-1'><div class='pure-menu'><p class='pure-menu-heading'>HfT-Stuttgart CO<sub>2</sub> Ampel</p></div></div>\n"
             "<div class='pure-u-1'><ul class='pure-menu pure-menu-horizontal pure-menu-list'>\n"
-            "<li class='pure-menu-item'><a href='#graph' class='pure-menu-link'>Graph</a></li>\n"
             "<li class='pure-menu-item'><a href='#table' class='pure-menu-link'>Info</a></li>\n"
+#ifdef CSV_WRITER
+            "<li class='pure-menu-item'><a href='#graph' class='pure-menu-link'>Graph</a></li>\n"
             "<li class='pure-menu-item'><a href='#log' class='pure-menu-link'>Log</a></li>\n"
             "<li class='pure-menu-item'><a href='./%s' class='pure-menu-link'>Download CSV</a></li>\n"
-            "<li class='pure-menu-item' id='led'>&#11044;</li>\n"// LED
+#endif
+            "<li class='pure-menu-item' id='led'>&#11044;</li>\n" // LED
             "</ul></div></div>\n"
             "<script>\n"
             // Show a colored dot on the webpage, with a similar color than on LED Ring.
@@ -82,8 +84,10 @@ namespace web_server {
             "<tr><td>Humidity</td><td>%.1f%%</td></tr>\n"
             "<tr><td>Last measurement</td><td>%s</td></tr>\n"
             "<tr><td>Measurement timestep</td><td>%5d s</td></tr>\n"
+#ifdef CSV_WRITER
             "<tr><td>Last CSV write</td><td>%s</td></tr>\n"
             "<tr><td>CSV timestep</td><td>%5d s</td></tr>\n"
+#endif
 #ifdef MQTT
             "<tr><td>Last MQTT publish</td><td>%s</td></tr>\n"
             "<tr><td>MQTT publish timestep</td><td>%5d s</td></tr>\n"
@@ -103,14 +107,18 @@ namespace web_server {
             "<tr><td>Uptime</td><td>%4d h %02d min %02d s</td></tr>\n"
             "</table>\n"
             "<div id='log' class='pure-u-1 pure-u-md-1-2'></div>\n"
+#ifdef CSV_WRITER
             "<form action='/delete_csv' method='POST' onsubmit=\"return confirm('Are you really sure you want to delete all data?') && (document.body.style.cursor = 'wait');\">"
             "<input type='submit' value='Delete CSV'/>"
             "</form>\n"
+#endif
             "</div>\n"
             "<a href='https://transfer.hft-stuttgart.de/gitlab/co2ampel/ampel-firmware' target='_blank'>Source code</a>\n"
             "<a href='https://transfer.hft-stuttgart.de/gitlab/co2ampel/ampel-documentation' target='_blank'>Documentation</a>\n");
 
-    script_template = PSTR("<script>\n"
+    script_template = PSTR(
+#ifdef CSV_WRITER
+        "<script>\n"
         "document.body.style.cursor = 'default';\n"
         "fetch('./%s',{credentials:'include'})\n"
         // Get CSV, fill table and fill diagram
@@ -150,13 +158,15 @@ namespace web_server {
         "return table;}\n"
         "function addLogTableToPage(table){document.getElementById('log').appendChild(table);}\n"
         "</script>\n"
-        "</body>\n"
-        "</html>");
+#endif
+        "</body>\n" "</html>");
 
     // Web-server
     http.on("/", handleWebServerRoot);
+#ifdef CSV_WRITER
     http.on("/" + csv_writer::filename, handleWebServerCSV);
     http.on("/delete_csv", HTTP_POST, handleDeleteCSV);
+#endif
     http.onNotFound(handlePageNotFound);
     http.begin();
 
@@ -198,7 +208,9 @@ namespace web_server {
     // Body
     snprintf_P(content, sizeof(content), body_template, SENSOR_ID.c_str(), sensor::co2, sensor::temperature,
         sensor::humidity, sensor::timestamp.c_str(), config::measurement_timestep,
+#ifdef CSV_WRITER
         csv_writer::last_successful_write.c_str(), config::csv_interval,
+#endif
 #ifdef MQTT
         mqtt::last_successful_publish.c_str(), config::sending_interval,
 #endif
-- 
GitLab