Arduino Forum

Using Arduino => Networking, Protocols, and Devices => Topic started by: hunter360 on Jul 03, 2018, 03:03 pm

Title: Send file from sd card to server
Post by: hunter360 on Jul 03, 2018, 03:03 pm
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();

}
Title: Send file from sd card to server esp8266
Post by: hunter360 on Jul 03, 2018, 03:08 pm
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();

}
Title: Re: Send file from sd card to server
Post by: PieterP on Jul 03, 2018, 03:09 pm
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!
Title: Re: Send file from sd card to server esp8266
Post by: PaulS on Jul 03, 2018, 03:12 pm
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.
Title: Re: Send file from sd card to server esp8266
Post by: Juraj on Jul 03, 2018, 03:15 pm
esp8266 can do client.write(file)

did you see the WiFiClient example?
Title: Re: Send file from sd card to server esp8266
Post by: PieterP on Jul 03, 2018, 03:17 pm
Cross-posted @ http://forum.arduino.cc/index.php?topic=556337.msg3793750#msg3793750 (http://forum.arduino.cc/index.php?topic=556337.msg3793750#msg3793750)

@OP, don't do that, you're wasting everybody's time.
Title: Re: Send file from sd card to server
Post by: Coding Badly on Jul 03, 2018, 03:59 pm

Threads merged.

Title: Re: Send file from sd card to server
Post by: hunter360 on Jul 06, 2018, 08:15 am
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.
Title: Re: Send file from sd card to server
Post by: Juraj on Jul 06, 2018, 09:29 am
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
Title: Re: Send file from sd card to server
Post by: hunter360 on Jul 06, 2018, 10:48 am
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.


Title: Re: Send file from sd card to server
Post by: Juraj on Jul 06, 2018, 11:14 am
the host and port should not be in url after POST

Code: [Select]
client.println(F("POST /folder HTTP/1.1"));
Title: Re: Send file from sd card to server
Post by: hunter360 on Jul 06, 2018, 12:21 pm
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?

Title: Re: Send file from sd card to server
Post by: PieterP on Jul 06, 2018, 12:23 pm
Have you tried flushing before stopping the client?
Title: Re: Send file from sd card to server
Post by: hunter360 on Jul 06, 2018, 12:26 pm
yes
Title: Re: Send file from sd card to server
Post by: PieterP on Jul 06, 2018, 12:34 pm
Where?
Title: Re: Send file from sd card to server
Post by: Juraj on Jul 06, 2018, 01:25 pm
how large is the file? test with a small file.
Title: Re: Send file from sd card to server
Post by: hunter360 on Jul 06, 2018, 01:27 pm
 it it's small file.  Only create test 1 2 3 , and nothing else.
Title: Re: Send file from sd card to server
Post by: Juraj on Jul 06, 2018, 02:05 pm
use IPAddress in connect and in Host header

Code: [Select]

IPAddress serverIP(123, 456, 78, 9);

if (client.connect(serverIP, 80)) {
...
client.print(F("Host: "));
client.println(serverIP);


Title: Re: Send file from sd card to server
Post by: hunter360 on Jul 06, 2018, 02:53 pm
nop, didn't work.  :(
Title: Re: Send file from sd card to server
Post by: PieterP on Jul 06, 2018, 02:57 pm
Time to open up Wireshark then.
Title: Re: Send file from sd card to server
Post by: Juraj on Jul 06, 2018, 06:16 pm
copy to Serial the response received by the client

Code: [Select]

  while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      Serial.write(c);
    }
  }
Title: Re: Send file from sd card to server
Post by: hunter360 on Jul 10, 2018, 01:02 pm


Code: [Select]
while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      Serial.write(c);
    }
  }

Nothing happened....
Title: Re: Send file from sd card to server
Post by: PaulS on Jul 10, 2018, 02:00 pm
Quote
Nothing happened....
Of course not. Your snippet won't even compile. If you want help, you'll stop posting snippets, and start posting ALL of your code.
Title: Re: Send file from sd card to server
Post by: hunter360 on Jul 10, 2018, 02:09 pm
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()
{


IPAddress serverIP(00, 00, 000, 00);

WiFiClient client;
  if (client.connect(serverIP, 80)) {
   
while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      Serial.write(c);
    }
  }
 
    File myFile = SD.open("test.txt");
    client.println(F("POST /folder HTTP/1.1"));
    client.print(F("Host: "));
    client.println(serverIP);
    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", "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) {
    ;
  }
}
Title: Re: Send file from sd card to server
Post by: Juraj on Jul 10, 2018, 02:23 pm
you should read the response after you send the request
Title: Re: Send file from sd card to server
Post by: hunter360 on Jul 10, 2018, 02:48 pm
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()
{


IPAddress serverIP(00, 00, 000, 00);

WiFiClient client;
  if (client.connect(serverIP, 80)) {
   

 
    File myFile = SD.open("test.txt");
    client.println(F("POST /folder HTTP/1.1"));
    client.print(F("Host: "));
    client.println(serverIP);
    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();

while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      Serial.write(c);
    }
  }
    client.stop();
  }
 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) {
    ;
  }
}





