Hello to all ![]()
I send a file of about 200 KB to SD connected to the Arduino MEGA 2560 using the ESP 8266 module (the modules are connected via a serial port) but my problem is in the transfer speed because My transfer speed is about 28 kbps and it takes about 11 seconds for my file to upload to the server.
Do you think there is a way to upload my file to the server faster and in less time?
Please help me if there is a way (I have no problem paying)
Additional explanation (I think my problem is with the serial port, which may have a transfer speed limit because I tested the same code with NodeMcu and it sent at a speed of 460 kbps)
My Sketch :
#include <SD.h>
#include <SPI.h>
#include <WiFiEspAT.h>
#include <StreamLib.h>
#if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1)
#define AT_BAUD_RATE 9600
#else
#define AT_BAUD_RATE 1000000
#endif
File myFile;
#define MTU_Size 2*1460
String post_host = "192.168.1.101";
const int post_port = 80;
String url = "/errtest.php?azyx=1&d=1";
char server[ ] = "192.168.1.101";
//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";
}
}
WiFiClient client;
void setup() {
Serial.begin(230400);
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...");
SD.begin(190000000, 53);
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);
//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";
unsigned long 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[MTU_Size];
int clientCount = 0;
while (myFile.available())
{
clientBuf [clientCount] = myFile.read ();
clientCount++;
if (clientCount > (MTU_Size - 1))
{
client.write((const uint8_t *)clientBuf, MTU_Size);
clientCount = 0;
}
}
if (clientCount > 0)
{
client.write((const uint8_t *)clientBuf, clientCount);
}
client.print(tail);
}
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()
{}