Hello Pablos
I tried a lot in the last days to make my code run based on your last sketch you uploaded and for me now I have the fileupload working fine with the below code but what I found out is that if I try to open a file from SD with command SD.open it does not work .
below I inserted my test sketch based on your last one you provided also I attached a screen shot of the serial monitor.
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <TinyWebServer.h>
File myFile;
// Don't forget to modify the IP to an available one on your home network
byte ip[] = { 192, 168, 1, 177 };
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
boolean file_handler(TinyWebServer& web_server);
boolean index_handler(TinyWebServer& web_server);
TinyWebServer::PathHandler handlers[] = {
{"/", TinyWebServer::GET, &index_handler },
{"/upload/" "", TinyWebServer::PUT, &TinyWebPutHandler::put_handler },
{"/" "", TinyWebServer::GET, &file_handler },
{NULL},
};
const char* headers[] = {"Content-Length",NULL};
TinyWebServer web = TinyWebServer(handlers, headers);
boolean has_filesystem = true;
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
void send_file_name(TinyWebServer& web_server, const char* filename) {
if (!filename) {
web_server.send_error_code(404);
web_server.write("Could not parse URL");
} else {
TinyWebServer::MimeType mime_type = TinyWebServer::get_mime_type_from_filename(filename);
web_server.send_error_code(200);
web_server.send_content_type(mime_type);
web_server.end_headers();
if (file.open(&root, filename, O_READ)) {
Serial.print("Read file "); Serial.println(filename);
web_server.send_file(file);
file.close();
} else {
web_server.write("Could not find file: ");
web_server.write(filename);
web_server.write("\n");
}
}
}
boolean file_handler(TinyWebServer& web_server) {
char* filename = TinyWebServer::get_file_from_path(web_server.get_path());
send_file_name(web_server, filename);
free(filename);
return true;
}
boolean index_handler(TinyWebServer& web_server) {
send_file_name(web_server, "INDEX.HTM");
return true;
}
void file_uploader_handler(TinyWebServer& web_server, TinyWebPutHandler::PutAction action, char* buffer, int size) {
static uint32_t start_time;
static uint32_t total_size;
switch (action) {
case TinyWebPutHandler::START:
start_time = millis();
total_size = 0;
if (!file.isOpen()) {
// File is not opened, create it. First obtain the desired name
// from the request path.
char* fname = web_server.get_file_from_path(web_server.get_path());
if (fname) {
Serial.print("Creating ");
Serial.println(fname);
file.open(&root, fname, O_CREAT | O_WRITE | O_TRUNC);
free(fname);
}
}
break;
case TinyWebPutHandler::WRITE:
if (file.isOpen()) {
file.write(buffer, size);
total_size += size;
}
break;
case TinyWebPutHandler:
file.sync();
Serial.print("Wrote ");
Serial.print (file.fileSize());
Serial.print(" bytes in ");
Serial.print(millis() - start_time);
Serial.print(" millis (received ");
Serial.print(total_size);
Serial.println(" bytes)");
file.close();
}
}
void setup() {
delay(1000);
Serial.begin(9600);
//SPI.begin();
//pinMode(4,OUTPUT);
//digitalWrite(4,1);
//pinMode(10,OUTPUT); //here cause problem at power reset initialize
//digitalWrite(10,1);
// initialize the SD card.
Serial.print("Setting up SD card...\n");
// pass over the speed and Chip select for the SD card
if (!card.init(SPI_HALF_SPEED, 4)) {
Serial.println("card failed");
has_filesystem = false;
}
// initialize a FAT volume.
if (!volume.init(&card)) {
Serial.println("vol.init failed!");
has_filesystem = false;
}
if (!root.openRoot(&volume)) {
Serial.print("openRoot failed");
has_filesystem = false;
}
if (has_filesystem) {
// Assign our function to `upload_handler_fn'.
TinyWebPutHandler::put_handler_fn = file_uploader_handler;
}
// Initialize the Ethernet.
Serial.println("Setting up the Ethernet card...");
Ethernet.begin(mac, ip);
//----------- debug connection LAN --------------------
Serial.println();
Serial.print("IP address Server: ");
Serial.println(Ethernet.localIP());
Serial.print("SubnetMask address: ");
Serial.println(Ethernet.subnetMask());
Serial.print("GatewayIP address: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DnsServer address: ");
Serial.println(Ethernet.dnsServerIP());
Serial.println();
//------------------------------------------------------
Serial.println("Web server starting...");
web.begin();
//pinMode(10,OUTPUT);
Serial.println("Ready to accept HTTP requests.");
//pinMode(10,1);
}
void loop() {
if (has_filesystem) {
web.process();
}
printDirectory();
delay(2000);
Test_file();
delay(2000);
}
void printDirectory() {
Serial.print("\nCard type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
}
void Test_file()
{
// Open the settings file for reading:
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}