still nothing
Title: Re: Send file from sd card to server
Post by: PaulS on Jul 10, 2018, 03:48 pm
That code looks like sh*t. Did you get a drunken monkey to operate the tab key for you?

Use Tools + Auto Format BEFORE you post crappy looking code like that again. Unless you don't want help. In which case, you are free to post crappy looking code.
Title: Re: Send file from sd card to server
Post by: Juraj on Jul 10, 2018, 04:20 pm
try it while connected to your wifi network. what is that wifimulti stuff?
Title: Re: Send file from sd card to server
Post by: hunter360 on Jul 11, 2018, 08:17 am

it was connected to wifi
wifimulti allowed me to set more wifi connections.


Title: Re: Send file from sd card to server
Post by: Juraj on Jul 11, 2018, 08:35 am
it was connected to wifi
wifimulti allowed me to set more wifi connections.
but was it the same network where the server is?
Title: Re: Send file from sd card to server
Post by: hunter360 on Jul 11, 2018, 08:43 am
but was it the same network where the server is?
No, server is on "Plesk". Godaddy. maybe thas why.

i am sorry to have confused you
Title: Re: Send file from sd card to server
Post by: hunter360 on Jul 11, 2018, 01:15 pm
Thank you everyone who tried to help, in the end i could not send csv file from sd card with esp8266, to server on plesk(goddady).

If some one have suggestions, i'll be here all the time...


Title: Re: Send file from sd card to server
Post by: PieterP on Jul 11, 2018, 01:27 pm
Unless there is only one virtual server on that server, you cannot use the IP address in the host header. I have no idea what Plesk is, but it probably has multiple servers running on the same address.
Title: Re: Send file from sd card to server
Post by: hunter360 on Aug 02, 2018, 10:17 am
Hi little update.

Have php file on server,

Code: [Select]
<?PHP
  if(!empty($_FILES['mans']))
  {
    $path = "upload/";
    $path = $path . basename( $_FILES['mans']['name']);
    if(move_uploaded_file($_FILES['mans']['tmp_name'], $path)) {
      echo "The file ".  basename( $_FILES['mans']['name']).
      " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
  }
?>


allows me to upload file to server. With postman works fine.

"The file test.txt has been uploaded"


now i need upload automatically from sd card file

here is code
Code: [Select]

      IPAddress serverIP(here ip address);

      WiFiClient client;
      if (client.connect(serverIP, 80)) {
   


   
      File file = SD.open(filename);
   

   
      client.println(F("POST http://example.com/upload.php HTTP/1.1"));
      client.print(F("Host: "));
      client.println(serverIP);
      client.println(F("Connection: close"));
      client.println("Content-Type: multipart/form-data; boundary=----
      WebKitFormBoundaryjg2qVIUS8teOAbN3");
 
      client.print("Content-Length: ");
      client.println(file.size()); 
      client.println();
      client.println("------WebKitFormBoundaryjg2qVIUS8teOAbN3");
      client.println("Content-Disposition: form-data; name=\"mans\"; filename=\"filename\"" );
      client.println("Content-Type: application/octet-stream");

      client.println();

 
      while (file.available()) {
     
         client.write(file.read());
       
    }
      file.close();

    while (client.connected()) {
         if (client.available()) {
         char c = client.read();
         Serial.write(c);
    }
  }
 
    client.stop();
 
  }



Serial monitor
Code: [Select]



HTTP/1.1 200 OK
Content-Type: text/html
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET

Date: Thu, 02 Aug 2018 08:15:36 GMT
Connection: close
Content-Length: 56

There was an error uploading the file, please try again!


If you know what to do to take file from sd card and upload, please tell me what to change, or do?

Title: Re: Send file from sd card to server
Post by: Dox213 on Sep 20, 2019, 10:32 pm
// i am using spiffs in here recode it sd !!! tryout
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <FS.h>

#define FILE "/atc.mp3" /change it
#define TYPE "mp3" /change it
#define URL "http://192.168.1.2/" /change it
#define SSID "wifissid" //change it
#define PSK "password" //change it
#define DBG Serial

void setup() {
  DBG.begin(115200);
  DBG.println("starting...");
  WiFi.begin(SSID, PSK);
  while (!WiFi.isConnected()) {
    DBG.println(".");
    delay(1000);
  }

  if (!SPIFFS.begin())
    DBG.println("failed to start SPIFFS!");
}

void loop() {
  static bool cloud_send = false;
  if (!cloud_send) {
    cloud_send = true;

    auto f = SPIFFS.open(FILE, "r");
    const auto f_size = f.size();

    WiFiClient client;
    HTTPClient http;
    http.setUserAgent(String("ESP ") + String(ESP.getChipId()) + " " + FILE + " " + TYPE);
    http.addHeader("Content-Type", "text/plain");
    http.addHeader("Content-Length", String(f_size, DEC));
    if (!http.begin(client, URL)) {
      DBG.println("\nfailed to begin http\n");
      return;
    }
   
    int http_code;
    const auto A = millis();
    http_code = http.sendRequest("POST", &f, f_size); //boom the misery sloved
    DBG.printf("\n time-keep: %lus", (millis() - A) / 1000);
    http.writeToStream(&DBG);
    http.end();
    f.close();
  }
  delay(10000);
}