Go Down

Topic: List only *.csv files (Read 3399 times) previous topic - next topic

Onenate

I think I need to put this in to the code but not too sure how

sd.vwd()->rewind();


Onenate

if I try this

Code: [Select]

#include <SdFat.h>
#include <SdFile.h>
#include <SdBaseFile.h>


// SD chip select pin
const uint8_t chipSelect = SS;

// file system object
SdFat sd;
SdFile file;
SdBaseFile lFile;
SdFile root;
SdVolume volume;
uint16_t count; // count of files
int const file_size =12;
char fname[12];
char file_list[] = "FILELIST.TXT";
char *CSV_EXT = "CSV";
#define SD_SELECT 4

// define a serial output stream
ArduinoOutStream cout(Serial);

#define error(s)

//------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
           
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);


if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

Serial.println("program started");


}



void loop() {
delay(500);
call();

//Serial.print("test");

}
void call()
{

if (count == 6)
{
sd.vwd()->rewind();
}
 

count = 0;

while (file.openNext(sd.vwd(), O_READ));

    if (file.isFile())
{ file.getFilename(fname);
if (strcmp(CSV_EXT, &fname[strlen(fname)-strlen(CSV_EXT)]) == 0){
                Serial.println(fname);
                count++;
}
      }
  file.close();

}



I only get the follow printing out

program started
10001210.CSV
10000607.CSV
10001009.CSV
10000101.CSV
10000219.CSV
10000308.CSV

I would expect when it hit the 6 recorded it rewinds it and will start over again but no luck


michinyon

You should check that the file actually exists,  before you try to delete it.

You are trying to do too many things at once.   Try reading the names of all of the files
on the SD card, first.   Get that to work.    And then you can just stop printing the
names of the files which are not CSV files.

If the SD card system allows file names longer than the old DOS 8.3 system,  then you should
read them.  There is no reason why you can't allocate a longer array to contain your file names.

Onenate

apposed to trying to delete the files.

how can I make the

while (file.openNext(sd.vwd(), O_READ));

repeat it has the close file at the end of it but it I rerun the command it does not relist the contents of the sd card

any ideas?

PaulS

Quote
any ideas?

Yes. Slow down. Think before you type. Use capital letters and punctuation where needed.

That makes parsing your sentences a lot easier.

Your last post makes little sense.

Onenate

I get told off for it all the time :)

Let me try and explain what I am trying to do.

I want to open the SD card and read through the files and check for any CSV files let say every 10 -15 min, if it finds any it will run an FTP command to upload all the *.csv files stored on the SD card, if the file that it is processing does not equal to the current log file it deletes it, as I have no need to store an old file on the SD card.

Code: [Select]


count = 0;

while (file.openNext(sd.vwd(), O_READ));

    if (file.isFile())
{ file.getFilename(fname);
if (strcmp(CSV_EXT, &fname[strlen(fname)-strlen(CSV_EXT)]) == 0){
                Serial.println(fname);
                count++;
}
      }
  file.close();

}


Onenate

If I run that command a second time I only get 1 or 2 files not all the file that are stored on the SD card.

PaulS

I know that
                        it has been
          suggest that
                                           you use
                 Tools + Auto Format
        to properly indent
                            your code.

http://snippets-r-us.com is still waiting for your snippets. Here, we need to see ALL of your code.

Onenate

Paul thanks for the tip on the auto format it cleaned up the code and it was soo easy.

here is the full code

Code: [Select]



#include <SdFat.h>
#include <SdFatUtil.h>
#include <SdFile.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 );


// SD chip select pin
const uint8_t chipSelect = SS;

// file system object
SdFat sd;
SdFile uploadfile;
uint16_t count; // count of files
int const file_size =13;
char fname[file_size];
char dname[file_size];
char file_list[13] = "10000219.CSV";
char *CSV_EXT = "CSV";
#define SD_SELECT 4


/************ SDCARD STUFF ************/
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile dfile;
SdBaseFile ufile;

