In SDFat. randomly I cannot read or open files, and sd.exists(path) would always be false after about 5 minutes of runtime. I use exFAT
#include <ESPAsyncWebServer.h>
#include <BufferedPrint.h>
#include <FreeStack.h>
#include <MinimumSerial.h>
#include <RingBuf.h>
#include <SdFat.h>
#include <SdFatConfig.h>
#include <sdios.h>
#include <WiFi.h>
// SD card chip select pin
const uint8_t SD_CHIP_SELECT = 5;
// Create SdFat instance
SdFat sd;
// Replace with your network credentials
const char* ssid = "ASUS_C8_2G_plus";
const char* password = "Seyitabdullah1440";
// Create a web server object that listens for HTTP requests on port 80
AsyncWebServer server(80);
// Helper function to check for the existence of an index file
String findIndexFile(const char* path) {
String indexFiles[] = {"/index.html", "/index.htm", "/index.txt"};
for (String indexFile : indexFiles) {
String fullPath = String(path) + indexFile;
if (sd.exists(fullPath.c_str())) {
return fullPath;
}
}
return "";
}
// Function to handle directory requests by generating an auto-index page
void handleDirectoryRequest(String path) {
SdFile dir;
// Attempt to open the directory
if (!dir.open(path.c_str(), O_READ)) {
server.send(500, "text/html", "<h1>500 - Error Opening Directory</h1>");
return;
}
String output = "<html><head><title>Index of " + path + "</title></head><body>";
output += "<h1>Index of " + path + "</h1><ul>";
SdFile entry;
char fileName[64];
// Iterate over the directory entries
while (entry.openNext(&dir, O_READ)) {
entry.getName(fileName, sizeof(fileName));
// Append a slash for directories
if (entry.isDir()) {
output += "<li><a href=\"" + path + String(fileName) + "/\">" + String(fileName) + "/</a></li>";
} else {
output += "<li><a href=\"" + path + String(fileName) + "\">" + String(fileName) + "</a></li>";
}
entry.close();
}
dir.close();
output += "</ul></body></html>";
// Send the generated page
server.send(200, "text/html", output);
}
// Updated logic in handleFileRequest()
void handleFileRequest() {
String path = server.uri();
if (path == "/software") {
handleSoftwareRequest();
return;
}
if (path.endsWith("/")) {
// If the path is a directory, check for an index file
String indexPath = findIndexFile(path.c_str());
if (!indexPath.isEmpty()) {
path = indexPath; // Use the found index file if it exists
} else {
handleDirectoryRequest(path); // Generate auto-index only if no index file is found
return;
}
}
// Check if the file exists on the SD card
if (!sd.exists(path.c_str())) {
server.send(404, "text/html", "<h1>404 - File Not Found</h1>");
return;
}
// Attempt to open the file for reading
SdFile file;
if (!file.open(path.c_str(), O_RDONLY)) {
server.send(500, "text/html", "<h1>500 - Error Opening File</h1>");
return;
}
// Determine content type and send the file
String contentType = "application/octet-stream";
if (path.endsWith(".html") || path.endsWith(".htm")) {
contentType = "text/html";
} else if (path.endsWith(".css")) {
contentType = "text/css";
} else if (path.endsWith(".png")) {
contentType = "image/png";
} else if (path.endsWith(".ico")) {
contentType = "image/x-icon";
} else if (path.endsWith(".jpg") || path.endsWith(".jpeg")) {
contentType = "image/jpeg";
} else if (path.endsWith(".gif")) {
contentType = "image/gif";
} else if (path.endsWith(".txt")) {
contentType = "text/plain";
} else if (path.endsWith(".exe")) {
contentType = "application/vnd.microsoft.portable-executable";
} else if (path.endsWith(".iso")) {
contentType = "application/x-iso9660-image";
} else if (path.endsWith(".zip")) {
contentType = "application/zip";
} else if (path.endsWith(".apk")) {
contentType = "application/vnd.android.package-archive";
}
server.setContentLength(file.fileSize());
server.send(200, contentType, "");
uint8_t buffer[1024];
size_t bytesRead;
while ((bytesRead = file.read(buffer, sizeof(buffer))) > 0) {
server.client().write(buffer, bytesRead);
}
file.close();
}
// Function to handle /software requests
void handleSoftwareRequest() {
server.send(200, "text/html", "<h1>Software page - currently empty</h1>");
}
// Function to handle not found requests
void notFound() {
server.send(404, "text/html", "<h1>404 - Not Found</h1>");
}
void setup() {
Serial.begin(115200);
// Initialize SD card
if (!sd.begin(SD_CHIP_SELECT, SD_SCK_MHZ(10))) {
Serial.println("SD card initialization failed!");
return;
}
Serial.println("SD card initialized.");
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.println("Connecting to Wi-Fi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
// Print IP address when connected
Serial.println("\nConnected to Wi-Fi");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Handle all file requests
server.onNotFound(handleFileRequest);
// Start the server
server.begin();
Serial.println("HTTP server started");
}
void loop() {
// Handle incoming client requests
server.handleClient();
}