Go Down

Topic: (RFID project) SD problem with Ethernet library (Read 1 time) previous topic - next topic

Terminox

Jul 23, 2014, 09:26 am Last Edit: Jul 23, 2014, 10:00 am by Terminox Reason: 1
Hi everyone!

I'm doing a RFID project using NFC PN532 (RFID reader) and Arduino UNO with Ethernet Shield. That is, when I touch the RFID card (I use Mifare Classic.) with the reader, the data is written to the Mifare and the micro SD card on the Ethernet Shield.

My code is here ... (My problem is below the code.)

Code: [Select]

#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <nfc.h>
NFC_Module nfc;

File myFile;

u8 buf[5], sta;

void setup()
{
 Serial.begin(9600);
 pinMode(4, OUTPUT);
 SD.begin(4);
 nfc.begin();

 uint32_t versiondata = nfc.get_version();
 if (!versiondata) {
   Serial.println("Didn't find PN53x board");
   delay(2000);
   asm volatile ("  jmp 0"); // Reboot if didn't find PN53x board.
 }
 
 nfc.SAMConfiguration();
 Serial.println("Waiting for a card ...");
}

void loop()
{
 sta = nfc.InListPassiveTarget(buf);
 delay(200);
 if (sta && buf[0] == 4)
 {
   Serial.println("Connected.");
   readWriteCard();
   writeSD();
   Serial.println("-----");
   delay(1000);
   Serial.println("Waiting for a card ...");
 }
}

boolean newCard(u8 block60[16]){
 int j = 0;
 for(int i = 0; i < 16; i++){
    if(block60[i] == 0){
       j++;
    }
 }
 if(j == 16){
   return true;
 } else{
   return false;
 }
}

void readWriteCard(){
 Serial.println("A card found!");
 Serial.print("Card ID: ");
 nfc.puthex(buf, buf[0]);
 Serial.println("");

 u8 keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
 u8 rdata[16], wdata[16];
 u8 auth;
 nfc.MifareAuthentication(0, 60, &buf[1], buf[0], keya);
 nfc.MifareReadBlock(60, rdata);
 if(newCard(rdata)){
   Serial.println("This is a new card.");
   memcpy(wdata, (u8[]){4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, sizeof wdata);
   nfc.MifareWriteBlock(60, wdata);
 }
 nfc.MifareReadBlock(60, rdata);
 int block = rdata[0];
 if((block + 1) % 4 == 0){
   block++;
 }
 auth = nfc.MifareAuthentication(0, block, &buf[1], buf[0], keya);
 Serial.print("Authentication: ");
 Serial.print("block "); Serial.print(block); Serial.print(" ");
 Serial.println(auth);
 if(auth){
   memcpy(wdata, (u8[]){1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, sizeof wdata);
   nfc.MifareWriteBlock(block, wdata);
   Serial.print("Card: "); nfc.puthex(wdata, 16); Serial.println("");
   nfc.MifareAuthentication(0, 60, &buf[1], buf[0], keya);
   memcpy(wdata, (u8[]){block+1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, sizeof wdata);
   nfc.MifareWriteBlock(60, wdata);
 } else{
   Serial.println("Authentication failed. This block can't be written.");
 }
}

void writeSD(){
 myFile = SD.open("test.txt", FILE_WRITE);
 if (myFile) {
   Serial.print("Writing to test.txt...");
   myFile.println("testing 1, 2, 3.");
   myFile.close();
   Serial.println("done.");
 } else {
   Serial.println("error opening test.txt");
 }
 myFile = SD.open("test.txt");
 if (myFile) {
   Serial.println("test.txt:");
   while (myFile.available()) {
    Serial.write(myFile.read());
   }
   myFile.close();
 } else {
   Serial.println("error opening test.txt");
 }
}


The line "myFile = SD.open("test.txt");" doesn't work well, so the serial monitor send "error opening test.txt". But if I doesn't include the Ethernet library (Ethernet.h), the project works!!!!!!!

I don't know how Ethernet.h make this happened.  :smiley-roll-sweat: I want your help so much.  =( Thank you very much for your kind attention and your help.  XD

*Note : Don't wonder why I include Ethernet.h without using it. My project haven't done yet.

SurferTim

You have probably run out of SRAM. There is an easy solution. Use a Mega 2560.

Go Up