Uploading a file from st card to FTP server

Sorry for insisting, but what's wrong with my previous suggestion (#15)?

sorry the reason for not using the standard SD library as the rest of my sketch depends on the sdfat library so that why I need it to work with sdfat as this is the last piece to the puzzle.

I got it going t bit further but now it is stopping at the
Serial.println("Start wercr");

byte eRcv()
{
  Serial.println("Start wercr");
  byte respCode;
  byte thisByte;

  while(!client.available()) delay(1);

  respCode = client.peek();

  outCount = 0;

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);

    if(outCount < 127)
    {
      outBuf[outCount] = thisByte;
      outCount++;      
      outBuf[outCount] = 0;
    }
  }

  if(respCode >= '4')
  {
    efail();
    return 0;  
     Serial.println("fail");
  }

  return 1;

 Serial.println("end revice");
 }

I got it going t bit further but now it is stopping at the
Serial.println("Start wercr");

What makes you think that? The first thing that happens after that statement is an endless loop that only terminates when there is data available from a client instance. Perhaps there never is.

  return 1;

 Serial.println("end revice");

You don't actually expect the print() statement to ever be executed, do you?

the only reason I was putting those extra lines in was to try and find out where it is getting to.

from the code it calls the eRcv() then just keeps looping I gather I was trying to work out why!

as I have not really changed this part in the code form SurferTim

http://playground.arduino.cc//Code/FTP

so I don't see why it would as from what I can see it just seeing if the server exists.

if (client.connect(serverName,21)) {
    Serial.println("Command connected");
  } 
  else {
    fh.close();
    Serial.println("Command connection failed");
    return 0;
  }

  if(!eRcv()) return 0;

I have got a bit further I have removed the ! from the front of the client and now I am getting

Type any character to start
Done - check on the SD
1Type any character to start
Done - check on the SD
2Type any character to start
Done - check on the SD
3Type any character to start
Done - check on the SD
4Type any character to start
Done - check on the SD
5Type any character to start
Done - check on the SD
Start FTP UploadFile is Open10001602.TXTSD opened
Command connected
0
255
fail

byte eRcv()
{
  byte respCode;
  byte thisByte;
Serial.print(respCode);

  while(client.available()); delay(1);

  respCode = client.peek();
  Serial.print(respCode);
  outCount = 0;

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);

    if(outCount < 127)
    {
      outBuf[outCount] = thisByte;
      outCount++;      
      outBuf[outCount] = 0;
    }
  }

  if(respCode >= '4')
  {
    Serial.print("fail");
    efail();
    return 0;  
  }

  return 1;
}

okay I feel stupid to admit this.... but I found something stupidly easy that I over looked I have the server name resolving to a url the only issue is I have not defined a DNS sever so that does help move it along a bit further.

Done - check on the SD
Start FTP UploadSD opened
Command connection failed
FTP FAIL
1Type any character to start
error: append failed
SD errorCode: 0X4,0XFF

  Serial.print(respCode);

I have statements like this. A number appears in the Serial Monitor, but there is no way to tell what it means.

Serial.print("respCode: ");
Serial.println(respCode);

leaves no doubt what the number means.

It appears that a number of your Serial.print() calls really should be Serial.println() calls.

Oh, and http://snippets-r-us.com might be able to help you with the snippets.

why do I get no match for operator in !fh when it complier accepts the first fh = file.open(fileName, 0_READ);

#ifdef FTPWRITE
  fh = file.open(fileName, 0_READ);
#else
  SD.remove(fileName);
  fh = SD.open(fileName,FILE_WRITE);
#endif


#endif

  if(!fh)
  {
    Serial.println("SD open fail");
    return 0;    
  }

so close I have now got it too connect and send a file but the file is blank and ideas?

as spatula state no idea why there was so many definition in it but I removed most of them and it let me get further but in doing so I think I have lost the reverence to the file.

/*
   FTP passive client for IDE v1.0.1 and w5100/w5200
   Posted October 2012 by SurferTim
*/

#include <SdFat.h>
#include <SdFatUtil.h>
#include <SPI.h>
#include <Ethernet.h>
// comment out next line to write to SD from FTP server
#define FTPWRITE
#define error(s) error_P(PSTR(s))

// this must be unique
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };  
char serverName[] = "update.ftp.com";

// change to your network settings
IPAddress ip( 192, 168, 3, 177 );    
IPAddress gateway( 192, 168, 3, 254 );
IPAddress subnet( 255, 255, 255, 0 );


/************ SDCARD STUFF ************/
Sd2Card card;
SdVolume volume;
SdFile root;
//SdFile file;
SdFat sd;
SdBaseFile file;

// change to your server
//IPAddress server(serverName);

EthernetClient client;
EthernetClient dclient;

char outBuf[128];
char outCount;

// change fileName to your file (8.3 format!)
char fileName[13] = "lo.txt";

const uint8_t chipSelect = SS;


ArduinoOutStream cout(Serial);
//...............errors..............
void error_P(const char* str) {
  PgmPrint("error: ");
  SerialPrintln_P(str);
  if (card.errorCode()) {
    PgmPrint("SD error: ");
    Serial.print(card.errorCode(), HEX);
    Serial.print(',');
    Serial.println(card.errorData(), HEX);
  }
  while(1);
}

void setup()
{
  Serial.begin(9600);

  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  if(sd.begin(4) == 0)
  {
    Serial.println("SD init fail");          
  }
 //if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

  Ethernet.begin(mac, ip, gateway, gateway, subnet); 
  digitalWrite(10,HIGH);
  delay(2000);
  Serial.println("Ready. Press f or r");
}

void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(inChar == 'f')
  {
    if(doFTP()) Serial.println("FTP OK");
    else Serial.println("FTP FAIL");
  }

  if(inChar == 'r')
  {
    readSD();    
  }

}

SdFile fh;
//File fh;

byte doFTP()
{
  Serial.println("SD opened");

  if (client.connect(serverName,21)) {
    Serial.println("Command connected");
  } 
  else {
    fh.close();
    Serial.println("Command connection failed");
    return 0;
  }

  if(!eRcv()) return 0;

  client.write("USER username\r\n");

  if(!eRcv()) return 0;

  client.write("PASS password\r\n");

  if(!eRcv()) return 0;

  client.write("SYST\r\n");

  if(!eRcv()) return 0;

  client.write("PASV\r\n");

  if(!eRcv()) return 0;

  char *tStr = strtok(outBuf,"(,");
  int array_pasv[6];
  for ( int i = 0; i < 6; i++) {
    tStr = strtok(NULL,"(,");
    array_pasv[i] = atoi(tStr);
    if(tStr == NULL)
    {
      Serial.println("Bad PASV Answer");    

    }
  }

  unsigned int hiPort,loPort;

  hiPort = array_pasv[4] << 8;
  loPort = array_pasv[5] & 255;

  Serial.print("Data port: ");
  hiPort = hiPort | loPort;
  Serial.println(hiPort);

  if (dclient.connect(serverName,hiPort)) {
    Serial.println("Data connected");
  } 
  else {
    Serial.println("Data connection failed");
    client.stop();
    fh.close();
    return 0;
  }

#ifdef FTPWRITE 
  client.write("STOR ");
  client.println(fileName);
#else
  client.write("RETR ");
  client.println(fileName);
#endif

  if(!eRcv())
  {
    dclient.stop();
    return 0;
  }

#ifdef FTPWRITE
  Serial.println("Writing");

  byte clientBuf[64];
  int clientCount = 0;

  while(fh.available())
  {
    clientBuf[clientCount] = fh.read();
    clientCount++;

    if(clientCount > 63)
    {
      Serial.println("Packet");
      dclient.write(clientBuf,64);
      clientCount = 0;
    }
  }

  if(clientCount > 0) dclient.write(clientBuf,clientCount);

#else
  while(dclient.connected())
  {
    while(dclient.available())
    {
      char c = dclient.read();
      fh.write(c);      
      Serial.write(c); 
    }
  }
#endif

  dclient.stop();
  Serial.println("Data disconnected");

  if(!eRcv()) return 0;

  client.write("QUIT\r\n");

  if(!eRcv()) return 0;

  client.stop();
  Serial.println("Command disconnected");

  fh.close();
  Serial.println("SD closed");
  return 1;
}




byte eRcv()
{
  byte respCode;
  byte thisByte;

  while(!client.available()) delay(1);

  respCode = client.peek();

  outCount = 0;

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);

    if(outCount < 127)
    {
      outBuf[outCount] = thisByte;
      outCount++;      
      outBuf[outCount] = 0;
    }
  }

  if(respCode >= '4')
  {
    efail();
    return 0;  
  }

  return 1;
}


void efail()
{
  byte thisByte = 0;

  client.write("QUIT\r\n");

  while(!client.available()) delay(1);

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  client.stop();
  Serial.println("Command disconnected");
  fh.close();
  Serial.println("SD closed");
}

void readSD()
{
file.open(fileName,O_READ);


  while(fh.available())
  {
    Serial.write(fh.read());
  }

  fh.close();
}

Onenate:
why do I get no match for operator in !fh when it complier accepts the first fh = file.open(fileName, 0_READ);

Is fh a boolean? If not, why are you using it where a boolean expression is expected?

I already gave you a sample of my limited understanding, but can you check the syntax of the open method for the version you are using? My impression from one of your previous posts is that it expects three parameters which may correspond to dirname, filename, mode. Here's the error you posted.

FTP_Upload:81: error: no match for 'operator=' in 'fh = file.SdFile::<anonymous>.SdBaseFile::open(((const char*)((char*)(& fileName))), 1u)'

Since you are using a static instantiation method (which is also overloaded), if the parameters don't match the compiler won't give you a simple syntax error but try to locate the closest match. The same may apply to the ! operator.

thank you all for you assistance what I ended up finding out was an issue with fh not getting define correctly so I took it out and removed any reference to downloading files as only need to upload the files here is the working code.

with thanks to SurferTim for your original code

/*
   FTP passive client for IDE v1.0.3 and w5100/w5200
   Posted orginally posted by SurferTim re Edited by Onenate 18/02/2013 works with SDFat libary 
   orginal code only works with sd.h libary remove download option only upload to server
   http://playground.arduino.cc//Code/FTP
*/

