Hola gente, cómo estáis? Espero que podáis ayudar a guiarme un poco porque ya he visto muchos foros y muchos posts pero no me aclaro mucho la verdad, no soy muy experimentado pero me encanta aprender y disfruto con ello!
Estoy usando un arduino Mega 2560 con un Ethernet Shield W5500, he creado un log.txt en la micro SD en el que guardo información de todos los movimientos que se van registrando en el programa.
Hasta aquí, todo bien. Cojo la tarjeta, la pongo en el ordenador y puedo ver todo el registro.
Mi problema es que ahora después de probar que funcione todo, necesito poder descargar el archivo de la micro SD a un PC o un móvil conectándose mediante el router, con la IP.
Tanto me da si es por html o por ftp aunque por lo que he estado viendo, por html no se comploca tanto.
Entiendo que lo que quieres es crear una página HTML y de algun modo disponer de un boton que permita que un dispositivo sea PC o celular puedan descargar el archivo citado.
Es así?
A ver si esto te sirve o te aproxima a tu idea : web server to read file from SD
Es muy similar a lo que quiers hacer.
Acabo fe ver el post pero no dicen de descargar el fichero, solo están diciendo de leer el archivo y mostrarlo en la pantalla. Durante estos días, he conseguido que me descargue el archivo, pero se me descarga un archivo en blanco, sin nada escrito dentro no se porque me descarga un archivo de texto vacío, os dejo el código:
#include <SD.h>
#include <SPI.h>
#include <Ethernet2.h>
#define REQ_BUF_SZ 20
byte mac = {0x90, 0xA2, 0xDA, 0x10, 0xA5, 0xC0};
IPAddress ip(192, 168, 1, 90);
byte gateway ={192, 168, 1, 1};
byte subnet ={255, 255, 255, 0};
EthernetServer server(80);
File myFile;
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0;
String readString;
//////////////////////
void setup()
{
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
Serial.begin(9600);
// initialize SD card
Serial.println(“Initializing SD card…”);
if (!SD.begin(4))
{
Serial.println(“ERROR - SD card initialization failed!”);
return;
}
Serial.println(“SUCCESS - SD card initialized.”);
if (!SD.exists(“TEST.txt”))
{
Serial.println(“ERROR - Can’t find index.htm file!”);
return; // can’t find index file
}
Serial.println(“SUCCESS - Found TEST.txt file.”);
Ethernet.begin(mac, ip, gateway, subnet); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.println(“Ready”);
}
void loop()
{
// Create a client connection
EthernetClient client = server.available();
if (client)
{
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100)
{
//store characters to string
readString += c;
Serial.print(c);
}
//if HTTP request has ended
if (c == ‘\n’ && currentLineIsBlank)
{
// open requested web page file
client.println();
if (HTTP_req, “GET /datalog.txt”)
{
client.println(“HTTP/1.1 200 OK”); //send new page
client.println(“Content-Disposition: attachment; filename=datalog.txt”);
client.println(“Connection: close”);
client.println();
Serial.println(readString); //print to serial monitor for debuging
if (myFile)
{
byte clientBuf[64];
int clientCount = 0;
while(myFile.available())
{
clientBuf[clientCount] = myFile.read();
clientCount++;
if(clientCount > 63)
{
client.write(clientBuf,64);
clientCount = 0;
}
}
//final <64 byte cleanup packet
if(clientCount > 0) client.write(clientBuf,clientCount);
// close the file:
myFile.close();
}
delay(1);
//stopping client
client.stop();
readString="";
}
}
}
}
}
}