Is the code calling download and upload spiffs correct, I always experience that when uploading a spiffs file the ssid that always appears is not the one in the new spiffs file, for example my AP file name is aden but the spiffs save setting called gyro doesn't appear after my .bin spiffs upload and stay in the log his name is aden?
// Function to handle SPIFFS backup
void handleSpiffsBackup(AsyncWebServerRequest *request) {
// First pass: Count files and calculate total size
uint16_t fileCount = 0;
size_t totalDataSize = 0;
#ifdef ESP8266
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
fileCount++;
totalDataSize += dir.fileSize();
}
#else
File root = SPIFFS.open("/");
File file = root.openNextFile();
while (file) {
fileCount++;
totalDataSize += file.size();
file = root.openNextFile();
}
root.close();
#endif
// Calculate total backup size including header and file entries
size_t totalSize = sizeof(SPIFFSHeader) + totalDataSize;
// Allocate buffer with error checking
uint8_t* buffer = (uint8_t*)malloc(totalSize);
if (!buffer) {
request->send(500, "text/plain", "Failed to allocate memory for backup");
return;
}
// Initialize header
SPIFFSHeader header;
header.fileCount = fileCount;
header.totalSize = totalDataSize;
// Copy header to buffer
size_t currentPos = 0;
memcpy(buffer, &header, sizeof(SPIFFSHeader));
currentPos += sizeof(SPIFFSHeader);
// Second pass: Copy file data
bool backupSuccess = true;
#ifdef ESP8266
dir = SPIFFS.openDir("/");
while (dir.next() && backupSuccess) {
String fileName = dir.fileName();
File f = dir.openFile("r");
#else
root = SPIFFS.open("/");
file = root.openNextFile();
while (file && backupSuccess) {
String fileName = String(file.name());
File f = file;
#endif
if (!f) {
backupSuccess = false;
break;
}
// Write file path length and path
uint16_t pathLen = fileName.length();
memcpy(buffer + currentPos, &pathLen, sizeof(pathLen));
currentPos += sizeof(pathLen);
memcpy(buffer + currentPos, fileName.c_str(), pathLen);
currentPos += pathLen;
// Write file size
uint32_t fileSize = f.size();
memcpy(buffer + currentPos, &fileSize, sizeof(fileSize));
currentPos += sizeof(fileSize);
// Write file content
size_t remaining = fileSize;
while (remaining > 0 && backupSuccess) {
size_t chunk = min(remaining, (size_t)1024);
size_t bytesRead = f.read(buffer + currentPos, chunk);
if (bytesRead == 0) {
backupSuccess = false;
break;
}
currentPos += bytesRead;
remaining -= bytesRead;
}
f.close();
#ifndef ESP8266
file = root.openNextFile();
#endif
}
#ifndef ESP8266
root.close();
#endif
if (!backupSuccess) {
free(buffer);
request->send(500, "text/plain", "Backup creation failed");
return;
}
AsyncWebServerResponse *response = request->beginResponse_P(200,
"application/octet-stream", buffer, currentPos);
response->addHeader("Content-Disposition",
"attachment; filename=\"configuration_backup.bin\"");
request->send(response);
free(buffer);
}
// Function to handle SPIFFS restore
void handleSpiffsRestore(AsyncWebServerRequest *request, const String& filename,
size_t index, uint8_t *data, size_t len, bool final) {
static size_t bufferSize = 0;
static uint8_t* buffer = nullptr;
if (!index) {
// Clear previous state
if (buffer) {
free(buffer);
buffer = nullptr;
}
bufferSize = 0;
buffer = (uint8_t*)malloc(32768);
if (!buffer) {
request->send(500, "text/plain", "Failed to allocate restore buffer");
return;
}
Serial.println("Starting SPIFFS restore...");
}
if (buffer && len) {
// Copy new data to buffer
memcpy(buffer + bufferSize, data, len);
bufferSize += len;
}
if (final) {
Serial.println("SPIFFS restore completed");
// Unmount current SPIFFS
SPIFFS.end();
// Remount SPIFFS
if (mountSPIFFS()) {
Serial.println("SPIFFS remounted successfully");
// Load WiFi credentials from restored files
loadWiFiCredentials();
loadAPSettings();
// Print restored settings for verification
Serial.println("\nRestored Settings:");
Serial.println("WiFi SSID: " + routerSSID);
Serial.println("Expected MAC: " + String(expectedMAC));
Serial.println("AP SSID: " + String(apSSID));
// Update WiFi configuration if credentials exist
if (routerSSID.length() > 0) {
Serial.println("Attempting to reconnect with restored WiFi credentials...");
// Keep AP running while attempting to connect to WiFi
WiFi.mode(WIFI_AP_STA);
// Set hostname from restored AP SSID
#ifdef ESP8266
WiFi.hostname(apSSID);
#else
WiFi.setHostname(apSSID);
#endif
// Try to connect with restored credentials
if (connectToWiFi()) {
Serial.println("Successfully connected with restored WiFi credentials");
} else {
Serial.println("Failed to connect with restored WiFi credentials");
}
}
// Update AP configuration
if (strlen(apSSID) > 0 && strlen(apPassword) >= 8) {
Serial.println("Updating AP with restored settings...");
WiFi.softAP(apSSID, apPassword);
Serial.println("AP updated with restored settings");
}
} else {
Serial.println("SPIFFS remount failed");
}
if (buffer) {
free(buffer);
buffer = nullptr;
}
listFiles();
}
}
// Helper function to list all files in SPIFFS
void listFiles() {
Serial.println("\nSPIFFS files after restore:");
#ifdef ESP8266
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
String fileName = dir.fileName();
size_t fileSize = dir.fileSize();
Serial.printf("File: %s, Size: %d bytes\n", fileName.c_str(), fileSize);
}
#else
File root = SPIFFS.open("/");
File file = root.openNextFile();
while (file) {
Serial.printf("File: %s, Size: %d bytes\n", file.name(), file.size());
file = root.openNextFile();
}
root.close();
#endif
}