Commit 40c139a8 authored by Eric Duminil's avatar Eric Duminil
Browse files

Always initialize filesystem, but csv can be off

parent b9002c70
Pipeline #5773 passed with stage
in 2 minutes and 8 seconds
...@@ -111,9 +111,7 @@ void setup() { ...@@ -111,9 +111,7 @@ void setup() {
sensor::initialize(); sensor::initialize();
#ifdef AMPEL_CSV
csv_writer::initialize(ampel.sensorId); csv_writer::initialize(ampel.sensorId);
#endif
#ifdef AMPEL_WIFI // Structure doesn't make sense anymore #ifdef AMPEL_WIFI // Structure doesn't make sense anymore
wifi::defineCommands(); wifi::defineCommands();
...@@ -161,10 +159,10 @@ void loop() { ...@@ -161,10 +159,10 @@ void loop() {
checkSerialInput(); checkSerialInput();
if (sensor::processData()) { if (sensor::processData()) {
#ifdef AMPEL_CSV if (config::csv_active()) {
//TODO: Check if not in AP mode. Lines get written with 1970 otherwise. //TODO: Check if not in AP mode. Lines get written with 1970 otherwise.
csv_writer::logIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity); csv_writer::logIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
#endif }
#if defined(AMPEL_WIFI) #if defined(AMPEL_WIFI)
if (config::mqtt_active()) { if (config::mqtt_active()) {
......
...@@ -292,9 +292,12 @@ namespace config { ...@@ -292,9 +292,12 @@ namespace config {
bool &daylight_saving_time = web_config::dstParam.value(); bool &daylight_saving_time = web_config::dstParam.value();
// CSV // CSV
bool csv_active() {
return web_config::csvParams.isActive();
}
uint16_t &csv_interval = web_config::csvTimestepParam.value(); uint16_t &csv_interval = web_config::csvTimestepParam.value();
// MQTT // MQTT
bool mqtt_active() { bool mqtt_active() {
return web_config::mqttParams.isActive(); return web_config::mqttParams.isActive();
} }
......
...@@ -28,7 +28,7 @@ namespace config { ...@@ -28,7 +28,7 @@ namespace config {
extern bool &daylight_saving_time; // [true / false] extern bool &daylight_saving_time; // [true / false]
//CSV //CSV
//TODO: Add use_csv bool csv_active();
extern uint16_t &csv_interval; // [s] extern uint16_t &csv_interval; // [s]
// MQTT // MQTT
......
...@@ -7,9 +7,7 @@ ...@@ -7,9 +7,7 @@
#include "wifi_util.h" #include "wifi_util.h"
#include "co2_sensor.h" #include "co2_sensor.h"
#include "sensor_console.h" #include "sensor_console.h"
#ifdef AMPEL_CSV #include "csv_writer.h"
# include "csv_writer.h"
#endif
#include "mqtt.h" #include "mqtt.h"
#ifdef AMPEL_LORAWAN #ifdef AMPEL_LORAWAN
# include "lorawan.h" # include "lorawan.h"
...@@ -50,10 +48,8 @@ namespace web_server { ...@@ -50,10 +48,8 @@ namespace web_server {
void handlePageNotFound(); void handlePageNotFound();
void handleWebServerCommand(); void handleWebServerCommand();
#ifdef AMPEL_CSV
void handleDeleteCSV(); void handleDeleteCSV();
void handleWebServerCSV(); void handleWebServerCSV();
#endif
void definePages() { void definePages() {
header_template = header_template =
...@@ -79,12 +75,10 @@ namespace web_server { ...@@ -79,12 +75,10 @@ namespace web_server {
"<div class='pure-u-1'><ul class='pure-menu pure-menu-horizontal pure-menu-list'>\n" "<div class='pure-u-1'><ul class='pure-menu pure-menu-horizontal pure-menu-list'>\n"
"<li class='pure-menu-item'><a href='/config' class='pure-menu-link'>Config</a></li>\n" "<li class='pure-menu-item'><a href='/config' class='pure-menu-link'>Config</a></li>\n"
"<li class='pure-menu-item'><a href='#table' class='pure-menu-link'>Info</a></li>\n" "<li class='pure-menu-item'><a href='#table' class='pure-menu-link'>Info</a></li>\n"
#ifdef AMPEL_CSV
"<li class='pure-menu-item'><a href='#graph' class='pure-menu-link'>Graph</a></li>\n" "<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='#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'><a href='%s' class='pure-menu-link'>Download CSV</a></li>\n"
#endif "<li class='pure-menu-item' id='led'>&#11044;</li>\n"// LED
"<li class='pure-menu-item' id='led'>&#11044;</li>\n" // LED
"</ul></div></div>\n" "</ul></div></div>\n"
"<script>\n" "<script>\n"
// Show a colored dot on the webpage, with a similar color than on LED Ring. // Show a colored dot on the webpage, with a similar color than on LED Ring.
...@@ -104,12 +98,10 @@ namespace web_server { ...@@ -104,12 +98,10 @@ namespace web_server {
"<tr><td>Humidity</td><td>%.1f%%</td></tr>\n" "<tr><td>Humidity</td><td>%.1f%%</td></tr>\n"
"<tr><td>Last measurement</td><td>%s</td></tr>\n" "<tr><td>Last measurement</td><td>%s</td></tr>\n"
"<tr><td>Measurement timestep</td><td>%5d s</td></tr>\n" "<tr><td>Measurement timestep</td><td>%5d s</td></tr>\n"
#ifdef AMPEL_CSV
"<tr><th colspan='2'>CSV</th></tr>\n" "<tr><th colspan='2'>CSV</th></tr>\n"
"<tr><td>Last write</td><td>%s</td></tr>\n" "<tr><td>Last write</td><td>%s</td></tr>\n"
"<tr><td>Timestep</td><td>%5d s</td></tr>\n" "<tr><td>Timestep</td><td>%5d s</td></tr>\n"
"<tr><td>Available drive space</td><td>%d kB</td></tr>\n" "<tr><td>Available drive space</td><td>%d kB</td></tr>\n"
#endif
"<tr><th colspan='2'>MQTT</th></tr>\n" "<tr><th colspan='2'>MQTT</th></tr>\n"
"<tr><td>Connected?</td><td>%s</td></tr>\n" "<tr><td>Connected?</td><td>%s</td></tr>\n"
"<tr><td>Last publish</td><td>%s</td></tr>\n" "<tr><td>Last publish</td><td>%s</td></tr>\n"
...@@ -137,68 +129,62 @@ namespace web_server { ...@@ -137,68 +129,62 @@ namespace web_server {
"</table>\n" "</table>\n"
"<div id='log' class='pure-u-1 pure-u-md-1-2'></div>\n" "<div id='log' class='pure-u-1 pure-u-md-1-2'></div>\n"
"<form action='/command'><input type='text' id='send' name='send'><input type='submit' value='Send'></form>\n" "<form action='/command'><input type='text' id='send' name='send'><input type='submit' value='Send'></form>\n"
#ifdef AMPEL_CSV
"<form action='/delete_csv' method='POST' onsubmit=\"return confirm('Are you really sure you want to delete all data?') && (document.body.style.cursor = 'wait');\">" "<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'/>" "<input type='submit' value='Delete CSV'/>"
"</form>\n" "</form>\n"
#endif
"</div>\n"); "</div>\n");
script_template = script_template =
PSTR( PSTR(
"<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-firmware' target='_blank'>Source code</a>\n"
"<a href='https://transfer.hft-stuttgart.de/gitlab/co2ampel/ampel-documentation' target='_blank'>Documentation</a>\n" "<a href='https://transfer.hft-stuttgart.de/gitlab/co2ampel/ampel-documentation' target='_blank'>Documentation</a>\n"
#ifdef AMPEL_CSV "<script>\n"
"<script>\n" "document.body.style.cursor = 'default';\n"
"document.body.style.cursor = 'default';\n" "fetch('%s',{credentials:'include'})\n"
"fetch('%s',{credentials:'include'})\n" ".then(response=>response.text())\n"
".then(response=>response.text())\n" ".then(csvText=>csvToTable(csvText))\n"
".then(csvText=>csvToTable(csvText))\n" ".then(htmlTable=>addLogTableToPage(htmlTable))\n"
".then(htmlTable=>addLogTableToPage(htmlTable))\n" ".then(_=>Plotly.newPlot('graph',data,layout,{displaylogo:false}))\n"
".then(_=>Plotly.newPlot('graph',data,layout,{displaylogo:false}))\n" ".catch(e=>console.error(e));\n"
".catch(e=>console.error(e));\n" "xs=[];\n"
"xs=[];\n" "data=[{x:xs,y:[],type:'scatter',name:'CO<sub>2</sub>',line:{color:'#2ca02c'}},\n"
"data=[{x:xs,y:[],type:'scatter',name:'CO<sub>2</sub>',line:{color:'#2ca02c'}},\n" "{x:xs,y:[],type:'scatter',name:'Temperature',yaxis:'y2',line:{color:'#ff7f0e',dash:'dot'}},\n"
"{x:xs,y:[],type:'scatter',name:'Temperature',yaxis:'y2',line:{color:'#ff7f0e',dash:'dot'}},\n" "{x:xs,y:[],type:'scatter',name:'Humidity',yaxis:'y3',line:{color:'#1f77b4',dash:'dot'}}];\n"
"{x:xs,y:[],type:'scatter',name:'Humidity',yaxis:'y3',line:{color:'#1f77b4',dash:'dot'}}];\n" "layout={height:600,title:'%s',legend:{xanchor:'right',x:0.2,y:1.0},\n"
"layout={height:600,title:'%s',legend:{xanchor:'right',x:0.2,y:1.0},\n" "xaxis:{domain:[0.0,0.85]},yaxis:{ticksuffix:'ppm',range:[0,2000],dtick:200},\n"
"xaxis:{domain:[0.0,0.85]},yaxis:{ticksuffix:'ppm',range:[0,2000],dtick:200},\n" "yaxis2:{overlaying:'y',side:'right',ticksuffix:'°C',position:0.9,anchor:'free',range:[0,30],dtick:3},\n"
"yaxis2:{overlaying:'y',side:'right',ticksuffix:'°C',position:0.9,anchor:'free',range:[0,30],dtick:3},\n" "yaxis3:{overlaying:'y',side:'right',ticksuffix:'%%',position:0.95,anchor:'free',range:[0,100],dtick:10}\n"
"yaxis3:{overlaying:'y',side:'right',ticksuffix:'%%',position:0.95,anchor:'free',range:[0,100],dtick:10}\n" "};\n"
"};\n" "function csvToTable(csvText) {\n"
"function csvToTable(csvText) {\n" "csvText=csvText.trim();\n"
"csvText=csvText.trim();\n" "lines=csvText.split('\\n');\n"
"lines=csvText.split('\\n');\n" "table=document.createElement('table');\n"
"table=document.createElement('table');\n" "table.className='pure-table-striped';\n"
"table.className='pure-table-striped';\n" "n=lines.length;\n"
"n=lines.length;\n" "lines.forEach((line,i)=>{\n"
"lines.forEach((line,i)=>{\n" "fields=line.split(';');\n"
"fields=line.split(';');\n" "xs.push(fields[0]);\n"
"xs.push(fields[0]);\n" "data[0]['y'].push(fields[1]);\n"
"data[0]['y'].push(fields[1]);\n" "data[1]['y'].push(fields[2]);\n"
"data[1]['y'].push(fields[2]);\n" "data[2]['y'].push(fields[3]);\n"
"data[2]['y'].push(fields[3]);\n" "if(i>4 && i<n-12){if(i==5){fields=['...','...','...','...']}else{return;}}\n"
"if(i>4 && i<n-12){if(i==5){fields=['...','...','...','...']}else{return;}}\n" "row=document.createElement('tr');\n"
"row=document.createElement('tr');\n" "fields.forEach((field,index)=>{\n"
"fields.forEach((field,index)=>{\n" "cell=document.createElement(i<2?'th':'td');\n"
"cell=document.createElement(i<2?'th':'td');\n" "cell.appendChild(document.createTextNode(field));\n"
"cell.appendChild(document.createTextNode(field));\n" "row.appendChild(cell);});\n"
"row.appendChild(cell);});\n" "table.appendChild(row);});\n"
"table.appendChild(row);});\n" "return table;}\n"
"return table;}\n" "function addLogTableToPage(table){document.getElementById('log').appendChild(table);}\n"
"function addLogTableToPage(table){document.getElementById('log').appendChild(table);}\n" "</script>\n"
"</script>\n" "</body>\n"
#endif "</html>");
"</body>\n"
"</html>");
// Web-server // Web-server
web_config::http.on("/", handleWebServerRoot); web_config::http.on("/", handleWebServerRoot);
web_config::http.on("/command", handleWebServerCommand); web_config::http.on("/command", handleWebServerCommand);
#ifdef AMPEL_CSV web_config::http.on(csv_writer::filename, handleWebServerCSV);
web_config::http.on(csv_writer::filename, handleWebServerCSV); //NOTE: csv_writer should have been initialized first.
web_config::http.on("/delete_csv", HTTP_POST, handleDeleteCSV); web_config::http.on("/delete_csv", HTTP_POST, handleDeleteCSV);
#endif
} }
// Allow access if http_user or http_password are empty, or if provided credentials match // Allow access if http_user or http_password are empty, or if provided credentials match
...@@ -228,11 +214,8 @@ namespace web_server { ...@@ -228,11 +214,8 @@ namespace web_server {
char content[2000]; // Update if needed char content[2000]; // Update if needed
// INFO - Header size : 1767 - Body size : 1991 - Script size : 1909 // INFO - Header size : 1767 - Body size : 1991 - Script size : 1909
snprintf_P(content, sizeof(content), header_template, sensor::co2, ampel.sensorId, wifi::local_ip snprintf_P(content, sizeof(content), header_template, sensor::co2, ampel.sensorId, wifi::local_ip,
#ifdef AMPEL_CSV csv_writer::filename);
, csv_writer::filename
#endif
);
// Serial.print(F("INFO - Header size : ")); // Serial.print(F("INFO - Header size : "));
// Serial.print(strlen(content)); // Serial.print(strlen(content));
...@@ -241,11 +224,9 @@ namespace web_server { ...@@ -241,11 +224,9 @@ namespace web_server {
// Body // Body
snprintf_P(content, sizeof(content), body_template, ampel.sensorId, sensor::co2, sensor::temperature, snprintf_P(content, sizeof(content), body_template, ampel.sensorId, sensor::co2, sensor::temperature,
sensor::humidity, sensor::timestamp, config::measurement_timestep, sensor::humidity, sensor::timestamp, config::measurement_timestep, csv_writer::last_successful_write,
#ifdef AMPEL_CSV config::csv_interval, csv_writer::getAvailableSpace() / 1024, mqtt::connected ? "Yes" : "No",
csv_writer::last_successful_write, config::csv_interval, csv_writer::getAvailableSpace() / 1024, mqtt::last_successful_publish, config::mqtt_sending_interval,
#endif
mqtt::connected ? "Yes" : "No", mqtt::last_successful_publish, config::mqtt_sending_interval,
#if defined(AMPEL_LORAWAN) && defined(ESP32) #if defined(AMPEL_LORAWAN) && defined(ESP32)
lorawan::connected ? "Yes" : "No", config::lorawan_frequency_plan, lorawan::last_transmission, lorawan::connected ? "Yes" : "No", config::lorawan_frequency_plan, lorawan::last_transmission,
config::lorawan_sending_interval, config::lorawan_sending_interval,
...@@ -259,18 +240,13 @@ namespace web_server { ...@@ -259,18 +240,13 @@ namespace web_server {
web_config::http.sendContent(content); web_config::http.sendContent(content);
// Script // Script
snprintf_P(content, sizeof(content), script_template snprintf_P(content, sizeof(content), script_template, csv_writer::filename, ampel.sensorId);
#ifdef AMPEL_CSV
, csv_writer::filename, ampel.sensorId
#endif
);
// Serial.print(F(" - Script size : ")); // Serial.print(F(" - Script size : "));
// Serial.println(strlen(content)); // Serial.println(strlen(content));
web_config::http.sendContent(content); web_config::http.sendContent(content);
} }
#ifdef AMPEL_CSV
void handleWebServerCSV() { void handleWebServerCSV() {
if (!shouldBeAllowed()) { if (!shouldBeAllowed()) {
return web_config::http.requestAuthentication(DIGEST_AUTH); return web_config::http.requestAuthentication(DIGEST_AUTH);
...@@ -297,7 +273,6 @@ namespace web_server { ...@@ -297,7 +273,6 @@ namespace web_server {
web_config::http.sendHeader("Location", "/"); web_config::http.sendHeader("Location", "/");
web_config::http.send(303); web_config::http.send(303);
} }
#endif
void handleWebServerCommand() { void handleWebServerCommand() {
if (!shouldBeAllowed()) { if (!shouldBeAllowed()) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment