Make SD card configurable via webserver

This commit is contained in:
Daniel Öster 2025-08-30 23:22:22 +03:00
parent 94eeccb017
commit 6c8326fce1
8 changed files with 132 additions and 128 deletions

View file

@ -80,10 +80,10 @@ void setup() {
return;
}
#if defined(LOG_CAN_TO_SD) || defined(LOG_TO_SD)
if (datalayer.system.info.CAN_SD_logging_active || datalayer.system.info.SD_logging_active) {
xTaskCreatePinnedToCore((TaskFunction_t)&logging_loop, "logging_loop", 4096, NULL, TASK_CONNECTIVITY_PRIO,
&logging_loop_task, esp32hal->WIFICORE());
#endif
}
if (!init_contactors()) {
return;
@ -167,22 +167,21 @@ void setup() {
// Loop empty, all functionality runs in tasks
void loop() {}
#if defined(LOG_CAN_TO_SD) || defined(LOG_TO_SD)
void logging_loop(void*) {
init_logging_buffers();
init_sdcard();
while (true) {
#ifdef LOG_TO_SD
if (datalayer.system.info.SD_logging_active) {
write_log_to_sdcard();
#endif
#ifdef LOG_CAN_TO_SD
}
if (datalayer.system.info.CAN_SD_logging_active) {
write_can_frame_to_sdcard();
#endif
}
}
}
#endif
void connectivity_loop(void*) {
esp_task_wdt_add(NULL); // Register this task with WDT

View file

@ -107,8 +107,6 @@
//#define EQUIPMENT_STOP_BUTTON // Enable this to allow an equipment stop button connected to the Battery-Emulator to disengage the battery
//#define LFP_CHEMISTRY //Tesla specific setting, enable this line to startup in LFP mode
//#define INTERLOCK_REQUIRED //Nissan LEAF specific setting, if enabled requires both high voltage conenctors to be seated before starting
//#define LOG_TO_SD //Enable this line to log diagnostic data to SD card (WARNING, raises CPU load, do not use for production)
//#define LOG_CAN_TO_SD //Enable this line to log incoming/outgoing CAN & CAN-FD messages to SD card (WARNING, raises CPU load, do not use for production)
/* CAN options */
//#define CAN_ADDON //Enable this line to activate an isolated secondary CAN Bus using add-on MCP2515 chip (Needed for some inverters / double battery)
#define CRYSTAL_FREQUENCY_MHZ 8 //CAN_ADDON option, what is your MCP2515 add-on boards crystal frequency?
@ -221,10 +219,6 @@ extern IPAddress gateway;
extern IPAddress subnet;
#endif
#if defined(DEBUG_VIA_USB) || defined(LOG_TO_SD)
#define DEBUG_LOG
#endif
#if defined(MEB_BATTERY)
#define PRECHARGE_CONTROL
#endif

View file

@ -138,6 +138,8 @@ void init_stored_settings() {
datalayer.system.info.CAN_usb_logging_active = settings.getBool("CANLOGUSB", false);
datalayer.system.info.usb_logging_active = settings.getBool("USBENABLED", false);
datalayer.system.info.web_logging_active = settings.getBool("WEBENABLED", false);
datalayer.system.info.CAN_SD_logging_active = settings.getBool("CANLOGSD", false);
datalayer.system.info.SD_logging_active = settings.getBool("SDLOGENABLED", false);
// WIFI AP is enabled by default unless disabled in the settings
wifiap_enabled = settings.getBool("WIFIAPENABLED", true);

View file

@ -244,9 +244,13 @@ struct DATALAYER_SYSTEM_INFO_TYPE {
/** bool, determines if USB serial logging should occur */
bool CAN_usb_logging_active = false;
/** bool, determines if USB serial logging should occur */
bool CAN_SD_logging_active = false;
/** bool, determines if USB serial logging should occur */
bool usb_logging_active = false;
/** bool, determines if general logging should be active for webserver */
bool web_logging_active = false;
/** bool, determines if general logging to SD card should be active */
bool SD_logging_active = false;
/** uint8_t, enumeration which CAN interface should be used for log playback */
uint8_t can_replay_interface = CAN_NATIVE;
/** bool, determines if CAN replay should loop or not */

View file

@ -63,9 +63,7 @@ void add_can_frame_to_buffer(CAN_frame frame, frameDirection msgDir) {
currentTime / 1000, currentTime % 1000, (msgDir == MSG_RX ? "RX0" : "TX1"), frame.ID, frame.DLC);
if (xRingbufferSend(can_bufferHandle, &messagestr_buffer, size, pdMS_TO_TICKS(2)) != pdTRUE) {
#ifdef DEBUG_VIA_USB
Serial.println("Failed to send message to can ring buffer!");
#endif // DEBUG_VIA_USB
logging.println("Failed to send message to can ring buffer!");
return;
}
@ -77,9 +75,7 @@ void add_can_frame_to_buffer(CAN_frame frame, frameDirection msgDir) {
size = snprintf(messagestr_buffer, sizeof(messagestr_buffer), "%02X\n", frame.data.u8[i]);
if (xRingbufferSend(can_bufferHandle, &messagestr_buffer, size, pdMS_TO_TICKS(2)) != pdTRUE) {
#ifdef DEBUG_VIA_USB
Serial.println("Failed to send message to can ring buffer!");
#endif // DEBUG_VIA_USB
logging.println("Failed to send message to can ring buffer!");
return;
}
}
@ -127,9 +123,7 @@ void add_log_to_buffer(const uint8_t* buffer, size_t size) {
return;
if (xRingbufferSend(log_bufferHandle, buffer, size, pdMS_TO_TICKS(1)) != pdTRUE) {
#ifdef DEBUG_VIA_USB
Serial.println("Failed to send message to log ring buffer!");
#endif // DEBUG_VIA_USB
logging.println("Failed to send message to log ring buffer!");
return;
}
}
@ -161,21 +155,22 @@ void write_log_to_sdcard() {
}
void init_logging_buffers() {
#if defined(LOG_CAN_TO_SD)
if (datalayer.system.info.CAN_SD_logging_active) {
can_bufferHandle = xRingbufferCreate(32 * 1024, RINGBUF_TYPE_BYTEBUF);
if (can_bufferHandle == NULL) {
logging.println("Failed to create CAN ring buffer!");
return;
}
#endif // defined(LOG_CAN_TO_SD)
}
#if defined(LOG_TO_SD)
if (datalayer.system.info.SD_logging_active) {
log_bufferHandle = xRingbufferCreate(1024, RINGBUF_TYPE_BYTEBUF);
if (log_bufferHandle == NULL) {
logging.println("Failed to create log ring buffer!");
return;
}
#endif // defined(LOG_TO_SD)
}
}
bool init_sdcard() {

View file

@ -57,9 +57,9 @@ String debug_logger_processor(void) {
content += "<button onclick='refreshPage()'>Refresh data</button> ";
}
content += "<button onclick='exportLog()'>Export to .txt</button> ";
#ifdef LOG_TO_SD
if (datalayer.system.info.SD_logging_active) {
content += "<button onclick='deleteLog()'>Delete log file</button> ";
#endif
}
content += "<button onclick='goToMainPage()'>Back to main page</button>";
// Start a new block for the debug log messages
@ -98,9 +98,9 @@ String debug_logger_processor(void) {
content += "<script>";
content += "function refreshPage(){ location.reload(true); }";
content += "function exportLog() { window.location.href = '/export_log'; }";
#ifdef LOG_TO_SD
if (datalayer.system.info.SD_logging_active) {
content += "function deleteLog() { window.location.href = '/delete_log'; }";
#endif
}
content += "function goToMainPage() { window.location.href = '/'; }";
content += "</script>";
content += index_html_footer;

View file

@ -266,6 +266,14 @@ String settings_processor(const String& var, BatteryEmulatorSettingsStore& setti
return settings.getBool("WEBENABLED") ? "checked" : "";
}
if (var == "CANLOGSD") {
return settings.getBool("CANLOGSD") ? "checked" : "";
}
if (var == "SDLOGENABLED") {
return settings.getBool("SDLOGENABLED") ? "checked" : "";
}
if (var == "MQTTENABLED") {
return settings.getBool("MQTTENABLED") ? "checked" : "";
}
@ -883,6 +891,12 @@ const char* getCANInterfaceName(CAN_Interface interface) {
<label>Enable logging via Webserver: </label>
<input type='checkbox' name='WEBENABLED' value='on' style='margin-left: 0;' %WEBENABLED% />
<label>Enable CAN logging via SD card: </label>
<input type='checkbox' name='CANLOGSD' value='on' style='margin-left: 0;' %CANLOGSD% />
<label>Enable logging via SD card: </label>
<input type='checkbox' name='SDLOGENABLED' value='on' style='margin-left: 0;' %SDLOGENABLED% />
<label>Enable MQTT: </label>
<input type='checkbox' name='MQTTENABLED' value='on' style='margin-left: 0;' %MQTTENABLED% />

View file

@ -267,7 +267,7 @@ void init_webserver() {
}
});
if (datalayer.system.info.web_logging_active) { //|| defined(LOG_TO_SD)
if (datalayer.system.info.web_logging_active || datalayer.system.info.SD_logging_active) {
// Route for going to debug logging web page
server.on("/log", HTTP_GET, [](AsyncWebServerRequest* request) {
AsyncWebServerResponse* response = request->beginResponse(200, "text/html", debug_logger_processor());
@ -289,7 +289,20 @@ void init_webserver() {
},
handleFileUpload);
#ifndef LOG_CAN_TO_SD
if (datalayer.system.info.CAN_SD_logging_active) {
// Define the handler to export can log
server.on("/export_can_log", HTTP_GET, [](AsyncWebServerRequest* request) {
pause_can_writing();
request->send(SD_MMC, CAN_LOG_FILE, String(), true);
resume_can_writing();
});
// Define the handler to delete can log
server.on("/delete_can_log", HTTP_GET, [](AsyncWebServerRequest* request) {
delete_can_log();
request->send(200, "text/plain", "Log file deleted");
});
} else {
// Define the handler to export can log
server.on("/export_can_log", HTTP_GET, [](AsyncWebServerRequest* request) {
String logs = String(datalayer.system.info.logged_can_messages);
@ -316,24 +329,9 @@ void init_webserver() {
response->addHeader("Content-Disposition", String("attachment; filename=\"") + String(filename) + "\"");
request->send(response);
});
#endif
}
#ifdef LOG_CAN_TO_SD
// Define the handler to export can log
server.on("/export_can_log", HTTP_GET, [](AsyncWebServerRequest* request) {
pause_can_writing();
request->send(SD_MMC, CAN_LOG_FILE, String(), true);
resume_can_writing();
});
// Define the handler to delete can log
server.on("/delete_can_log", HTTP_GET, [](AsyncWebServerRequest* request) {
delete_can_log();
request->send(200, "text/plain", "Log file deleted");
});
#endif
#ifdef LOG_TO_SD
if (datalayer.system.info.SD_logging_active) {
// Define the handler to delete log file
server.on("/delete_log", HTTP_GET, [](AsyncWebServerRequest* request) {
delete_log();
@ -346,9 +344,7 @@ void init_webserver() {
request->send(SD_MMC, LOG_FILE, String(), true);
resume_log_writing();
});
#endif
#ifndef LOG_TO_SD
} else {
// Define the handler to export debug log
server.on("/export_log", HTTP_GET, [](AsyncWebServerRequest* request) {
String logs = String(datalayer.system.info.logged_can_messages);
@ -375,7 +371,7 @@ void init_webserver() {
response->addHeader("Content-Disposition", String("attachment; filename=\"") + String(filename) + "\"");
request->send(response);
});
#endif
}
// Route for going to cellmonitor web page
def_route_with_auth("/cellmonitor", server, HTTP_GET, [](AsyncWebServerRequest* request) {
@ -413,8 +409,8 @@ void init_webserver() {
};
const char* boolSettingNames[] = {
"DBLBTR", "CNTCTRL", "CNTCTRLDBL", "PWMCNTCTRL", "PERBMSRESET",
"REMBMSRESET", "USBENABLED", "CANLOGUSB", "WEBENABLED", "CANFDASCAN",
"DBLBTR", "CNTCTRL", "CNTCTRLDBL", "PWMCNTCTRL", "PERBMSRESET", "SDLOGENABLED",
"REMBMSRESET", "USBENABLED", "CANLOGUSB", "WEBENABLED", "CANFDASCAN", "CANLOGSD",
"WIFIAPENABLED", "MQTTENABLED", "HADISC", "MQTTTOPICS", "INVICNT",
};
@ -1389,7 +1385,7 @@ String processor(const String& var) {
content += "<button onclick='Advanced()'>More Battery Info</button> ";
content += "<button onclick='CANlog()'>CAN logger</button> ";
content += "<button onclick='CANreplay()'>CAN replay</button> ";
if (datalayer.system.info.web_logging_active) { //|| defined(LOG_TO_SD)
if (datalayer.system.info.web_logging_active || datalayer.system.info.SD_logging_active) {
content += "<button onclick='Log()'>Log</button> ";
}
content += "<button onclick='Cellmon()'>Cellmonitor</button> ";