From f07350abf8f29b7a2c37a128036cc993a4927cfb Mon Sep 17 00:00:00 2001 From: Eyuep Sueyruege Date: Tue, 12 Dec 2023 20:52:34 +0100 Subject: [PATCH] Add src/arduino/writeBytes.ino --- src/arduino/writeBytes.ino | 79 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/arduino/writeBytes.ino diff --git a/src/arduino/writeBytes.ino b/src/arduino/writeBytes.ino new file mode 100644 index 0000000..123806e --- /dev/null +++ b/src/arduino/writeBytes.ino @@ -0,0 +1,79 @@ +#include +#include +#include + +#define PN532_IRQ (2) +#define PN532_RESET (3) // Not connected by default on the NFC Shield + +Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); + +void setup(void) { + Serial.begin(115200); + while (!Serial) delay(10); + + Serial.println("Hello!"); + + nfc.begin(); + + uint32_t versiondata = nfc.getFirmwareVersion(); + if (!versiondata) { + Serial.print("Didn't find PN53x board"); + while (1); + } + + 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); + + Serial.println("Waiting for an ISO14443A Card ..."); +} + +void loop(void) { + uint8_t success; + uint8_t uid[] = {0, 0, 0, 0, 0, 0, 0}; + uint8_t uidLength; + + success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); + + if (success) { + Serial.println("Found an ISO14443A card"); + Serial.print(" UID Length: "); + Serial.print(uidLength, DEC); + Serial.println(" bytes"); + Serial.print(" UID Value: "); + for (uint8_t i = 0; i < uidLength; i++) { + Serial.print(uid[i], HEX); + if (i + 1 < uidLength) { + Serial.print(", "); + } + } + Serial.println(""); + + // Authenticate to write to Block 7 + uint8_t keya[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 7, 0, keya); + + if (success) { + Serial.println("Authentication successful. Writing to Block 7..."); + + // Data to write (replace with your own data) + uint8_t data[16] = {0x1C, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + + // Write to Block 7 + success = nfc.mifareclassic_WriteDataBlock(7, data); + + if (success) { + Serial.println("Write successful"); + } else { + Serial.println("Write failed"); + } + } else { + Serial.println("Authentication failed"); + } + + delay(1000); + } +} \ No newline at end of file