Go Down

Topic: Send file from sd card to server (Read 5863 times) previous topic - next topic

hunter360

Hi i have esp8266 nodemcu, and sd card reader. I need send a file from sd card to server. How to do it?

i found code but it doesn't work.

here code.


Code: [Select]


    WiFiClient client;
if (client.connect("server ip", 80)) {
File file = SD.open("test.csv");
client.println(F("POST /http://example.com/...../examplefolder/ HTTP/1.1 "));

client.print(F("Host: "));
client.println("server ip");
client.println(F("Connection: close"));
client.print(F("Content-Length: "));
client.println(file.size());
client.println();
while (file.available()) {
client.write(file.read());
}

file.close();
client.stop();

}

hunter360

Hi i have esp8266 nodemcu, and sd card reader. I need send a file from sd card to server. How to do it?

i found code but it doesn't work.

here code.


Code: [Select]


WiFiClient client;
if (client.connect("server ip", 80)) {
File file = SD.open("test.csv");
client.println(F("POST /http://example.com/...../examplefolder/ HTTP/1.1 "));

client.print(F("Host: "));
client.println("server ip");
client.println(F("Connection: close"));
client.print(F("Content-Length: "));
client.println(file.size());
client.println();
while (file.available()) {
client.write(file.read());
}

file.close();
client.stop();

}

PieterP

#2
Jul 03, 2018, 03:09 pm Last Edit: Jul 03, 2018, 03:13 pm by PieterP
Define "it doesn't work".
Please post your complete code. Where did you find it? What have you tried already? Do you understand the code you're using, or did you just blindly copy-paste it?

Pieter

Edit: Do not cross-post!

PaulS

Code: [Select]
client.println(F("POST /http://example.com/...../examplefolder/ HTTP/1.1 "));

Rubbish. The protocol (http://) and server are NOT part of the POST command.

Quote
How to do it?
Just like that, if you want it to take forever.

Both the read() and write() methods are overloaded to use array, resulting in many fewer packets, and much faster sending.
The art of getting good answers lies in asking good questions.

Juraj

#4
Jul 03, 2018, 03:15 pm Last Edit: Jul 03, 2018, 03:38 pm by Juraj
esp8266 can do client.write(file)

did you see the WiFiClient example?

PieterP


Coding Badly


hunter360

Hi, again, i am so sorry for Cross-post, but i really need help about this.

I just don't know how to send from sd card file, to server.



and when i do this...
client.write(file) -->Error compiling for board NodeMCU 1.0 (ESP-12E Module).
 

If you have some examples, please tell me what code i need to send from sd card to server a file.

Juraj

#8
Jul 06, 2018, 09:29 am Last Edit: Jul 06, 2018, 09:36 am by Juraj
and when i do this...
client.write(file) -->Error compiling for board NodeMCU 1.0 (ESP-12E Module).
sorry, it works only with SPIFFS file, not with SD file


the file upload is done with POST request. do not forget to set the Content-length header

Code: [Select]
WiFiClient client;
  if (client.connect(yourHost, 80)) {
    File dataFile = SD.open("filename.csv");
    client.println(F("POST /your-url HTTP/1.1"));
    client.print(F("Host: "));
    client.println(yourHost);
    client.println(F("Connection: close"));
    client.print(F("Content-Length: "));
    client.println(dataFile.size());
    client.println();
    while (dataFile.available()) {
        client.write(dataFile.read());
    }
    dataFile.close();
    client.stop();
  }


warning, this will be slow. if it works, we can add buffering

hunter360

Code: [Select]

  #include <ESP8266WiFi.h>
      #include <ESP8266WiFiMulti.h>
      #include <ESP8266HTTPClient.h>
#include <SPI.h>
#include <SD.h>
 ESP8266WiFiMulti WiFiMulti;
File myFile;

void setup()
{

  Serial.begin(115200);

  WiFiMulti.addAP("ssid", "password");
        WiFiMulti.addAP("ssid2", "password2");
     Serial.println("Connecting ...");
     

  int i = 0;
  (WiFiMulti.run() != WL_CONNECTED) ; // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
 delay(500);
 
   
   
   
      Serial.println('\n');
  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());              // Tell us what network we're connected to
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());
  while (!Serial) {
    ;
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");


  myFile = SD.open("test.txt", FILE_WRITE);

 
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
   
    myFile.close();
    Serial.println("done.");
  } else {
   
    Serial.println("error opening test.txt");
  }

  //
}

void loop()
{

//in 00.00.000.00 is my servers ip, and in example is my url
  WiFiClient client;
  if (client.connect("00.00.000.00", 80)) {
    File myfile = SD.open("test.txt");
    client.println(F("POST /http://example.com/folder HTTP/1.1"));
    client.print(F("Host: "));
    client.println("00.00.000.00");
    client.println(F("Connection: close"));
    client.print(F("Content-Length: "));
    client.println(myfile.size());
    client.println();
    while (myfile.available()) {
        client.write(myfile.read());
    }
    myfile.close();
    client.stop();
  }


  WiFiMulti.addAP("ssid", "password");
        WiFiMulti.addAP("ssid2", "password");
     Serial.println("Connecting ...");
     

  int i = 0;
  (WiFiMulti.run() != WL_CONNECTED) ; // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
 delay(500);
 
   
   
   
      Serial.println('\n');
  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());              // Tell us what network we're connected to
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());
  while (!Serial) {
    ;
  }
}



code works, but nothing appears in server directory.



Juraj

#10
Jul 06, 2018, 11:14 am Last Edit: Jul 06, 2018, 11:30 am by Juraj
the host and port should not be in url after POST

Code: [Select]
client.println(F("POST /folder HTTP/1.1"));

hunter360

Still nothing with changes... client.println(F("POST /folder HTTP/1.1"));

but have question, is there other way to send to server? file from sd card?


PieterP

Have you tried flushing before stopping the client?


PieterP


Go Up