Chat freely about anything...

User avatar
By Rizal Maulana
#83271 I want to upload a file or image in my server (for testing, i use free hosting server 000webhost). The file or image is in SD Card. Then in the hardware, i have ESP8266 NodeMCU and SD Card module to read the files and send it to server using WiFi Internet.

I have tried using POSTMAN and it works perfectly (so i think, there is no problem with PHP code). But when i try to upload files with ESP8266 NodeMCU and SD Card module, it is not working. The files is not uploaded, but there is not error.

This is my PHP code:
Code: Select all<?PHP

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


and This is my ESP8266 NodeMCU code:
Code: Select all#include <ESP8266WiFi.h>
#include <SPI.h>
#include <SD.h>

File myFile;

const char* ssid     = "zzz";
const char* password = "xxx";
String host = "xxxx.000webhostapp.com";

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

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

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

  Serial.print("connecting to ");
  Serial.println(host);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  File myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test file:ok");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  int filesize = myFile.size();
  Serial.print("filesize=");
  Serial.println(filesize);
  String fileName = myFile.name();
  String fileSize = String(myFile.size());

  Serial.println("reading file");

  String url = "/upload.php";

  String boundary = "WebKitFormBoundary7MA4YWxkTrZu0gW";
  String contentType = "text/plain";
  // post header
  String postHeader = "POST " + url + " HTTP/1.1\r\n";
  postHeader += "Host: " + host + " \r\n";
  postHeader += "Content-Type: multipart/form-data; boundary=----" + boundary + "\r\n";
  String keyHeader = "------" + boundary + "\r\n";
  keyHeader += "Content-Disposition: form-data; name=\"data\"\r\n\r\n";
  String requestHead = "------" + boundary + "\r\n";
  requestHead += "Content-Disposition: form-data; name=\"data\"; filename=\"" + fileName + "\"\r\n";
  requestHead += "Content-Type: " + contentType + "\r\n\r\n";

  // post tail
  String tail = "\r\n------" + boundary + "--\r\n\r\n";

  // content length
  int contentLength = keyHeader.length() + requestHead.length() + myFile.size() + tail.length();
  postHeader += "Content-Length: " + String(contentLength, DEC) + "\n\n";

  // send post header
  char charBuf0[postHeader.length() + 1];
  postHeader.toCharArray(charBuf0, postHeader.length() + 1);
  client.write(charBuf0);
  Serial.print("send post header=");
  Serial.println(charBuf0);

  // send key header
  char charBufKey[keyHeader.length() + 1];
  keyHeader.toCharArray(charBufKey, keyHeader.length() + 1);
  client.write(charBufKey);
  Serial.print("send key header=");
  Serial.println(charBufKey);

  // send request buffer
  char charBuf1[requestHead.length() + 1];
  requestHead.toCharArray(charBuf1, requestHead.length() + 1);
  client.write(charBuf1);
  Serial.print("send request buffer=");
  Serial.println(charBuf1);

  // create buffer
  const int bufSize = 2048;
  byte clientBuf[bufSize];
  int clientCount = 0;

  // read myFile until there's nothing else in it:
  while (myFile.available())
  {
    clientBuf[clientCount] = myFile.read();
    clientCount++;
    if (clientCount > (bufSize - 1))
    {
      client.write((const uint8_t *)clientBuf, bufSize);
      clientCount = 0;
    }
  }
  if (clientCount > 0)
  {
    client.write((const uint8_t *)clientBuf, clientCount);
    Serial.println("Sent LAST buffer");
  }

  // send tail
  char charBuf3[tail.length() + 1];
  tail.toCharArray(charBuf3, tail.length() + 1);
  client.write(charBuf3);
  Serial.print(charBuf3);

  Serial.println("end_request");


  String lastline;
  while (client.connected())
  {
    while (client.available())
    {
      String line = client.readStringUntil('\r');
      lastline = line;
      Serial.print(line);
      if (line.indexOf("{") > 0)
      {
        client.stop();
      }
    }
  }
  Serial.println(lastline);
  myFile.close();
  Serial.println("closing connection");
}

void loop(){
}


And the result from my serial monitor is:

Code: Select allWiFi connected
IP address:
192.168.43.169
Initializing SD card...initialization done.
connecting to serveriot.000webhostapp.com
test file:ok
filesize=144
reading file
send post header=POST /upload.php HTTP/1.1
Host: serveriot.000webhostapp.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Length: 386


send key header=------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="data"


send request buffer=------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="data"; filename="test.txt"

Sent LAST buffer

------WebKitFormBoundary7MA4YWxkTrZu0gW--

end_request
HTTP/1.1 200 OK
Date: Tue, 30 Jul 2019 01:32:19 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Request-ID: 5d9decf0575d7243f6fce1135504e3db

1


0



closing connection


Thanks