Arduino MEGA + Arduino Ethernet Shield + 2 Adafruit PN532 RFID / NFC on SPI

Hello,

My name is Romain and I’m almost a beginner with Arduino. I mean I know the basics but I’m blocked on a problem. :disappointed_relieved:
I hope you’ll be able to help me

Here is what makes me crazy.

My goal is to record on a server which RFID reader has read a tag.

The server is a JAVA Application that accepts TCP connection.

On the other side of my construction I have an Arduino MEGA 2560. Above the board, there is Arduino Ethernet Shield with SD port.
So it’s connected through the ICSP header.
The mega + the Ethernet Shield work perfectly together.

I also have two Adafruit PN532 RFID/NFC Shield connected via SPI.
I use them via SPI because I need to have many readers. Originaly they use i2C but because it’s impossible to change their adress, they can’t work simultaneously…
So SPI.

The two RFID readers work well with the arduino mega via SPI. They’re connected to the pins 52,51 and 50 (plus 47 and 44 for SS).

Unfortunalety, when I use the two readers + the ethernet shield. It’s doesn’t work ! :0

Here is my sketch :

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

#define BORNE_NUMBER 1

// Mapping
int REDPin = 7;    // RED pin of the LED to PWM pin 7
int GREENPin = 5;  // GREEN pin of the LED to PWM pin 5
int BLUEPin = 6;   // BLUE pin of the LED to PWM pin 6
int BEEPPin = 8; // Speaker

#define SCK  (52)
#define MOSI (51)
#define SS   (46)
#define SSS  (47)
#define MISO (50)

Adafruit_PN532 rifd1(SCK, MISO, MOSI, SS);
Adafruit_PN532 rifd2(SCK, MISO, MOSI, SSS);

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0x90, 0xA2, 0xDA, 0x0E, 0xCF, 0x62 };
IPAddress serverIP(192,168,0,1); // IP Adress to our Server
int serverPort=9000;
EthernetClient client;
byte ip[] = { 192, 168, 0, 2 };

String reponse1uid = "ajhhzohzioahsncqskln4556";
String reponse2uid = "pozpoppppppzzpzopozopopoz";

void setup()
{
  pinMode(REDPin, OUTPUT);
  pinMode(GREENPin, OUTPUT);
  pinMode(BLUEPin, OUTPUT);
  pinMode(BEEPPin, OUTPUT);
   
  Serial.begin(115200);

  rifd1.begin();
  rifd2.begin();
  
  uint32_t versiondata = rifd1.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board number 1 ");
    while (1); // halt
  }
  Serial.print("Found chip PN5 number 1 "); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  
  uint32_t versiondata2 = rifd2.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board number 2 ");
    while (1); // halt
  }
  Serial.print("Found chip PN5 number 2 "); Serial.println((versiondata2>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata2>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata2>>8) & 0xFF, DEC);
 
  // configure board to read RFID tags
  rifd1.SAMConfig();
  rifd1.SAMConfig();
  

  
  Ethernet.begin(mac,ip );
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting to Server ...");
  
  // if you get a connection to the Server
  if (client.connect(serverIP, serverPort)) {
    Serial.println("connected");//report it to the Serial
    String msg="Hello Server";//Message to be sent
    Serial.println("sending Message:"+msg);//log to serial
    client.println(msg);//send the message
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  Serial.println("Waiting for an ISO14443A card");
  
  analogWrite(GREENPin, HIGH);
  beep(50);
  beep(50);
  beep(50);
  analogWrite(GREENPin, LOW);
  analogWrite(BLUEPin, HIGH);
  
}