#include <SdFat.h>
#include <SdFatUtil.h>
#include <SPI.h>
#include <Ethernet.h>

// comment out next line to write to SD from FTP server
#define FTPWRITE
#define error(s) error_P(PSTR(s))

// this must be unique
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };  

// change to your network settings
IPAddress ip( 192, 168, 3, 177 );    
IPAddress gateway( 192, 168, 3, 254 );
IPAddress subnet( 255, 255, 255, 0 );


/************ SDCARD STUFF ************/
Sd2Card card;
SdVolume volume;
SdFile root;
SdFat sd;
SdBaseFile file;

// change to your server
//IPAddress server(serverName);
char serverName[] = "upload.ftpserver.com";

EthernetClient client;
EthernetClient dclient;

char outBuf[128];
char outCount;

// change fileName to your file (8.3 format!)
char fileName[13] = "Log.txt"; // change file to upload 

const uint8_t chipSelect = SS;


ArduinoOutStream cout(Serial);
//...............errors..................
void error_P(const char* str) {
  PgmPrint("error: ");
  SerialPrintln_P(str);
  if (card.errorCode()) {
    PgmPrint("SD error: ");
    Serial.print(card.errorCode(), HEX);
    Serial.print(',');
    Serial.println(card.errorData(), HEX);
  }
  while(1);
}

void setup()
{
  Serial.begin(9600);

  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  if(sd.begin(4) == 0)
  {
    Serial.println("SD init fail");          
  }

  Ethernet.begin(mac, ip, gateway, gateway, subnet); 
  digitalWrite(10,HIGH);
  delay(2000);
  Serial.println("Ready. Press f ");
}

void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(inChar == 'f')
  {
    if(doFTP()) Serial.println("FTP OK");
    else Serial.println("FTP FAIL");
  }

  }


byte doFTP()
{


  Serial.println("SD opened");

  if (client.connect(serverName,21)) {
    Serial.println("Command connected");
  } 
  else {
    file.close();
    Serial.println("Command connection failed");
    return 0;
  }

  if(!eRcv()) return 0;

  client.write("USER username\r\n");  // change your username here 

  if(!eRcv()) return 0;

  client.write("PASS password\r\n"); // change your password here 

  if(!eRcv()) return 0;

  client.write("SYST\r\n");

  if(!eRcv()) return 0;

  client.write("PASV\r\n");

  if(!eRcv()) return 0;

  char *tStr = strtok(outBuf,"(,");
  int array_pasv[6];
  for ( int i = 0; i < 6; i++) {
    tStr = strtok(NULL,"(,");
    array_pasv[i] = atoi(tStr);
    if(tStr == NULL)
    {
      Serial.println("Bad PASV Answer");    

    }
  }

  unsigned int hiPort,loPort;

  hiPort = array_pasv[4] << 8;
  loPort = array_pasv[5] & 255;

  Serial.print("Data port: ");
  hiPort = hiPort | loPort;
  Serial.println(hiPort);

  if (dclient.connect(serverName,hiPort)) {
    Serial.println("Data connected");
  } 
  else {
    Serial.println("Data connection failed");
    client.stop();
    file.close();
    return 0;
  }

  Serial.println("open file");
 file.open(fileName,O_READ);

  client.write("STOR ");
  client.println(fileName);

  if(!eRcv())
  {
    dclient.stop();
    return 0;
  }

  Serial.println("Writing");
  byte clientBuf[64];
  int clientCount = 0;

  while(file.available())
  {
    clientBuf[clientCount] = file.read();
    clientCount++;

    if(clientCount > 63)
    {
      Serial.println("Packet");
      dclient.write(clientBuf,64);
      clientCount = 0;
    }
  }

  if(clientCount > 0) dclient.write(clientBuf,clientCount);


  dclient.stop();
  Serial.println("Data disconnected");

  if(!eRcv()) return 0;

  client.write("QUIT\r\n");

  if(!eRcv()) return 0;

  client.stop();
  Serial.println("Command disconnected");
 
  file.close();
  Serial.println("SD closed");
  return 1;
}




byte eRcv()
{
  byte respCode;
  byte thisByte;

  while(!client.available()) delay(1);

  respCode = client.peek();

  outCount = 0;

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);

    if(outCount < 127)
    {
      outBuf[outCount] = thisByte;
      outCount++;      
      outBuf[outCount] = 0;
    }
  }

  if(respCode >= '4')
  {
    efail();
    return 0;  
  }

  return 1;
}


void efail()
{
  byte thisByte = 0;

  client.write("QUIT\r\n");

  while(!client.available()) delay(1);

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  client.stop();
  Serial.println("Command disconnected");
  file.close();
  Serial.println("SD closed");
}

now my next issue is how do I upload any file that end with .txt and csv

when I look at the

root.ls(LS_R | LS_DATE | LS_SIZE);

I am trying to define it to only resolve .txt of csv no every thing.

any idea?

i am using your code for uploading a text file to the server but i m unable to access it. kindly reply..