I have achieved the following :
I have a Teensey-Ultimate GPS Sheild-Esp8266 Setup. I am trying to send a file of 1Mb which is stored in the SD-card to a local python Server using TCP. I am able to send the file but the problem is that it is taking 4 Minutes to send the 1Mb file, whereas the same file when i send from a Python-Client script it takes 5 Seconds.
What is the reason for this slow transfer of data ? Is there a way to improve the speed.
Following is my Arduino and Python sketch :
Arduino Sketch:
#include <SoftwareSerial.h>
//------------------------------------------------------
#include <SPI.h>
//#include <SD.h>
#define TIMEOUT 5000 // mS
#define LED 13
#include <SdFat.h>
SdFat sd;
SdFile myFile;
uint8_t buf[1024];
const int chipSelect = SS;
unsigned long millisStartTime = 0;
unsigned long millisStopTime = 0;
unsigned long difference = 0;
//---------------------------------------------------------------------------
#define DEBUG true
void setup()
{
Serial.begin(115200);
Serial1.begin(2000000);
Serial1.attachCts(20);
delay(1000);
Serial.println("Initializing SD card...");
if (!sd.begin(chipSelect, SPI_FULL_SPEED)) sd.initErrorHalt();
delay(1000);
Serial.println("Card Initialized");
//AT+UART_DEF=2000000,8,1,0,1....use this command to set BAUD RATE
sendData("AT\r\n",2000,DEBUG);
sendData("AT+CWJAP=\"Connasys_2\",\"0987654321\"\r\n",5000,DEBUG);
sendData("AT+CIFSR\r\n",2000,DEBUG); // get ip address
sendData("AT+CIPMUX=0\r\n",1000,DEBUG); // configure for multiple connections
sendData("AT+CIPMODE=1\r\n",1000,DEBUG);
sendData2("AT+CIPSTART=\"TCP\",\"192.168.0.119\",80",2000);
//----------------FIND IMPORT STRING--------------------
//Serial.print("ENTER STRING:");
String IncomingString="";
boolean StringReady = false;
delay(2000);
StringReady= true;
if (StringReady){
//----------------READ FILE IF FOUND IMPORT STRING--------------------
if (!myFile.open("DATA3.TXT", O_READ))
{
sd.errorHalt("opening test.txt for read failed");
}
millisStartTime = millis();
sendData2("\r\n",1000);
sendData2("AT+CIPSEND\r\n",2000);
//Serial1.println("hello world");
while(myFile.available())
{
Serial1.print(myFile.read());
}
sendData2("\r\n",500);
sendData2("+++",1000);
millisStopTime = millis();
difference = millisStopTime - millisStartTime;
Serial.println("File Sending Complete");
Serial.println("Total Time taken is: ");
Serial.println(difference);
sendData("AT+CIPCLOSE=1\r\n",1000,DEBUG);
myFile.close();
}
}
void loop()
{
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
Serial1.print(command);
long int time = millis();
while( (time+timeout) > millis())
{
while(Serial1.available())
{
char c = Serial1.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
String sendData2(String command, const int timeout)
{
String response = "";
Serial1.print(command);
long int time = millis();
while( (time+timeout) > millis())
{
while(Serial1.available())
{
char c = Serial1.read(); // read the next character.
response+=c;
}
}
Serial.print(response);
return response;
}
Python Sketch:
import socket # Import socket module
import time
s = socket.socket() # Create a socket object
host = '192.168.0.119' # Get local machine name
port = 80 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
f = open('Got_File.txt','wb')
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print ('Got connection from', addr)
start_time = time.time()
print ("Receiving Data from Client...")
l = c.recv(1024)
while (l):
print ("Receiving...")
f.write(l)
#print (l)
l = c.recv(1024)
f.close()
#s.shutdown(socket.SHUT_WR)
print ("Done Receiving")
print("--- %s seconds ---" % (time.time() - start_time))
#c.send('Thank you for connecting')
c.close()
---------------------------------------------------------------------------------------------------------------------------------------
Any Help would be appreciated. Thank You