void loop()
{
  Serial.println("Loop");
  boolean success, success2;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uid2[] = { 0, 0, 0, 0, 0, 0, 0 };
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  uint8_t uidLength2; 
  success = rifd1.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
  if (success) {
    analogWrite(GREENPin, HIGH);
    analogWrite(BLUEPin, LOW);
    beep(1000);
    Serial.println("Found a card on board 1 !");
    //Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    //Serial.print("UID Value: ");
    String uidvalue;
    for (uint8_t i=0; i < uidLength; i++) 
    {
      uidvalue = uidvalue + String(uid[i], HEX);
      //Serial.print(" 0x");Serial.print(uid[i], HEX); 
    }
    Serial.println(uidvalue);
    client.println(BORNE_NUMBER+":"+reponse1uid+":"+uidvalue);//send the message
    delay(2000);
  }
  else
  {
    success2 = rifd2.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid2[0], &uidLength2);
    if(success2)
    {
      analogWrite(GREENPin, HIGH);
      analogWrite(BLUEPin, LOW);
      beep(1000);
      Serial.println("Found a card on board 2 !");
      //Serial.print("UID Length: ");Serial.print(uidLength2, DEC);Serial.println(" bytes");
      //Serial.print("UID Value: ");
      String uidvalue;
      for (uint8_t i=0; i < uidLength2; i++) 
      {
        uidvalue = uidvalue + String(uid2[i], HEX);
        //Serial.print(" 0x");Serial.print(uid2[i], HEX); 
      }
      Serial.println(uidvalue);
      client.println(BORNE_NUMBER+":"+reponse1uid+":"+uidvalue);//send the message
      delay(2000);
    }
  }
  analogWrite(GREENPin, LOW);
  analogWrite(BLUEPin, HIGH);
  /*
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();//report it to the serial
    Serial.println("disconnected");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }*/
}

void beep(unsigned char delayms){
  analogWrite(9, 20);      // Almost any value can be used except 0 and 255
                          // experiment to get the best tone
  delay(delayms);          // wait for a delayms ms
  analogWrite(9, 0);       // 0 turns it off
  delay(delayms);          // wait for a delayms ms   
}

The result in Serial observer is :

Loop
Loop
Loop
Loop…

Thank you !

Why is checking the second reader conditional on checking the first one? Isn’t the idea that they are supposed to be independent?

#define SCK  (52)
#define MOSI (51)
#define SS   (46)
#define SSS  (47)
#define MISO (50)

The value doesn’t need to be in parentheses.

From the library:

class Adafruit_PN532{
 public:
  Adafruit_PN532(uint8_t cs, uint8_t clk, uint8_t mosi, uint8_t miso);

That’s not even close to the order of the pins you are supplying.

PaulS: Why is checking the second reader conditional on checking the first one? Isn't the idea that they are supposed to be independent?

#define SCK  (52)
#define MOSI (51)
#define SS   (46)
#define SSS  (47)
#define MISO (50)

The value doesn't need to be in parentheses.

From the library:

class Adafruit_PN532{
 public:
  Adafruit_PN532(uint8_t cs, uint8_t clk, uint8_t mosi, uint8_t miso);

That's not even close to the order of the pins you are supplying.

I followed the example provided with the library : https://github.com/adafruit/Adafruit-PN532/blob/master/examples/iso14443a_uid/iso14443a_uid.pde

I ask the second RFID reader only if the first didn't read something because it takes time to ask the reader. So the application is faster. On each loop it record only one reader.

The order is right according to https://github.com/adafruit/Adafruit-PN532/blob/master/examples/iso14443a_uid/iso14443a_uid.pde Plus, the mega and the two readers work well without the Ethernet Shield.

Can arduino support SPI protocol when it comes from both ICSP header and SPI pins ? I don't know if the pins and the header are directly connected.

Does the header have a SS pin ? I wonder if the ethernet shield doesn't say to arduino "you're the slave now and I'm the master".

I can't find informations about that :relaxed:

OK. So, it appears that the library is bit-banging SPI, since the pins in the example are not the (hardware) SPI pins on any Arduino.

So, don't use the hardware SPI pins.

I find it difficult to understand why anyone would name the chip select pin SCK, the clock pin MISO, the master out/slave in pin MOSI, and the master in/slave out pin SS. That example leaves a lot to be desired.

PaulS: OK. So, it appears that the library is bit-banging SPI, since the pins in the example are not the (hardware) SPI pins on any Arduino.

So, don't use the hardware SPI pins.

I find it difficult to understand why anyone would name the chip select pin SCK, the clock pin MISO, the master out/slave in pin MOSI, and the master in/slave out pin SS. That example leaves a lot to be desired.

Thank you, it works. I can scan tags and send message to the server. I didn't hear about bit-banging before today ^^' Thank you again

Hello Ganoninc, could you be so kind and post the final worikng code? I have the similar problem. Thank you"

petamoravec: Hello Ganoninc, could you be so kind and post the final worikng code? I have the similar problem. Thank you"

Did you see "Guest" under Ganoninc's name? That means that he/she is no longer a registered user, so is highly unlikely to see your post.

I did, but i hope that there could be some e-mail notification (when there is new post in auhor's topic)... Or it is not? :-(