Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <SPI.h>
  3. #include <Adafruit_PN532.h>
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. #if defined(ARDUINO_ARCH_SAMD)
  7. #define Serial SerialUSB
  8. #endif
  9. #define PN532_SCK (14) //D5
  10. #define PN532_MOSI (13) //D7
  11. #define PN532_SS (4) //D2
  12. #define PN532_MISO (12) //d6
  13. #define PN532_IRQ (2)
  14. #define PN532_RESET (3) // Not connected by default on the NFC Shield
  15. Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
  16.  
  17.  
  18. uint8_t datacredit1[16];
  19. uint8_t datacredit2[16];
  20. uint8_t datacont[16];
  21. uint8_t keyb[6];
  22. //const String datablock1g1 = (const uint8_t[]) {0x00, 0xC0, 0x06, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0 };
  23. const char* datablock1g3 = "0xAA, 0xAA, 0xAA, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0";
  24.  
  25. void setup(void) {
  26. delay(1000);
  27.  
  28. #ifndef ESP8266
  29. while (!Serial); // for Leonardo/Micro/Zero
  30. #endif
  31. Serial.begin(115200);
  32.  
  33. Serial.println("### OMEGA NFC v1.0 Scrittura Credito ###");
  34. pinMode(LED_BUILTIN, OUTPUT);
  35. delay(500);
  36. nfc.begin();
  37. delay(500);
  38. uint32_t versiondata = nfc.getFirmwareVersion();
  39. if (! versiondata) {
  40. Serial.print("Lettore non rilevato");
  41. while (1); // halt
  42. }
  43.  
  44. // Got ok data, print it out!
  45. Serial.print("Rilevato chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX);
  46. Serial.print("Firmware NFC ver. "); Serial.print((versiondata >> 16) & 0xFF, DEC);
  47. Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC);
  48. nfc.setPassiveActivationRetries(0xFF);
  49. nfc.SAMConfig();
  50.  
  51. //Serial.println("Attesa Chiave: ");
  52. Serial.println("############################# ATTESA CHIAVE #################################");
  53. }
  54.  
  55.  
  56. //#####################################################
  57.  
  58.  
  59.  
  60.  
  61. void loop(void) {
  62.  
  63. boolean success;
  64. uint8_t autenticazione_ok;
  65. uint8_t scrittura_ok;
  66. uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
  67. uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  68.  
  69.  
  70. success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
  71.  
  72. if (success) {
  73. Serial.println("#############################################################################");
  74. //nfc.PrintHex(uid, uidLength);
  75. Serial.println("Chiave rilevata!");
  76. Serial.print("Lunghezza UID : ");
  77. Serial.print(uidLength, DEC);
  78. Serial.println(" bytes");
  79. Serial.print("Valore UID: ");
  80. char totUid;
  81. String convUid;
  82. for (uint8_t i = 0; i < uidLength; i++)
  83. {
  84. // Serial.print(" 0x");
  85. Serial.print(uid[i], HEX);
  86. totUid = (uid[i]);
  87. convUid += String(totUid, HEX); // VAR CON UID CONVERTITO
  88. }
  89. // Serial.println("UID Tot:" + convUid);
  90. // Wait 1 second before continuing
  91. delay(2000);
  92. Serial.println("");
  93.  
  94.  
  95. //######################################################################
  96.  
  97.  
  98. if (uidLength == 4) {
  99. autenticazione_ok = 0;
  100. Serial.println("UID Valido!");
  101. // uint8_t keyb[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
  102. //########################## PELLEGRINI ################################
  103. if (convUid == "93addb69") {
  104. Serial.println("## Chiave Gv Pellegrini! ##");
  105. // memcpy(datacredit1, (const uint8_t[]) {0x00, 0xC0, 0x06, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0 }, sizeof datacredit1);
  106. memcpy(keyb, (const uint8_t[]) {0xA8, 0x8E, 0x0A, 0x53, 0xE9, 0x28}, sizeof keyb); //chiave b del settore credito
  107. autenticazione_ok = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 9, 2, keyb);
  108.  
  109.  
  110. if (autenticazione_ok) {
  111. Serial.println("Auth OK");
  112. long time = (millis()/1000);
  113. //########################## Salva dati in memoria ################################
  114. Serial.println("Scrittura Credito 17.45");
  115. // memcpy(datacredit1, datablock1g3, sizeof datacredit1); //blocco da scrivere (credito old)
  116. 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)
  117. 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)
  118. 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)
  119.  
  120. //########################## Scrivi su chiave ######################################
  121. Serial.println("Scrittura Credito OLD OK");
  122. scrittura_ok = nfc.mifareclassic_WriteDataBlock (8, datacredit1);
  123. Serial.println("Scrittura Credito NEW OK");
  124. scrittura_ok = nfc.mifareclassic_WriteDataBlock (9, datacredit2);
  125. Serial.println("Scrittura Contatore OK");
  126. scrittura_ok = nfc.mifareclassic_WriteDataBlock (10, datacont);
  127. //######################### LAMPEGGIO ##############################
  128. wrledok();
  129. }
  130. }
  131. //########################## PELLEGRINI END ################################
  132.  
  133.  
  134. //########################## YYB ################################
  135. else if (convUid == "3e69a045") {
  136. Serial.println("## Chiave Gv YYB! ##");
  137. memcpy(keyb, (const uint8_t[]) {0xEB, 0xEF, 0xFD, 0x86, 0x91, 0xB9}, sizeof keyb); //chiave b del settore credito
  138. autenticazione_ok = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 6, 1, keyb);
  139. if (autenticazione_ok) {
  140. Serial.println("Auth OK");
  141. long time = (millis()/1000);
  142. //########################## Salva dati in memoria ################################
  143. Serial.println("Scrittura Credito 10.00");
  144. 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)
  145. Serial.println("Scrittura Credito YYB OK");
  146. scrittura_ok = nfc.mifareclassic_WriteDataBlock (6, datacredit1);
  147. //######################### LAMPEGGIO ##############################
  148. wrledok();
  149. }
  150.  
  151. //########################## YYB END ################################
  152.  
  153.  
  154. }
  155.  
  156. else
  157. {
  158. ledko();
  159. }
  160.  
  161.  
  162. }
  163. }
  164. } //loop
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. // FUNZIONI #############################
  173.  
  174.  
  175. void wrledok () {
  176. digitalWrite(LED_BUILTIN, HIGH); //tre lampeggi veloci avvenuta scrittura
  177. delay(100);
  178. digitalWrite(LED_BUILTIN, LOW);
  179. delay(100);
  180. digitalWrite(LED_BUILTIN, HIGH);
  181. delay(100);
  182. digitalWrite(LED_BUILTIN, LOW);
  183. delay(100);
  184. digitalWrite(LED_BUILTIN, HIGH);
  185. delay(100);
  186. digitalWrite(LED_BUILTIN, LOW);
  187.  
  188. Serial.println("Scrittura Eseguita .. attesa 5 sec. prima della prossima scrittura!");
  189. Serial.println("#############################################################################");
  190. Serial.println("");
  191.  
  192. }
  193.  
  194.  
  195.  
  196.  
  197. void ledko () {
  198. // PN532 probably timed out waiting for a card
  199. Serial.println("Chiave non riconosciuta");
  200. Serial.println("");
  201. Serial.println("#############################################################################");
  202. digitalWrite(LED_BUILTIN, HIGH); //due lampeggi lenti autenticazione errata
  203. delay(500);
  204. digitalWrite(LED_BUILTIN, LOW);
  205. delay(500);
  206. digitalWrite(LED_BUILTIN, HIGH);
  207. delay(500);
  208. digitalWrite(LED_BUILTIN, LOW);
  209. delay(5000);
  210. //Serial.println("Timed out waiting for a card");
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement