Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <SPI.h>
- #include <Adafruit_PN532.h>
- #include <stdint.h>
- #include <stdbool.h>
- #if defined(ARDUINO_ARCH_SAMD)
- #define Serial SerialUSB
- #endif
- #define PN532_SCK (14) //D5
- #define PN532_MOSI (13) //D7
- #define PN532_SS (4) //D2
- #define PN532_MISO (12) //d6
- #define PN532_IRQ (2)
- #define PN532_RESET (3) // Not connected by default on the NFC Shield
- Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
- uint8_t datacredit1[16];
- uint8_t datacredit2[16];
- uint8_t datacont[16];
- uint8_t keyb[6];
- //const String datablock1g1 = (const uint8_t[]) {0x00, 0xC0, 0x06, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0 };
- const char* datablock1g3 = "0xAA, 0xAA, 0xAA, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0";
- void setup(void) {
- delay(1000);
- #ifndef ESP8266
- while (!Serial); // for Leonardo/Micro/Zero
- #endif
- Serial.begin(115200);
- Serial.println("### OMEGA NFC v1.0 Scrittura Credito ###");
- pinMode(LED_BUILTIN, OUTPUT);
- delay(500);
- nfc.begin();
- delay(500);
- uint32_t versiondata = nfc.getFirmwareVersion();
- if (! versiondata) {
- Serial.print("Lettore non rilevato");
- while (1); // halt
- }
- // Got ok data, print it out!
- Serial.print("Rilevato chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX);
- Serial.print("Firmware NFC ver. "); Serial.print((versiondata >> 16) & 0xFF, DEC);
- Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC);
- nfc.setPassiveActivationRetries(0xFF);
- nfc.SAMConfig();
- //Serial.println("Attesa Chiave: ");
- Serial.println("############################# ATTESA CHIAVE #################################");
- }
- //#####################################################
- void loop(void) {
- boolean success;
- uint8_t autenticazione_ok;
- uint8_t scrittura_ok;
- 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[0], &uidLength);
- if (success) {
- Serial.println("#############################################################################");
- //nfc.PrintHex(uid, uidLength);
- Serial.println("Chiave rilevata!");
- Serial.print("Lunghezza UID : ");
- Serial.print(uidLength, DEC);
- Serial.println(" bytes");
- Serial.print("Valore UID: ");
- char totUid;
- String convUid;
- for (uint8_t i = 0; i < uidLength; i++)
- {
- // Serial.print(" 0x");
- Serial.print(uid[i], HEX);
- totUid = (uid[i]);
- convUid += String(totUid, HEX); // VAR CON UID CONVERTITO
- }
- // Serial.println("UID Tot:" + convUid);
- // Wait 1 second before continuing
- delay(2000);
- Serial.println("");
- //######################################################################
- if (uidLength == 4) {
- autenticazione_ok = 0;
- Serial.println("UID Valido!");
- // uint8_t keyb[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
- //########################## PELLEGRINI ################################
- if (convUid == "93addb69") {
- Serial.println("## Chiave Gv Pellegrini! ##");
- // memcpy(datacredit1, (const uint8_t[]) {0x00, 0xC0, 0x06, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0 }, sizeof datacredit1);
- memcpy(keyb, (const uint8_t[]) {0xA8, 0x8E, 0x0A, 0x53, 0xE9, 0x28}, sizeof keyb); //chiave b del settore credito
- autenticazione_ok = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 9, 2, keyb);
- if (autenticazione_ok) {
- Serial.println("Auth OK");
- long time = (millis()/1000);
- //########################## Salva dati in memoria ################################
- Serial.println("Scrittura Credito 17.45");
- // memcpy(datacredit1, datablock1g3, sizeof datacredit1); //blocco da scrivere (credito old)
- memcpy(datacredit1, (const uint8_t[]) {0x00, 0xC0, 0x06, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0 }, sizeof datacredit1); //blocco da scrivere (credito new)
- memcpy(datacredit2, (const uint8_t[]) {0x00, 0xD1, 0x06, 0xD7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA1 }, sizeof datacredit2); //blocco da scrivere (credito new)
- memcpy(datacont, (const uint8_t[]) {0xAA, 0xA1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, sizeof datacont); //blocco da scrivere (contatore)
- //########################## Scrivi su chiave ######################################
- Serial.println("Scrittura Credito OLD OK");
- scrittura_ok = nfc.mifareclassic_WriteDataBlock (8, datacredit1);
- Serial.println("Scrittura Credito NEW OK");
- scrittura_ok = nfc.mifareclassic_WriteDataBlock (9, datacredit2);
- Serial.println("Scrittura Contatore OK");
- scrittura_ok = nfc.mifareclassic_WriteDataBlock (10, datacont);
- //######################### LAMPEGGIO ##############################
- wrledok();
- }
- }
- //########################## PELLEGRINI END ################################
- //########################## YYB ################################
- else if (convUid == "3e69a045") {
- Serial.println("## Chiave Gv YYB! ##");
- memcpy(keyb, (const uint8_t[]) {0xEB, 0xEF, 0xFD, 0x86, 0x91, 0xB9}, sizeof keyb); //chiave b del settore credito
- autenticazione_ok = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 6, 1, keyb);
- if (autenticazione_ok) {
- Serial.println("Auth OK");
- long time = (millis()/1000);
- //########################## Salva dati in memoria ################################
- Serial.println("Scrittura Credito 10.00");
- memcpy(datacredit1, (const uint8_t[]) {0x00, 0x03, 0xE8, 0x01, 0x07, 0xD9, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, sizeof datacredit1); //blocco da scrivere (credito)
- Serial.println("Scrittura Credito YYB OK");
- scrittura_ok = nfc.mifareclassic_WriteDataBlock (6, datacredit1);
- //######################### LAMPEGGIO ##############################
- wrledok();
- }
- //########################## YYB END ################################
- }
- else
- {
- ledko();
- }
- }
- }
- } //loop
- // FUNZIONI #############################
- void wrledok () {
- digitalWrite(LED_BUILTIN, HIGH); //tre lampeggi veloci avvenuta scrittura
- delay(100);
- digitalWrite(LED_BUILTIN, LOW);
- delay(100);
- digitalWrite(LED_BUILTIN, HIGH);
- delay(100);
- digitalWrite(LED_BUILTIN, LOW);
- delay(100);
- digitalWrite(LED_BUILTIN, HIGH);
- delay(100);
- digitalWrite(LED_BUILTIN, LOW);
- Serial.println("Scrittura Eseguita .. attesa 5 sec. prima della prossima scrittura!");
- Serial.println("#############################################################################");
- Serial.println("");
- }
- void ledko () {
- // PN532 probably timed out waiting for a card
- Serial.println("Chiave non riconosciuta");
- Serial.println("");
- Serial.println("#############################################################################");
- digitalWrite(LED_BUILTIN, HIGH); //due lampeggi lenti autenticazione errata
- delay(500);
- digitalWrite(LED_BUILTIN, LOW);
- delay(500);
- digitalWrite(LED_BUILTIN, HIGH);
- delay(500);
- digitalWrite(LED_BUILTIN, LOW);
- delay(5000);
- //Serial.println("Timed out waiting for a card");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement