diff --git a/ESP32_CAN_Logger.ino b/ESP32_CAN_Logger.ino index 9da4005..290d3c6 100644 --- a/ESP32_CAN_Logger.ino +++ b/ESP32_CAN_Logger.ino @@ -233,6 +233,7 @@ int commentCount = 0; void loadSettings() { preferences.begin("can-logger", false); + // WiFi 설정 로드 preferences.getString("wifi_ssid", wifiSSID, sizeof(wifiSSID)); preferences.getString("wifi_pass", wifiPassword, sizeof(wifiPassword)); @@ -244,12 +245,27 @@ void loadSettings() { strcpy(wifiPassword, "12345678"); } + // CAN 속도 로드 (기본값: 1Mbps = 3) + int speedIndex = preferences.getInt("can_speed", 3); + if (speedIndex >= 0 && speedIndex < 4) { + currentCanSpeed = canSpeedValues[speedIndex]; + Serial.printf("✓ 저장된 CAN 속도 로드: %s\n", canSpeedNames[speedIndex]); + } + + // MCP2515 모드 로드 (기본값: Normal = 0) + int mcpMode = preferences.getInt("mcp_mode", 0); + if (mcpMode >= 0 && mcpMode <= 3) { + currentMcpMode = (MCP2515Mode)mcpMode; + Serial.printf("✓ 저장된 MCP 모드 로드: %d\n", mcpMode); + } + preferences.end(); } void saveSettings() { preferences.begin("can-logger", false); + // WiFi 설정 저장 preferences.putString("wifi_ssid", wifiSSID); preferences.putString("wifi_pass", wifiPassword); @@ -263,6 +279,31 @@ void saveSettings() { Serial.println("⚠️ 재부팅 후 WiFi 설정이 적용됩니다."); } +void saveCANSettings() { + preferences.begin("can-logger", false); + + // CAN 속도 저장 (인덱스로 저장) + int speedIndex = 3; // 기본값: 1M + for (int i = 0; i < 4; i++) { + if (canSpeedValues[i] == currentCanSpeed) { + speedIndex = i; + break; + } + } + preferences.putInt("can_speed", speedIndex); + + // MCP2515 모드 저장 + preferences.putInt("mcp_mode", (int)currentMcpMode); + + preferences.end(); + + Serial.println("\n✓ CAN 설정 저장 완료:"); + Serial.println("----------------------------------------"); + Serial.printf(" CAN Speed : %s\n", canSpeedNames[speedIndex]); + Serial.printf(" MCP Mode : %d\n", (int)currentMcpMode); + Serial.println("----------------------------------------"); +} + // ======================================== // 시퀀스 관리 함수 // ======================================== @@ -1156,7 +1197,10 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) mcp2515.setBitrate(currentCanSpeed, MCP_8MHZ); setMCP2515Mode(currentMcpMode); // 현재 모드 유지 - Serial.printf("✓ CAN 속도 변경: %s\n", canSpeedNames[speedIndex]); + // 비휘발성 메모리에 저장 + saveCANSettings(); + + Serial.printf("✓ CAN 속도 변경 및 저장: %s\n", canSpeedNames[speedIndex]); } } else if (message.indexOf("\"cmd\":\"setMcpMode\"") >= 0) { @@ -1168,7 +1212,10 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) int mode = message.substring(modeStart, modeEnd).toInt(); if (mode >= 0 && mode <= 3) { // 0~3으로 확장 (TRANSMIT 모드 포함) - setMCP2515Mode((MCP2515Mode)mode); + if (setMCP2515Mode((MCP2515Mode)mode)) { + // 모드 변경 성공 시 비휘발성 메모리에 저장 + saveCANSettings(); + } } } else if (message.indexOf("\"cmd\":\"syncTimeFromPhone\"") >= 0) { @@ -1562,9 +1609,11 @@ void setup() { // MCP2515 초기화 mcp2515.reset(); - mcp2515.setBitrate(CAN_1000KBPS, MCP_8MHZ); - setMCP2515Mode(MCP_MODE_NORMAL); - Serial.println("✓ MCP2515 초기화 완료"); + // 저장된 CAN 속도 적용 (loadSettings에서 로드됨) + mcp2515.setBitrate(currentCanSpeed, MCP_8MHZ); + // 저장된 MCP 모드 적용 (loadSettings에서 로드됨) + setMCP2515Mode(currentMcpMode); + Serial.println("✓ MCP2515 초기화 완료 (저장된 설정 적용)"); // Mutex 생성 (다른 초기화보다 먼저!) sdMutex = xSemaphoreCreateMutex(); diff --git a/index.h b/index.h index 7da59dd..0909ef1 100644 --- a/index.h +++ b/index.h @@ -1155,6 +1155,10 @@ const char index_html[] PROGMEM = R"rawliteral( if (ws && ws.readyState === WebSocket.OPEN) { ws.send(JSON.stringify({cmd: 'startLogging'})); console.log('Start logging command sent'); + // 로깅 시작 후 파일 목록 새로고침 (1초 후) + setTimeout(() => { + refreshFiles(); + }, 1000); } } @@ -1162,6 +1166,10 @@ const char index_html[] PROGMEM = R"rawliteral( if (ws && ws.readyState === WebSocket.OPEN) { ws.send(JSON.stringify({cmd: 'stopLogging'})); console.log('Stop logging command sent'); + // 로깅 종료 후 파일 목록 새로고침 (1초 후) + setTimeout(() => { + refreshFiles(); + }, 1000); } }