okay I have almost got everything I need working, I have removed the need to log the information to file and got it to processes straight away to an ftp now I just need to check if the file being processed matches a set file name and skip it or if not delete it
this is the bit of code I am have issues with so what I what I am trying to resolve is if the fname = "10000219.CSV" skip else delete the file
int const file_size =13;
char fname[file_size];
char dname[file_size];
char file_list[13] = "10000219.CSV";
if (fname == file_list)
{
Serial.print ("Todays File");
}
else
{
dfile.open(fname);
dfile.remove();
Serial.print("File Removed");
}
this code works a treat it uploads each file one at a time.
/*
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 <SdFile.h>
#include <SdBaseFile.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 file;
SdBaseFile lFile;
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;
SdFat remove;
// change to your server
//IPAddress server(serverName);
char serverName[] = "update.ftp.com.au";
EthernetClient client;
EthernetClient dclient;
char outBuf[128];
char outCount;
// 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);
Serial.println("Ready. Press f ");
}
void loop()
{
byte inChar;
inChar = Serial.read();
if(inChar == 'f')
{ callfile();
Serial.println("FTP OK");
}
}
void callfile()
{
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);
doFTP();
if (fname == file_list)
{
Serial.print ("Todays File");
}
else
{
dfile.open(fname);
dfile.remove();
Serial.print("File Removed");
}
count++;
}
}
file.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 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");
}