Here is the code to see the problem i hope, it requires an SD module connected, just fill this part with the FTP information:
#define FTP_USER "user"
#define FTP_PASS "pass"
#define FTP_ADDRESS "148.251.48.69"
#define FTP_DATA_ADDRESS "148,251,48,69," //this is the ip address for the data connection (given here "227 Entering Passive Mode (148,251,48,69,181,150)")
#define FILE_TO_UPLOAD "DSCF0003.jpg"
// **************************************
then when flashed just send any char throught serial and will start to connect, transfer the file and disconnect.
// This #include statement was automatically added by the Spark IDE.
#include "sd-card-library/sd-card-library.h"
#include "application.h"
#define BUF_SIZE 1000
// *** JUST FILL IN THIS TO TEST ***
#define FTP_USER "user"
#define FTP_PASS "pass"
#define FTP_ADDRESS "148.251.48.69"
#define FTP_DATA_ADDRESS "148,251,48,69," //this is the ip address for the data connection (given here "227 Entering Passive Mode (148,251,48,69,181,150)")
#define FILE_TO_UPLOAD "DSCF0003.jpg"
// **************************************
// SOFTWARE SPI pin configuration - modify as required
// The default pins are the same as HARDWARE SPI
const uint8_t chipSelect = A2; // Also used for HARDWARE SPI setup
const uint8_t mosiPin = A5;
const uint8_t misoPin = A4;
const uint8_t clockPin = A3;
TCPClient ClientOperation,ClientData;
File myFile;
String messageToSend,messageReceived;
char LineBreaker[2];
int step=-1;
byte serverAddress[4];
int StartingPosition;
int EndingPosition;
int value;
String preString=FTP_DATA_ADDRESS;
String RawValue;
int length;
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
int totalread=0;
int fileSize=0;
void error(const char* str)
{
Serial.print("error: ");
Serial.println(str);
if (card.errorCode()) {
Serial.print("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1) {
SPARK_WLAN_Loop();
};
}
void InitSD(){
// initialize the SD card at SPI_FULL_SPEED for best performance.
// try SPI_HALF_SPEED if bus errors occur.
// Initialize HARDWARE SPI with user defined chipSelect
if (!card.init(SPI_FULL_SPEED, chipSelect)) error("card.init failed");
// initialize a FAT volume
if (!volume.init(&card)) error("volume.init failed!");
Serial.print("Type is FAT");
Serial.println(volume.fatType(), DEC);
if (!root.openRoot(&volume)) error("openRoot failed");
Serial.println("SD initialized");
}
void ipArrayFromString(byte ipArray[], String ipString) {
int dot1 = ipString.indexOf('.');
ipArray[0] = ipString.substring(0, dot1).toInt();
int dot2 = ipString.indexOf('.', dot1 + 1);
ipArray[1] = ipString.substring(dot1 + 1, dot2).toInt();
dot1 = ipString.indexOf('.', dot2 + 1);
ipArray[2] = ipString.substring(dot2 + 1, dot1).toInt();
ipArray[3] = ipString.substring(dot1 + 1).toInt();
}
void connectToMyServer(String ip) {
ipArrayFromString(serverAddress, ip);
if (ClientOperation.connect(serverAddress, 21)){// && client1.connect(serverAddress, 9000) && client2.connect(serverAddress, 9000)) {
Serial.println("connected to the FTP server");
} else {
Serial.println("failed ");
}
}
void setup() {
Serial.begin(115200);
LineBreaker[0]='\015';
LineBreaker[1]='\012';
LineBreaker[2]='\0';
while (!Serial.available()) Spark.process(); //Spark.process() it seems without the spark has some problem
//just to consume whatever is passed in the begin to start
while (Serial.available()) Serial.read();
InitSD();
connectToMyServer(FTP_ADDRESS);
}
void SendFileContent(char * Filename){
double currentTime,sentFTPTime;
int remainingBytes=0;
uint32_t t;
double r;
uint32_t n;
byte * buf;
totalread=0;
fileSize=0;
// open the file to send from the SD
if (!file.open(&root, Filename, O_RDWR)) {
error("open failed");
}
//allocate space to store a piece of data
buf = (byte *)malloc(sizeof(byte)*BUF_SIZE);
n = file.fileSize()/BUF_SIZE; //chunks to send
fileSize=file.fileSize();
t = millis();
Serial.print("n: ");
Serial.println(n);
delay(3000);
//send chunks of data, size is BUF_SIZE
for (uint32_t i = 0; i < n; i++) {
if (file.read(buf, BUF_SIZE) != BUF_SIZE) {
error("read failed");
}
else{
//The time interval between two TCP/IP actions must >1 ms.
while((currentTime-sentFTPTime)< 10)
currentTime=millis();
//Serial.println(buf);
ClientData.write(buf,BUF_SIZE);
progressReport(BUF_SIZE);
sentFTPTime=currentTime;
currentTime=millis();
}
}
free(buf);
//at the end this write the remaining bytes if there are any
remainingBytes=file.fileSize() % BUF_SIZE;
if (remainingBytes>0){
//allocate space to store a piece of data
buf = (byte *)malloc(sizeof(byte)*remainingBytes);
if (file.read(buf,remainingBytes) != remainingBytes) {
error("read failed");
}
else{
ClientData.write(buf,remainingBytes);
progressReport(remainingBytes);
}
}
free(buf);
//speed report
t = millis() - t;
r = (double)file.fileSize()/t;
Serial.print("Read ");
Serial.print(r);
Serial.println(" kB/sec");
Serial.println("Done");
file.close();
}
void progressReport(int Add){
totalread+=Add;
Serial.print(totalread);
Serial.print("/");
Serial.println(fileSize);
}
void loop() {
switch(step){//Prepare the message to send
case 1:
messageToSend="USER ";
messageToSend.concat(FTP_USER);
delay(3000);
break;
case 2:
messageToSend="PASS ";
messageToSend.concat(FTP_PASS);
delay(3000);
break;
case 3:
messageToSend="PASV";
delay(3000);
break;
case 4:
messageToSend="STOR ";
messageToSend.concat(FILE_TO_UPLOAD);
delay(3000);
break;
case 5:
ClientOperation.stop();
Serial.println("ClientOperation stopped");
step+=1;
break;
}
if (messageToSend != ""){//Send the message to the server
length=messageToSend.length();
ClientOperation.write((uint8_t *)messageToSend.c_str(),length);
ClientOperation.write((uint8_t *)LineBreaker,2);
Serial.println(messageToSend);
messageToSend="";
delay(3000);
}
//Client operation
if (ClientOperation.connected()) {
while(ClientOperation.available()) {//shows reponses messages from the FTP server
char charac1 = ClientOperation.read();
Serial.print(charac1);
messageReceived+=charac1;
}
if (step==3){//find the last two number that will determine the port to connect to (from the response message "227 Entering Passive Mode (xx,xx,xx,xx,142,73)")
length=preString.length();
StartingPosition=messageReceived.indexOf(preString)+length;
EndingPosition=messageReceived.indexOf(",",StartingPosition);
value=messageReceived.substring(StartingPosition,EndingPosition).toInt();
value*=256;
StartingPosition=EndingPosition+1;
EndingPosition=messageReceived.indexOf(")",StartingPosition);
value+=messageReceived.substring(StartingPosition,EndingPosition).toInt();
ClientData.connect(serverAddress, value); //open a connection and wait to transfer the file
delay(1000);
}
if (ClientData.connected() && step==4) {
Serial.println("ClientData IS COONECTED: ");
SendFileContent(FILE_TO_UPLOAD);
Serial.println("COPYING DONE");
ClientData.stop();
Serial.println("CONNECTION CLOSED");
}
step+=1;
messageReceived="";
delay(1000);
}
}