I also take the same problem: I can save data to SD into ethernet shield; however, when I reopen this file to send on FTP, the serial announce that it can not open the file. I think there is a trouble between I2C of RTC and SPI of ethernet shield, but I dont know why. Here is my code:
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#define FTPWRITE
//const int chipSelect = 4;
const int ACSPin = A3; // Define analog input pin
File dataFile;
char fileName[13] = "datalog.csv"; // filename to save to
double Current;
const double StandbyCurrent = 0.01;
// *************************** this must be unique *******************************************
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// ************************* change to your server *******************************************
IPAddress server( 172, 23, 152, 2 );
EthernetClient client;
EthernetClient dclient;
char outBuf[128];
char outCount;
//************************************** Set up Function *************************************
void setup()
{
Serial.begin(9600);
while (!Serial) ; // wait for serial
//SPI.begin();
//SPI.setBitOrder(MSBFIRST);
//SPI.setDataMode(SPI_MODE0);
// initialize the SD card:
pinMode(10, OUTPUT);
digitalWrite(10,HIGH);
// see if the card is present and can be initialized:
if (SD.begin(4))
{
Serial.println("SD initialized.");
/*
dataFile = SD.open(fileName, FILE_WRITE);
// open the file:
if (dataFile)
{
dataFile.print(" Status,Current,Date(dd/mm/yy),Time(hh:mm:ss)");
dataFile.println();
dataFile.close();
}
/
}
delay(2000);
// start the Ethernet connection:
if (Ethernet.begin(mac) != 0)
{
Serial.println("Success to configure Ethernet using DHCP");
}
// give the Ethernet shield a second to initialize:
digitalWrite(10,HIGH);
delay(2000);
// initial RTC
//SPI.setDataMode(SPI_MODE0);
setSyncProvider(RTC.get); // the function to get the time from the RTC
delay(2000);
}
//******************************* Main Function***************************************************
void loop()
{
Current = currentSensor(analogRead(ACSPin));
if (Current > StandbyCurrent)
{
rtcsd();
delay(2000);
}
else
{
if(upload()) Serial.println(F("FTP OK"));
else Serial.println(F("FTP FAIL"));
delay(2000);
}
}
//***********************************************************************************************************
byte upload()
{
//SPI.setDataMode(SPI_MODE0);
#ifdef FTPWRITE
dataFile = SD.open(fileName,FILE_READ);
#else
SD.remove(fileName);
dataFile = SD.open(fileName,FILE_WRITE);
#endif
if(!dataFile)
{
Serial.println(F("SD open fail"));
return 0;
}
#ifndef FTPWRITE
if(!dataFile.seek(0))
{
Serial.println(F("Rewind fail"));
dataFile.close();
return 0;
}
#endif
Serial.println(F("SD opened"));
if (client.connect(server,21)) {
Serial.println(F("Command connected"));
}
else {
dataFile.close();
Serial.println(F("Command connection failed"));
return 0;
}
if(!eRcv()) return 0;
client.println(F("USER myuser"));
if(!eRcv()) return 0;
client.println(F("PASS mypassword"));
if(!eRcv()) return 0;
client.println(F("SYST"));
if(!eRcv()) return 0;
client.println(F("PASV"));
if(!eRcv()) return 0;
char *tStr = strtok(outBuf,"(,");
int array_pasv[6];
for ( int i = 0; i < 6; i++) {
tStr = strtok(NULL,"(,");
array_pasv = atoi(tStr);
-
if(tStr == NULL)*
-
{*
-
Serial.println(F("Bad PASV Answer")); *
-
}*
-
}*
-
unsigned int hiPort,loPort;*
-
hiPort = array_pasv[4] << 8;*
-
loPort = array_pasv[5] & 255;*
-
Serial.print(F("Data port: "));*
-
hiPort = hiPort | loPort;*
-
Serial.println(hiPort);*
-
if (dclient.connect(server,hiPort)) {*
-
Serial.println(F("Data connected"));*
-
}*
-
else {*
-
Serial.println(F("Data connection failed"));*
-
client.stop();*
-
dataFile.close();*
-
return 0;*
-
}*
#ifdef FTPWRITE
-
client.print(F("STOR "));*
-
client.println(fileName);*
#else
-
client.print(F("RETR "));*
-
client.println(fileName);*
#endif
-
if(!eRcv())*
-
{*
-
dclient.stop();*
-
return 0;*
-
}*
#ifdef FTPWRITE
-
Serial.println(F("Writing"));*
-
byte clientBuf[64];*
-
int clientCount = 0;*
-
while(dataFile.available())*
-
{*
-
clientBuf[clientCount] = dataFile.read();*
-
clientCount++;*
-
if(clientCount > 63)*
-
{*
-
dclient.write(clientBuf,64);*
-
clientCount = 0;*
-
}*
-
}*
-
if(clientCount > 0) dclient.write(clientBuf,clientCount);*
#else
-
while(dclient.connected())*
-
{*
-
while(dclient.available())*
-
{*
-
char c = dclient.read();*
-
dataFile.write(c); *
-
Serial.write(c);*
-
}*
-
}*
#endif
-
dclient.stop();*
-
Serial.println(F("Data disconnected"));*
-
if(!eRcv()) return 0;*
-
client.println(F("QUIT"));*
-
if(!eRcv()) return 0;*
-
client.stop();*
-
Serial.println(F("Command disconnected"));*
-
dataFile.close();*
-
Serial.println(F("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.println(F("QUIT"));*
-
while(!client.available()) delay(1);*
-
while(client.available())*
-
{ *
-
thisByte = client.read(); *
-
Serial.write(thisByte);*
-
}*
-
client.stop();*
-
Serial.println(F("Command disconnected"));*
-
dataFile.close();*
-
Serial.println(F("SD closed"));*
}
_//**************************************************************************** _
void rtcsd()
{
-
//SPI.setDataMode(SPI_MODE1);*
-
tmElements_t tm;*
-
RTC.read(tm);*
-
//SPI.setDataMode(SPI_MODE0);*
-
// open the file*
-
dataFile = SD.open(fileName, FILE_WRITE);*
-
// if the file is available, write to it:*
-
if (dataFile)*
-
{*
-
dataFile.print("ON ");*
-
dataFile.print(" , ");*
-
dataFile.print(Current);*
-
dataFile.print(" , "); *
-
dataFile.print(tm.Day);*
-
dataFile.print("/");*
-
dataFile.print(tm.Month);*
-
dataFile.print("/");*
-
dataFile.print(tmYearToCalendar(tm.Year));*
-
dataFile.print(" , ");*
-
if (tm.Hour <10) dataFile.print('0');*
-
dataFile.print(tm.Hour);*
-
dataFile.print(":");*
-
if (tm.Minute <10) dataFile.print('0');*
-
dataFile.print(tm.Minute);*
-
dataFile.print(":");*
-
dataFile.print(tm.Second); *
-
dataFile.println(" "); *
-
dataFile.close(); *
-
}*
}
//Calculate current and print current Function************************
double currentSensor(int RawADC) {
-
int Sensitivity = 66; // mV/A*
-
double ZeroCurrentVcc = 2.5;*
_ double SensedVoltage = RawADC * 0.0049;_
-
double Difference = SensedVoltage - ZeroCurrentVcc;*
-
double SensedCurrent = Difference / Sensitivity;*
-
return SensedCurrent; // Return the Current*
}