adafruit / Adafruit-PN532

Arduino library for SPI and I2C access to the PN532 RFID/Near Field Communication chip
Other
421 stars 265 forks source link

Problems combining Adafruit_PN532.h V1.2.2 (SPI) with Ethernet.h V2.0 #98

Open DavidMR91 opened 2 years ago

DavidMR91 commented 2 years ago

define PN532_MISO (50)

define PN532_MOSI (51)

define PN532_SCK (52)

define PN532_SS (34)

//#define PN532_RESET (3) // Not connected by default on the NFC Shield

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC Address IPAddress ip(192,168,1,5); //arduino IP EthernetClient client; const int ledPin = LED_BUILTIN; String UID_CARD; String attendance_status; String user_card; Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS); //Adafruit_PN532 nfc(PN532_SS_1);

void setup(void) { pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); Serial.begin(250000); while (!Serial) delay(10); // for Leonardo/Micro/Zero nfc.begin(); Serial.println("Mesin Absen Gabut"); delay(2000); uint32_t versiondata = nfc.getFirmwareVersion(); if (! versiondata) { Serial.print("Didn't find PN53x board"); while (1); // halt } // Got ok data, print it out! Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);

// configure board to read RFID tags nfc.SAMConfig(); Ethernet.begin(mac, ip); Serial.print("Local IP: "); Serial.println(Ethernet.localIP()); Serial.println("Waiting for an ISO14443A Card ..."); }

void loop(void) { uint8_t success; uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

if (success) { // Display some basic information about the card Serial.println("Found an ISO14443A card"); Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); Serial.print(" UID Value: "); nfc.PrintHex(uid, uidLength); Serial.println("");

uint32_t szUid;
String uidData;
for (szUid=0; szUid < uidLength; szUid++){
      uidData += String(uid[szUid]&0xFF, HEX);
}
uidData.toUpperCase(); //biarhuruf besar
Serial.println(uidData);
UID_CARD =uidData;

if (uidLength == 4){
  //Serial.println("Seems to be a Mifare Classic card (4 byte UID)");   
  //Data Nama disimpan di blok 4 data Status di blok 5
  Serial.println("Trying to authenticate block 4 with default KEYA value");
  uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

  //baca blok4
  success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);

  if (success){
    Serial.println("Blocks 4) has been authenticated");
    uint8_t data[16];
    // Try to read the contents of block 4
    success = nfc.mifareclassic_ReadDataBlock(4, data);

    if (success){
      // Data seems to have been read ... spit it out
      Serial.println("Reading Block 4:");
      nfc.PrintHexChar(data, 16);
      Serial.println("");

      uint32_t szPos;
      String phrase;
      for (szPos=0; szPos < 16; szPos++){
            phrase += (char)data[szPos];
      }
      Serial.print("User: ");
      Serial.println(phrase);//Nama pengguna
      user_card=phrase;
      //user_card += "";
    }
    else{
      Serial.println("Ooops ... unable to read the requested block.  Try another key?");
    }
  }
  else{
    Serial.println("Ooops ... authentication failed: Try another key?");
  }

  //baca blok5
  success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 5, 0, keya);
  if (success){
    Serial.println("Blocks 5) has been authenticated");
    uint8_t data1[16];
    // Try to read the contents of block 5
    success = nfc.mifareclassic_ReadDataBlock(5, data1);

    if (success){
      // Data seems to have been read ... spit it out
      Serial.println("Reading Block 5:");
      nfc.PrintHexChar(data1, 16);
      Serial.println("");

      uint32_t szPos;
      String phrase;
      for (szPos=0; szPos < 16; szPos++){
            phrase += (char)data1[szPos];
      }

      Serial.println(phrase);
      if ((phrase == "IN") || (phrase == "START")){
          digitalWrite(ledPin, HIGH);
          memcpy(data1, (const uint8_t[]){ 'O', 'U', 'T', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, sizeof data1);
          success = nfc.mifareclassic_WriteDataBlock (5, data1);
          attendance_status = "OUT";
          delay(200);
          digitalWrite(ledPin, LOW);
            Serial.print("Local IP: ");
            Serial.println(Ethernet.localIP());

       }else if (phrase == "OUT"){
          digitalWrite(ledPin, HIGH);
          memcpy(data1, (const uint8_t[]){ 'I', 'N', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, sizeof data1);
          success = nfc.mifareclassic_WriteDataBlock (5, data1);
          attendance_status = "IN";
          delay(200);
          digitalWrite(ledPin, LOW);
            Serial.print("Local IP: ");
            Serial.println(Ethernet.localIP());
       }else{
        Serial.println("invalid card");
       }

      // Wait a bit before reading the card again
      delay(4000);
    }
    else{
      Serial.println("Ooops ... unable to read the requested block.  Try another key?");
    }
  }
  else{
    Serial.println("Ooops ... authentication failed: Try another key?");
  }
}

if (uidLength == 7){
  Serial.println("Kartu anda ilegal, colongan ya?");
}

} }

caternuson commented 1 year ago
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
//Adafruit_PN532 nfc(PN532_SS_1);

Which one is being used for the PN532? Since they are sharing the SPI bus, use hardware SPI with the PN532.

Also make sure the Ethernet library is able to share the SPI bus and manages its chip select pin correctly.