// change to your server
//IPAddress server(serverName);
char serverName[] = "update.ftp.com.au";

EthernetClient client;
EthernetClient dclient;

char outBuf[128];
char outCount;
int CYCCount = 0;

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


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

  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  //root.openRoot(volume);

  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);


  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  digitalWrite(10,HIGH);
  delay(2000);
}

void loop()
{

if ( CYCCount >= 5000)
{Serail.print()
  callfile();
CYCCount = CYCCount =0;
}

CYCCount =CYCCount +1 ;
}


//***************************************** voids******************************************//

void callfile()
{
  count = 0;
  while (uploadfile.openNext(sd.vwd(), O_READ))
  { 
    if (uploadfile.isFile())
    {
      uploadfile.getFilename(fname);
      if (strcmp(CSV_EXT, &fname[strlen(fname)-strlen(CSV_EXT)]) == 0){
        //Serial.println(fname);
        doFTP();
        count++;
      }
    }
    delay (1000);
    uploadfile.close();

  }
}

byte doFTP()
{


  Serial.println("SD opened");

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

  if(!eRcv()) return 0;

  client.write("USER uesname\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();
    ufile.close();
    return 0;
  }

  Serial.println("open file");
  ufile.open(fname,O_READ);

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

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

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

  while(ufile.available())
  {
    clientBuf[clientCount] = ufile.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");

  ufile.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");
  ufile.close();
  Serial.println("SD closed");

}



Onenate

Okay I worked out the rewind function, see the updated void loop with the rewind before it calls the open next command.

Now I just need to work out how to ping the IP address of the server before it starts the FTP .

Also Delete the file the uploaded file once processed.

Code: [Select]
void loop()
{

if ( CYCCount >= 5000)
{
  Serial.println("FTP upload Started ");
    sd.vwd()->rewind();
   callfile();
CYCCount = CYCCount =0;
}
Serial.println(CYCCount);
CYCCount =CYCCount +1 ;
}


Onenate

Okay I have the working code that
1. cycles through the code for at the moment 10 seconds.
2. Pings a for a local internet connection.
3. If found  it will check for *.CSV files on the SD card.
4. Uploads any CSV file to a server.
5. Deletes the file from the SD card once uploaded.
6. Restarts Cycle.

The only final issues I have is if the internet connection is lost when it has started the read *.CSV or FTP it does not time out.

Any ides how to add this in to ftp section ?

Once I have worked this out I have every thing I need and it is all done and dusted :)


Code: [Select]

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

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

// 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 );
byte pingAddr[] = {
  192,168,3,254}; // ip address to ping

// SD chip select pin
const uint8_t chipSelect = SS;

// file system object
SdFat sd;
SdFile uploadfile;
uint16_t count; // count of files
int const file_size =13;
char fname[file_size];
char dname[file_size];
//char file_list[13] = "10000219.CSV";
char fileName[13] = "10000221.CSV";
char *CSV_EXT = "CSV";
char pingbuffer [256];
#define SD_SELECT 4


/************ SDCARD STUFF ************/
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile dfile;
SdBaseFile ufile;

// change to your server
//IPAddress server(serverName);
char serverName[] = "update.ftp.com.au";

EthernetClient client;
EthernetClient dclient;

char outBuf[128];
char outCount;
int CYCCount = 0;
byte pingcount=0;
byte pingfail=0;

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


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");         
  }
  PgmPrint("Free RAM: ");
  Serial.println(FreeRam());

  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  // if (!card.init(SPI_HALF_SPEED))

  pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
  digitalWrite(10, HIGH); // but turn off the W5100 chip!
  if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");

  // initialize a FAT volume
  if (!volume.init(&card)) error("vol.init failed!");

  PgmPrint("Volume is FAT");
  Serial.println(volume.fatType(),DEC);
  Serial.println();

  if (!root.openRoot(&volume)) error("openRoot failed");

  // list file in root with date and size
  PgmPrintln("Files found in root:");
  root.ls(LS_DATE | LS_SIZE);
  Serial.println();

  // Recursive list of all directories
  PgmPrintln("Files found in all dirs:");
  root.ls(LS_R);

  Serial.println();
  PgmPrintln("Done");


  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  digitalWrite(10,HIGH);
  delay(2000);
}

