Post topics, source code that relate to the Arduino Platform

User avatar
By foxeast
#91075 Hi my friends
I recorded a wave file with Arduino mega 2560 (microphone is connected to Arduino mega 2560) and now I want to upload the recorded wave file is stored in my sd card to a web server with ESP12-F module which connected to Arduino mega 2560 with serial (UART) port, So I can't remove Arduino mega 2560 because I used of it for recording voices;

Now my problem is in uploading recorded wave file from my sd card (which connected to Arduino mega 2560) to a web server.
The problem is when I sending a file with a less size of 32 kb to the webserver, it's working fine and the file has been uploaded but when the size of my file is bigger than 32 kb, I can't send this file.
I think the problem is from the size of the Serial buffer and I don't know my problem for buffer size or another problem!
additional explanation : ( I used WampServer app as webserver and this library (WifiEspAT.h) for communication with ESP12-F module )

this is Link's from WIFIESPAT.h library (https://github.com/jandrassy/WiFiEspAT) and this is my sketch code
Code: Select all#include <WiFiEspAT.h>
#include <SD.h>
#include <SPI.h>
//#include <StreamLib.h>

File myFile;

String post_host = "192.168.1.103";
const int  post_port  = 80;
String  url = "/upload_file.php";
char server[] = "192.168.1.103";

// Emulate Serial1 on pins 6/7 if not present
#if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1)
//#include <SoftwareSerial.h>
//SoftwareSerial Serial1(6, 7); // RX, TX
#define AT_BAUD_RATE 9600
#else
#define AT_BAUD_RATE 115200
#endif

//const char* server = "arduino.cc";

WiFiClient client;


//format bytes
String formatBytes(unsigned int bytes) {
  if (bytes < 1024) {
    return String(bytes) + "B";
  } else if (bytes < (1024 * 1024)) {
    return String(bytes / 1024.0) + "KB";
  } else if (bytes < (1024 * 1024 * 1024)) {
    return String(bytes / 1024.0 / 1024.0) + "MB";
  }
}

void setup() {
  Serial.begin(115200);
  while (!Serial);

  Serial1.begin(AT_BAUD_RATE);
  WiFi.init(Serial1);

  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println();
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  // waiting for connection to Wifi network set with the SetupWiFiConnection sketch
  Serial.println("Waiting for connection to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print('.');
  }
  Serial.println();
  Serial.println("Connected to WiFi network.");

   //test connect to sd
  Serial.print("Initializing SD card...");
  if (!SD.begin(53))
  {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  //read file from SD
  //define file
  myFile = SD.open("data.wav", FILE_READ);


  String fileName = myFile.name();
  String fileSize = formatBytes(myFile.size());

  Serial.println();
  Serial.println("file exists");


  if (myFile)
  {
    Serial.println("test file:ok");
    // print content length and host
    Serial.print("contentLength : ");
    Serial.println(fileSize);
    Serial.print("connecting to ");
    Serial.println(post_host);

    // We now create a URI for the request
    Serial.println("Connected to server");
    Serial.print("Requesting URL: ");
    Serial.println(url);

    // Make a HTTP request and add HTTP headers
    //String boundary = "CustomizBoundarye----";
    //change with your content type
    String contentType = "audio/x-wav";
    String portString = String(post_port);
    String hostString = String(post_host);


    String requestHead = "--RandomNerdTutorials\r\nContent-Disposition: form-data; name=\"data\"; filename=\"data.wav\"\r\nContent-Type: audio/x-wav\r\n\r\n";

    String tail = "\r\n--RandomNerdTutorials--\r\n";


    int contentLength =  requestHead.length() + myFile.size() + tail.length();

    client.connect(server, 80);

    client.println("POST " + url + " HTTP/1.1");
    client.println("Host: " + post_host);
    client.println("Content-Length: " + String(contentLength, DEC));
    client.println("Content-Type: multipart/form-data; boundary=RandomNerdTutorials");
    client.println();
    client.print(requestHead );

    // send myFile:
    //this method is for upload data very fast
    //and only work in ESP software version after 2.3.0
    //if your version is lower than please update
    //esp software to last version or use bellow comment code
    client.write(myFile);
    // create file buffer
    const int bufSize = 2048;
    byte clientBuf[bufSize];
    int clientCount = 0;

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

    }

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


    //Serial.print(charBuf3);
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println("error opening test.WAV");
    Serial.println("Post Failure");
  }



  // Read all the lines of the reply from server and print them to Serial


  Serial.println("request sent");
  String responseHeaders = "";

  while (client.connected() ) {
    //      Serial.println("while client connected");
    String line = client.readStringUntil('\n');
    Serial.println(line);
    responseHeaders += line;
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }

  String line = client.readStringUntil('\n');

  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("closing connection");

  //close file
  myFile.close();

}
//}
void loop()
{}

and this is my php code :
Code: Select all<?PHP

    $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!";
   
  }
?>

and this is Serial monitor response for wave file bigger and less than 32kb ( screenshot's PNG is attached )


(( and also I tested this code with ESP8266 NODE MCU and I can send a bigger file of about 1MB to the same webserver (WampServer) ))

If possible helping me :cry:
thanks....
You do not have the required permissions to view the files attached to this post.