Go Down

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

hunter360

#30
Jul 11, 2018, 08:43 am Last Edit: Jul 11, 2018, 08:52 am by hunter360
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

hunter360

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...



PieterP

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.

hunter360

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?


Dox213

// 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);
}

Go Up