Arduino RFID with the Arduino UNO, Ethernet Shield and RC522

Hey there

I'm working at a Arduino Project which isn't working. It should be possible to read RFID tags with the RC522 Reader over the Ethernet Shield. The Ethernet Shield is plugged on the Arduino Uno and RC522 is connected with it.

I've written a code but it isn't working. Can someone help me and tell me why?

Thanks for your help

Greetings

Michel

You can find the Code down here:

/*
* Michel Kempf
*/

#include <SPI.h>
#include <Ethernet.h>
#include <MFRC522.h>

#define RST_PIN         9           
#define SS_PIN          8         

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Instanz für MFRC522 erzeugen


#define NR_KNOWN_KEYS   8
byte knownKeys[NR_KNOWN_KEYS][MFRC522::MF_KEY_SIZE] =  {
   {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // FF FF FF FF FF FF = factory default
   {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // A0 A1 A2 A3 A4 A5
   {0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, // B0 B1 B2 B3 B4 B5
   {0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}, // 4D 3A 99 C3 51 DD
   {0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}, // 1A 98 2C 7E 45 9A
   {0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // D3 F7 D3 F7 D3 F7
   {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, // AA BB CC DD EE FF
   {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}  // 00 00 00 00 00 00
};

/*
* Inizialisieren
*/
void setup()

   
{

   Serial.begin(9600);         // Initialisiere serielle Verbindung mit dem Computer
   while (!Serial);            // Mache nichts wenn der serielle Port nicht frei ist
   SPI.begin();                // Initialisiere SPI bus
   mfrc522.PCD_Init();         // Initialisiere MFRC522 card
   //Serial.println(F("Try the most used default keys to print block 0 of a MIFARE PICC."));
   Serial.println("RFID Projekt");
   Serial.println("=====================");
}

/*
* Hex Werte für die Serielle Übertragung
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
   for (byte i = 0; i < bufferSize; i++) {
       Serial.print(buffer[i] < 0x10 ? " 0" : " ");
       Serial.print(buffer[i], HEX);
   }
}

//Keytest
boolean try_key(MFRC522::MIFARE_Key *key)
{
   boolean result = false;
   byte buffer[18];
   byte block = 0;
   MFRC522::StatusCode status;
   
   if ( ! mfrc522.PICC_IsNewCardPresent())
       return false;
   if ( ! mfrc522.PICC_ReadCardSerial())
       return false;
   // Serial.println(F("Authenticating using key A..."));
   status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid));
   if (status != MFRC522::STATUS_OK) {
       //Serial.print(F("PCD_Authenticate() failed: "));
       //Serial.println(mfrc522.GetStatusCodeName(status));
       return false;
   }

   // Lesen
   byte byteCount = sizeof(buffer);
   status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
   if (status != MFRC522::STATUS_OK) {
       //Serial.print(F("MIFARE_Read() failed: "));
       //Serial.println(mfrc522.GetStatusCodeName(status));
   }
   /*else {
       // Erfolgreiches lesen
       result = true;
       Serial.print(F("Success with key:"));
       dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE);
       Serial.println();
       Dump block data
       Serial.print(F("Block ")); Serial.print(block); Serial.print(F(":"));
       dump_byte_array(buffer, 16);
       Serial.println();
   }
   Serial.println();

   mfrc522.PICC_HaltA();       // Halt PICC
   mfrc522.PCD_StopCrypto1();  // Stopt die RC522 Entschlüsselnung
   return result;
}*/

/*
* Main loop
*/
void loop() {
   // Neue Karte
   if ( ! mfrc522.PICC_IsNewCardPresent())
       return;

   // Bekannte Karte
   if ( ! mfrc522.PICC_ReadCardSerial())
       return;

   // Anzeige
   Serial.print(F("Buhler AG")); //Überschrift
   Serial.println();
   Serial.print(F("ID:")); //ID Überschrift
   dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); //ID Ausgabe
   Serial.println();
   Serial.println("---------------------");
   delay(1000); // Pause beim Auslesen
   
   /*
   * Auskommentierung der PICC Type Ausgabe
   * 
   //Serial.print(F("PICC type: "));
   //MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
   //Serial.println(mfrc522.PICC_GetTypeName(piccType));
   delay(2000);
   *    
   */
   
   // Überprüfung von unbekannten Schlüsseln
   MFRC522::MIFARE_Key key;
   for (byte k = 0; k < NR_KNOWN_KEYS; k++) {
       // Kopieren des Chips ind die MIFARE_Key Struktur
       for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
           key.keyByte[i] = knownKeys[k][i];
       }
       // Testen des Chips
       if (try_key(&key)) {
           // Chip gefunden
           break;
       }
   }
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup (the italics in your code above for example), leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

When your code requires a library that's not included with the Arduino IDE please always post a link (using the chain link icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger(Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

Please explain exactly what you mean by "isn't working".

The programm don't write the rfid number in the serial monitor. I am using the Com6 monitor with 9600 baud.

This person is having the same problem.

http://forum.arduino.cc/index.php?topic=483360.0

Does it work without the Ethernet shield?

At which line number of your sketch are you expecting the RFID number to be printed?

Yes it is working without the Ethernet Shield.

The number should printed out at Line:

dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);

Yes it is working without the Ethernet Shield.

That suggests, then, that both shields are using SPI to communicate with the Arduino, and that both are using the same slave select pin. Without seeing a link to the RFID reader you are using, and a link to the library that deals with that hardware, we can't help you.

I use this RFID Reader:
https://www.microspot.ch/msp/de/pc-komponenten/pc-zubehoer/arduino-compatible-rc522-rfid-module-(blue)-0001324021

And this library:

https://drive.google.com/drive/folders/0B_8LHGc8wgu2TWRRREczTWY3V1E?usp=sharing

michel_kempf:
I use this RFID Reader:
https://www.microspot.ch/msp/de/pc-komponenten/pc-zubehoer/arduino-compatible-rc522-rfid-module-(blue)-0001324021

And this library:

https://drive.google.com/drive/folders/0B_8LHGc8wgu2TWRRREczTWY3V1E?usp=sharing

Is it so hard to use the proper icon to make those "links" actually work?

https://www.microspot.ch/msp/de/pc-komponenten/pc-zubehoer/arduino-compatible-rc522-rfid-module-(blue)-0001324021

https://drive.google.com/drive/folders/0B_8LHGc8wgu2TWRRREczTWY3V1E?usp=sharing

When I click on your library link, I get this:

The proxy server is refusing connections

I KNOW that the library was not published there. Provide a link to where you got the library, not where you put it.

michel_kempf:
GitHub - miguelbalboa/rfid: Arduino RFID Library for MFRC522

The header file contains:

 * The microcontroller and card reader uses SPI for communication.

The constructor defines the slave select pin, which you set to pin 8, so that shouldn't conflict with the Ethernet library.

However, I just re-read your code, and I see nothing in it that actually uses the Ethernet shield.

So, are you says that just using the two shields at the same time causes the RFID shield to stop working?

The main target is to store the rfid number in MySQL database. I tested it with an other program to connect to the database and it worked. But before i store the number in the database it should be possible to read the tags when the rfid reader is connected with the Ethernet shield.

But before i store the number in the database it should be possible to read the tags when the rfid reader is connected with the Ethernet shield.

In theory, anyway. So, you have not answered my question.

With an example that reads data from the RFID card loaded, does the code work when the RFID shield is in place, without the ethernet shield being in place?

With no code changes, does simply unplugging the RFID shield, plugging the Ethernet shield in, and then plugging the RFID shield back in cause the code to quit working?

I don't understand what you mean. Should I load the program on the shield and then plug the rfid reader in?

Should I load the program on the shield

You can't do that. You can only load programs onto the Arduino.

Unplug everything. Plug the RFID shield only in. Power the Arduino and wave a card. Does it get read?

Unplug everything. Plug the Ehternet shield in. Plug the RFID shield in. Power the Arduino and wave a card. Does it get read?

Obviously, you need to upload the sketch before plugging any shields in.

Bough variations don't work. :confused:

I think it's a power supply problem. The usb connection must be giving the Arduino not enough power.

michel_kempf:
Bough variations don't work. :confused:

So, the Ethernet shield is a red herring.

Is something in Code wrong?