void loop()
{

  if ( CYCCount >= 1000)
  {
    bool pingRet;
    ICMPPing ping(pingSocket);
    pingRet = ping(3, pingAddr, pingbuffer);
    Serial.println(pingbuffer);
    if(pingRet) // Failure
    {
      Serial.println("pingSuccess");
      pingcount = pingcount +1;
    }
    else
    {
      Serial.println("pingfail");
      pingfail = pingfail +1; 
    }

    if (pingcount >=10)
    {
      Serial.println("FTP upload Started ");
      sd.vwd()->rewind();
      callfile();
      CYCCount = CYCCount =0;
      pingcount =pingcount =0;
    }

    if (pingfail >=1)
    {
      pingcount =pingcount =0;
      pingfail = pingfail =0; 
      CYCCount = CYCCount =0;
    }
  }

  Serial.println(CYCCount);
  CYCCount =CYCCount +1 ;
}






//***************************************** voids******************************************//
void callfile()
{
  Serial.println("Call File");
  count = 0;
  while (uploadfile.openNext(sd.vwd(), O_READ))
  { 
    if (uploadfile.isFile())
    {
      uploadfile.getFilename(fname);
      if (strcmp(CSV_EXT, &fname[strlen(fname)-strlen(CSV_EXT)]) == 0){
        Serial.println(fname);
        doFTP();
        todelete();
        count++;
      }
    }
    delay (1000);
    uploadfile.close();
  }


}


void todelete()

  if (strcmp(fname,fileName) == 0){
    Serial.println("Working File");
    uploadfile.close();
  }
  else
  {
    uploadfile.close();
    if (dfile.open(&root, fname, O_RDWR))
      dfile.remove() ;
    Serial.println("----------------------------------------------------------->File To Be Deleted");
  }
}

byte doFTP()
{


  Serial.println("SD opened");

  if (client.connect(serverName,21)) {
    Serial.println("Command connected");
  }
  else {
    ufile.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();
    ufile.close();
    return 0;
  }

  Serial.println("open file");
  ufile.open(fname,O_READ);

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

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

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

  while(ufile.available())
  {
    clientBuf[clientCount] = ufile.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");

  ufile.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");
  ufile.close();
  Serial.println("SD closed");

}




Onenate

I tried to add this this error in to the FTP section it works on a fail and seems to continue but then just hangs.

Is their any way I can make to program restart if hung?

Code: [Select]
void connectionConnect()
{
  bool pingRet;
  ICMPPing ping(pingSocket);
  pingRet = ping(3, pingAddr, pingbuffer);
  Serial.println(pingbuffer);
  if(pingRet) // Failure
  {
    Serial.println("pingSuccess");
  }
  else
  {
    Serial.println("pingfail");
    efail();
  }

}


PaulS

Code: [Select]
  if(pingRet) // Failure
  {
    Serial.println("pingSuccess");
  }

The print statement and the cryptic comment don't seem to match.

Code: [Select]
  bool pingRet;
  pingRet = ping(3, pingAddr, pingbuffer);

It's not only OK to declare and value a variable in one statement, it's generally preferred.
Code: [Select]
  bool pingRet = ping(3, pingAddr, pingbuffer);

Quote
Is their any way I can make to program restart if hung?

Don't do things that cause it to hang.

Onenate

Paul,

My issues that I am trying to take in to consideration is the file is in the middle of uploading and the internet drops it will time out and go back to a normal loop.


Onenate

I have a strange issue with my mega 2560 r3 it was all good now it will not load any sketch at all even a basic code like the following.

Code: [Select]

void setup() {
Serial.print("hello");}
void loop() {

}


any ideas?

Go Up