Go Down

Topic: Upload file to SD in Ethernet Shield (Read 528 times) previous topic - next topic

enrico_19

Hi there,
is there a way to upload files to sd in ethernet card connected to the router without remove sd, put it into pc, copy file, remove sd and put it in ethernet shield? :)
I searched on the net and find a method with curl but i don't know why it doesn't work... :(
Could you help me please?
Thanks

PaulS

Quote
but i don't know why it doesn't work.
Because curl doesn't work on most Arduinos.

Which Arduino do you have? Running what code?

https://playground.arduino.cc/Code/FTP
The art of getting good answers lies in asking good questions.

enrico_19

Because curl doesn't work on most Arduinos.

Which Arduino do you have? Running what code?

https://playground.arduino.cc/Code/FTP
I have an Arduino Mega not original... I've also an original Uno but I need more pins...

The code is this and I try to upload index.htm

Code: [Select]

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 100); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

File webFile;

void setup()
{
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.begin(9600);       // for debugging
   
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
    }
    Serial.println("SUCCESS - SD card initialized.");
    // check for index.htm file
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        return;  // can't find index file
    }
    Serial.println("SUCCESS - Found index.htm file.");
}

void loop()
{
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    // send web page
                    webFile = SD.open("index.htm");        // open web page file
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read()); // send web page to client
                        }
                        webFile.close();
                    }
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}

PaulS

Quote
The code is this and I try to upload index.htm
What part of that code reads the data from the client, and writes it to index.htm? I only see code to read from index.htm.
The art of getting good answers lies in asking good questions.

enrico_19

No...
Index.htm is in the SD card and I want to upload (not by code) a new index.htm to overwrite the old in the SD card.
The sketch of arduino is only the webserver that read index.htm...
I need something external to arduino to upload index.htm...

enrico_19

I tried with curl -0 -T index.htm http://192.168.1.100/upload/ but it doesn't work

PaulS

The Arduino would need to have some code that read the data to write to the index.htm file and would write the data to the file on SD card.

Quote
but it doesn't work
Of course not. There is nothing in your code that handles a GET request containing /upload any different from any other GET request. Your code performs the same action, and returns the same response, regardless of what the client asked for.

Did you look at the link I posted?
The art of getting good answers lies in asking good questions.

enrico_19

No, I'll look at it... I think that you only need to launch curl.exe from the dos prompt...

PaulS

Quote
I think that you only need to launch curl.exe from the dos prompt...
What do you suppose that curl actually does? Somehow, it must make a connection to the server (the Arduino) at the IP address supplied, and make a GET or POST or PUT request. Most likely, it makes a PUT request, which your code doesn't handle.
The art of getting good answers lies in asking good questions.



PaulS

But I need to upload local file to SD...
That code can be run to act as an uploader or downloader, depending on whether, or not, FTPWRITE is defined. If it is, then you will be able to FTP a file to the Arduino. If it is not, then you will only be able to FTP a file from the Arduino.

With as little memory as the Arduino has, it is hardly reasonable to expect it to act as a web server and an FTP server at the same time.
The art of getting good answers lies in asking good questions.

enrico_19

Ok thank you even if I didn't solve the problem... I decided to develop an android app instead of the web page and it's already working ;)
sd card will be for log and data...

Go Up