使用Nodemcu ESP8266从SD卡上传文件/图像到服务器?

时间:2019-07-30 01:47:15

标签: php c++ upload esp8266

我想在服务器中上传文件或图像(为了进行测试,我使用免费的托管服务器000webhost)。文件或图像在SD卡中。然后在硬件中,我有ESP8266 NodeMCU和SD卡模块来读取文件,然后使用WiFi Internet将其发送到服务器。

我已经尝试过使用POSTMAN,并且它运行良好(所以我认为PHP代码没有问题)。但是,当我尝试使用ESP8266 NodeMCU和SD卡模块上传文件时,它不起作用。文件未上传,但没有错误。

这是我的PHP代码:

<?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;
?>

这是我的ESP8266 NodeMCU代码:

#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(){
}

我的串行监视器的结果是:

WiFi 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

0 个答案:

没有答案