Go Down

Topic: Uploading a file from st card to FTP server (Read 2662 times) previous topic - next topic

spatula

Sorry, I am confused by all those #ifdefs and I may have got your code wrong, but if you are using the SD library I think you should

Code: [Select]
open("file.txt", FILE_READ);

PaulS

It still looks like you are trying to open the file using zero_READ, rather than Oh_READ.

spatula

Looking for alternatives, is the destination server under your control (i.e., you can install whatever software you like, you have no special security concerns...)? Is the file binary or text? Is it like a log file, that you can append to an existing file?

Onenate

It just a standard log.txt file with text staying at the moment "Another line for the logfile"

Paul is the zero meant to be an "o" not a zero?

Onenate

if I change the zero I get a

invalid suffix "_READ" on integer constant

if I change it to and "o"

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

Code: [Select]

byte doFTP()
{
#ifdef FTPWRITE
  fh = file.open(fileName, O_READ);
#else
  SD.remove(fileName);
  fh = SD.open(fileName,FILE_WRITE);
#endif

spatula

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

Onenate

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

Code: [Select]

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

PaulS

Quote
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.

Code: [Select]
  return 1;

Serial.println("end revice");

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

Onenate

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.

Code: [Select]

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

  if(!eRcv()) return 0;

Onenate

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

Code: [Select]
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;
}

Onenate

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

PaulS

Code: [Select]
  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.

Code: [Select]
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.

Onenate

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

Code: [Select]


#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;   
  }



Onenate

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. 

Code: [Select]

/*
   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();
}

PeterH


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 only provide help via the forum - please do not contact me for private consultancy.

Go Up