Pages: 1 2 [3]   Go Down
Author Topic: Uploading a file from st card to FTP server  (Read 1693 times)
0 Members and 1 Guest are viewing this topic.
Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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


Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Pages: 1 2 [3]   Go Up
Jump to: