I am using an ethernet module and the ethercard library.
In my project, I want to read data from a web page ( a php script that echos a page of data ) and then save that data to a file on the SD card.
I have the ethernet module working and communicating with the online web page, and receiving the data.
I also have the writing and reading of the data to / from the SD card.
Am using a Mega 2560 board, with common pins for ethercard and SDcard on pins 50, 51, 52. Pin 53 is set as Output, and ethercard uses CS pin 46, and SD card CS pin 42.
No problems so far, except that some of the packets received in the Ethernet buffer sent to the callback are duplicated, and I can't see any reason why this would be so, or how to overcome this ( except for having to read all the data in the SD card file and check that the buffer data received in the callback has not already been saved to the SD card.
In the Serial Monitor data below, the following are duplicated :
my_callback initiated. Block : 3
is a duplicate of :
my_callback initiated. Block : 1
my_callback initiated. Block : 5
is a duplicate of :
my_callback initiated. Block : 4
The working code that I have so far is :
#include <EtherCard.h>
#include <SPI.h>
#include <SD.h>
// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x70,0x2D,0x30,0x31 };
static byte static_ip[] = { 192,168,1,219 }; //local IP of the ethercard module
static byte static_gw[] = { 192,168,1,254 }; // router IP address
static byte static_dns[] = { 192,168,1,254 }; // dns via router
byte Ethernet::buffer[700]; // buffer for ethercard to pass received data to callback function
byte ebuf2[700]; // array used to copy Etehrnet::buffer for use in the callback
int CallBackBlockCount = 0;
static uint32_t timer;
const char website[] PROGMEM = "www.galatime.co.za";
boolean SDavailable;
int LoopCntr = 0;
char TXbuf[20];
// SD Card variables
char SDvar1[5];
char SDvar2[5];
char SDvar3[5];
char SDvar4[5];
char SDvar5[5];
void ClearSDvars(){ // Clears all the temporary holding variables used when reading data from the SD card
memset(SDvar1, 0, sizeof(SDvar1)); SDvar1[0] = '\0';
memset(SDvar2, 0, sizeof(SDvar2)); SDvar2[0] = '\0';
memset(SDvar3, 0, sizeof(SDvar3)); SDvar3[0] = '\0';
memset(SDvar4, 0, sizeof(SDvar4)); SDvar4[0] = '\0';
memset(SDvar5, 0, sizeof(SDvar5)); SDvar5[0] = '\0';
}
void WriteSDfile(int WriteFileNo = 0, int RemFile = 0, char* wrTxt = ""){
Serial.println("......................................");
Serial.println("WriteSDfile got data : ...............");
Serial.println("......................................");
Serial.println(wrTxt);
Serial.println("......................................");
Serial.println("End of Data ..........................");
Serial.println("......................................");
File fh;
if(RemFile == 1){
if (WriteFileNo == 1) SD.remove("file1.txt");
if (WriteFileNo == 2) SD.remove("file2.txt");
if (WriteFileNo == 3) SD.remove("file3.txt");
}
if (WriteFileNo >= 1 && WriteFileNo <= 3){
// open the file. note that only one file can be open at a time, so you have to close this one before opening another.
if (WriteFileNo == 1) fh = SD.open("file1.txt", FILE_WRITE);
if (WriteFileNo == 2) fh = SD.open("file2.txt", FILE_WRITE);
if (WriteFileNo == 3) fh = SD.open("file3.txt", FILE_WRITE);
if (fh) { // if the file opened okay, write to it:
Serial.print("Writing to file ...");
fh.write(wrTxt);
fh.close(); // close the file:
Serial.println("done.");
Serial.println("......................................");
Serial.println("......................................");
} else {
Serial.println("error opening SD file for writing"); // if the file didn't open, print an error:
}
}
}
void ReadSDfile(int ReadFileNo = 0){
if (ReadFileNo >= 1 && ReadFileNo <= 3){
File fh;
Serial.println("......................................");
Serial.println("......................................");
Serial.print("SD Read File No : ");
Serial.println(ReadFileNo);
Serial.print("SD Available : ");
Serial.println(SDavailable);
Serial.println("......................................");
Serial.println("......................................");
if(SDavailable != false) {
if (ReadFileNo == 1) fh = SD.open("file1.txt", FILE_READ);
if (ReadFileNo == 2) fh = SD.open("file2.txt", FILE_READ);
if (ReadFileNo == 3) fh = SD.open("file3.txt", FILE_READ);
int useSDvar = 1;
int chPos = 0;
int lineNo = 0;
ClearSDvars();
if(!fh) {
Serial.println("Error : SD open fail");
} else {
while(fh.available()) {
char ch = fh.read(); // read 1 char at a time from the SD file
if (ReadFileNo != 2){
if(ch == '\n') { // \n = LF ( used for all OS ) - End of Line - do something with the populated SDvars
// move the holding variables data to the data arrays
// echo the vars to the Serial monitor
Serial.print("var 1 : "); Serial.println(SDvar1);
Serial.print("var 2 : "); Serial.println(SDvar2);
Serial.print("var 3 : "); Serial.println(SDvar3);
Serial.print("var 4 : "); Serial.println(SDvar4);
Serial.print("var 5 : "); Serial.println(SDvar5);
Serial.println("----------------------");
ClearSDvars(); // clear the holding variables
useSDvar = 1; // move to the first holding variable
chPos = 0; // move back to the first char position in the new holding variable
lineNo++;
} else if(ch == '\r') { // \r = CR ( used by Windows only )
// do nothing - ignore the \r because we reacted to the \n
} else if(ch == ',') { // this is a comma to delimit fields
useSDvar++; // move to the next holding variable
chPos = 0; // move back to the first char position in the new holding variable
} else if(chPos < 59) { // not \n or \r so deal with read char
if(useSDvar == 1) SDvar1[chPos] = ch; // add the char to the holding variable at position chPos
if(useSDvar == 2) SDvar2[chPos] = ch;
if(useSDvar == 3) SDvar3[chPos] = ch;
if(useSDvar == 4) SDvar4[chPos] = ch;
if(useSDvar == 5) SDvar5[chPos] = ch;
chPos++; // move to the next char position in the holding variable
}
}
if (ReadFileNo == 2){
Serial.print(ch); // just dump the file content to the serial monitor
}
} // end of : while(fh.available)
fh.close();
} // end of : if SD opened OK
}else{
Serial.println("Error : SD connect failed");
}
ClearSDvars(); // clear the holding variables as the data has been moved to the data arrays
}
}
}