Hello, this is my first post on this forum even if i used a lot this tool.
I'm working on an arduino UNO with an ethernet shield, and i have to manage files from SD card by using a web page.
Functions i have to implement are the following :
- explore existing files of the SD card
- import files from a computer to SD card
- delete files.
The first step is working, i can print repertories and each files on an html page and download them.
My problem is that i really don't no how to delete a choosen file and how to import file from the computer (text file for example). After a lot of research with a large documentation on this subject, i don't find help with my problem.
This is my actual code, i hope someone could bring me a little help !
#include <SPI.h>
#include <SD.h>
#include <Ethernet.h> // Using WIZ5500 Ethernet chip so Adafruit Ethernet2 library is required
/************ ETHERNET STUFF ************/
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // change if necessary
byte ip[] = { 10, 8, 0, 2 }; // change if necessary
EthernetServer server(80);
/************ SDCARD STUFF ************/
#define SDCARD_CS 4
File root;
#define WIZ_CS 10
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void error_P(const char* str) {
Serial.print(F("error: "));
Serial.println(str);
while (1);
}
void setup() {
Serial.begin(115200);
while (!Serial); // For 32u4 based microcontrollers like 32u4 Adalogger Feather
Serial.print(F("Free RAM: ")); Serial.println(FreeRam());
if (!SD.begin(SDCARD_CS)) { //verification que la carte est bien detéctée
error("card.init failed!");
}
printDirectory(root, 0);
root = SD.open("/");
// listing des dossiers + fichiers avec la fonction printDirectory
Serial.println(F("Fichiers trouves dans le repertoire:"));
printDirectory(root, 0);
Serial.println();
Serial.println(F("Done"));
// lancement du serveur
Serial.println(F("Initialisation..."));
Ethernet.init(WIZ_CS);
// on donne le temps a l'initialisation
delay(1000);
// lancement de la connexion ethernet avec les adresses mac et ip specifiees
//adresse mac donnee par nous et ip est une adresse libre dans le reseau uha filaire
Ethernet.begin(mac, ip);
// affichage de l'ip utilisee sur le port serie pour verification
Serial.print(F("L'adresse IP est : "));
Serial.println(Ethernet.localIP());
server.begin(); //lancement serveur
}
void ListFiles(EthernetClient client, uint8_t flags, File dir) { //fonction de listing des fichiers pour affichage html
client.println("<ul>");
while (true) { //on prefere utiliser une boucle infinie
File entry = dir.openNextFile(); //parcours le repertoire en lisant successivement les fichiers
// fin de chaine : plus de fichiers suivants
if (! entry) {
break;
}
// identation et listing html
client.print("<li><a href=\"");
client.print(entry.name()); //on prend le nom du fichier entry declare precedemment
if (entry.isDirectory()) { //si c'est un repertoire, affichage special
client.println("/");
}
client.print("\">");
// si on clique sur un dossier, il affiche de nouveau le contenu (on change de repertoire en quelque sorte)
//le nouveau repertoire est le repertoire fils et on re test la presence de contenu
client.print(entry.name());
if (entry.isDirectory()) {
client.println("/");
}
client.print("</a>");
client.println("<input type=button>cliquez pour supprimer</>");
//client.println("<input type=button OnClick=SD.remove(entry.name())> cliquez pour supprimer</>");
client.println("</li>");
entry.close(); //fermeture du fichier entry car traitement effectue
}
client.println("</ul>");
}
// How big our line buffer should be. 100 is plenty!
#define BUFSIZ 100
void loop()
{
char clientline[BUFSIZ];
char name[17];
int index = 0;
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
// reset the input buffer
index = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// If it isn't a new line, add the character to the buffer
if (c != '\n' && c != '\r') {
clientline[index] = c;
index++;
// are we too big for the buffer? start tossing out data
if (index >= BUFSIZ)
index = BUFSIZ - 1;
// continue to read more data!
continue;
}
// got a \n or \r new line, which means the string is done
clientline[index] = 0;
// Print it out for debugging
Serial.println(clientline);
// Look for substring such as a request to get the file
if (strstr(clientline, "GET /") != 0) {
// this time no space after the /, so a sub-file!
char *filename;
filename = clientline + 5; // look after the "GET /" (5 chars) *******
// a little trick, look for the " HTTP/1.1" string and
// turn the first character of the substring into a 0 to clear it out.
(strstr(clientline, " HTTP"))[0] = 0;
if (filename[strlen(filename) - 1] == '/') { // Trim a directory filename
filename[strlen(filename) - 1] = 0; // as Open throws error with trailing /
}
Serial.print(F("Web request for: ")); Serial.println(filename); // print the file we want
File file = SD.open(filename, O_READ);
if ( file == 0 ) { // Opening the file with return code of 0 is an error in SDFile.open
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
client.println("<h2>File Not Found!</h2>");
client.println("
<h3>Couldn't open the File!</h3>");
break;
}
Serial.println("File Opened!");
client.println("HTTP/1.1 200 OK");
if (file.isDirectory()) {
Serial.println("is a directory");
//file.close();
client.println("Content-Type: text/html");
client.println();
client.print("<h2>Files in /");
client.print(filename);
client.println(":</h2>");
ListFiles(client, LS_SIZE, file);
file.close();
} else { // Any non-directory clicked, server will send file to client for download
client.println("Content-Type: application/octet-stream");
client.println();
char file_buffer[16];
int avail;
while (avail = file.available()) {
int to_read = min(avail, 16);
if (to_read != file.read(file_buffer, to_read)) {
break;
}
// uncomment the serial to debug (slow!)
//Serial.write((char)c);
client.write(file_buffer, to_read);
}
file.close();
}
} else {
// everything else is a 404
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
client.println("<h2>File Not Found!</h2>");
}
break;
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}