Hi guys,
I'm trying to interface with 9 PN532 NFC readers via SPI. I connected all power, MISO and MOSI lines together and assigned each reader a seperate CS pin.
I'm using the NDEF library by don which implements Yihui Xiong's PN532 Library.
When I try to initialize each reader on its pin seperately, the sketch operates as intended.
Up to four readers works correctly too.
Only when the fifth reader initializes, the arduino crashes.
this is my serial output:
Initializing NFC Readers
Init Board 0...
Found chip PN532
Firmware ver. 1.6
Init Board 1...
Found chip PN532
Firmware ver. 1.6
Init Board 2...
Found chip PN532
Firmware ver. 1.6
Init Board 3...
Found chip PN532
Firmware ver. 1.6
Init Board 4...
Didn't find PN53x board
Is this a bug with the NDEF or PN532 libraries? It seems to be struggling with more than 5 different instances of NfcAdapter... Am I interpreting this correctly?
This is my full code:
#include <SPI.h>
#include <PN532_SPI.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_SPI spi0(SPI, A5); NfcAdapter nfc0 = NfcAdapter(spi0);
PN532_SPI spi1(SPI, A4); NfcAdapter nfc1 = NfcAdapter(spi1);
PN532_SPI spi2(SPI, A3); NfcAdapter nfc2 = NfcAdapter(spi2);
PN532_SPI spi3(SPI, A2); NfcAdapter nfc3 = NfcAdapter(spi3);
PN532_SPI spi4(SPI, A1); NfcAdapter nfc4 = NfcAdapter(spi4);
PN532_SPI spi5(SPI, A0); NfcAdapter nfc5 = NfcAdapter(spi5);
PN532_SPI spi6(SPI, 3 ); NfcAdapter nfc6 = NfcAdapter(spi6);
PN532_SPI spi7(SPI, 4 ); NfcAdapter nfc7 = NfcAdapter(spi7);
PN532_SPI spi8(SPI, 5 ); NfcAdapter nfc8 = NfcAdapter(spi8);
#define NUM_ADAPTERS 9
NfcAdapter *nfc[NUM_ADAPTERS] = {&nfc0, &nfc1, &nfc2, &nfc3, &nfc4, &nfc5, &nfc6, &nfc7, &nfc8};
String data[NUM_ADAPTERS];
boolean dataUpdate = false;
void setup() {
Serial.begin(115200);
//while(!Serial){}
Serial.println("Initializing NFC Readers");
for ( int i = 0; i < NUM_ADAPTERS; i++ ) {
Serial.println("Init Board " + String(i) + "...");
nfc[i]->begin();
//delay(500);
}
}
void loop() {
for ( int i = 0; i < NUM_ADAPTERS; i++ ) {
if (nfc[i]->tagPresent()) {
Serial.println("Tag present!");
NfcTag tag = nfc[i]->read();
if (tag.hasNdefMessage()) {
NdefMessage message = tag.getNdefMessage();
String messageString = messageAsString(message);
if ( data[i] != messageString ) {
data[i] = messageString;
dataUpdate = true;
}
}
} else {
if ( data[i] != "" ) {
data[i] = "";
dataUpdate = true;
}
}
}
if (dataUpdate) {
Serial.print("New Data: ");
for ( int i = 0; i < NUM_ADAPTERS; i++ ) {
Serial.print("[");
Serial.print(data[i]);
if (i < NUM_ADAPTERS - 1) Serial.print("]");
}
Serial.println("]");
dataUpdate = false;
}
}
String getJSONString() {
String jsonString = "{";
for ( int i = 0; i < NUM_ADAPTERS; i++ ) {
jsonString += ("\"sensor" + String(i) + "\":");
jsonString += ("\"" + data[i] + "\"");
if (i < NUM_ADAPTERS - 1) jsonString += ",";
}
jsonString += "}";
return jsonString;
}
String messageAsString(NdefMessage message ) {
// cycle through the records, printing some info from each
if ( message.getRecordCount() > 0 ) {
NdefRecord record = message.getRecord(0);
int payloadLength = record.getPayloadLength();
byte payload[payloadLength];
record.getPayload(payload);
String payloadAsString = "";
for (int c = 0; c < payloadLength; c++) {
payloadAsString += (char)payload[c];
}
return payloadAsString;
}
return "";
}