zykes

Untitled

Feb 26th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. /**************************************************************************/
  2. /*!
  3. This example will attempt to connect to an ISO14443A
  4. card or tag and retrieve some basic information about it
  5. that can be used to determine what type of card it is.
  6.  
  7. Note that you need the baud rate to be 115200 because we need to print
  8. out the data and read from the card at the same time!
  9.  
  10. To enable debug message, define DEBUG in PN532/PN532_debug.h
  11.  
  12. */
  13. /**************************************************************************/
  14.  
  15. #include <SPI.h>
  16. #include <PN532_SPI.h>
  17. #include "PN532.h"
  18.  
  19. const int TE_PIN = PD2;
  20. const int BUZZER_PIN = PD3;
  21.  
  22. // Var to store the last read uid in so we dont spam MQTT
  23. int lastCardUid[] = { 0, 0, 0, 0, 0, 0, 0 };
  24. unsigned long lastRead = 0;
  25. unsigned long lastReadSuccess = 0;
  26.  
  27. // Track durations
  28. unsigned long buzzerAt = 0;
  29. int buzzerDuration = 500;
  30.  
  31. PN532_SPI pn532spi(SPI, SS);
  32. PN532 nfc(pn532spi);
  33.  
  34. void buzzerSet(int duration = 600) {
  35. digitalWrite(BUZZER_PIN, HIGH);
  36. buzzerAt = millis();
  37. buzzerDuration = duration;
  38. }
  39.  
  40. void buzzerTimeout() {
  41. if ((millis() - buzzerAt) > buzzerDuration) {
  42. digitalWrite(BUZZER_PIN, LOW);
  43. }
  44. }
  45.  
  46. void setup(void) {
  47. Serial.begin(9600);
  48. Serial.println("Hello!");
  49.  
  50. nfc.begin();
  51.  
  52. uint32_t versiondata = nfc.getFirmwareVersion();
  53. if (! versiondata) {
  54. Serial.print("Didn't find PN53x board");
  55. while (1); // halt
  56. }
  57.  
  58. // Got ok data, print it out!
  59. Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
  60. Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
  61. Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  62.  
  63. // Set the max number of retry attempts to read from a card
  64. // This prevents us from waiting forever for a card, which is
  65. // the default behaviour of the PN532.
  66. nfc.setPassiveActivationRetries(0xFF);
  67.  
  68. // configure board to read RFID tags
  69. nfc.SAMConfig();
  70.  
  71. Serial.println("Waiting for an ISO14443A card");
  72. }
  73.  
  74. void getUidChars(uint8_t uid[], uint8_t uidLength, char* result, const unsigned result_length) {
  75. char part[] = {};
  76. for (int i = 0; i < uidLength; i++) {
  77. sprintf(part, "%02x", uid[i]);
  78. strcat(result, part);
  79. }
  80. }
  81.  
  82. void readRFID() {
  83. uint8_t success;
  84. uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
  85. uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  86.  
  87. // Wait for an ISO14443A type cards (Mifare, etc.). When one is found
  88. // 'uid' will be populated with the UID, and uidLength will indicate
  89. // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  90. success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  91.  
  92. if (success) {
  93. /*
  94. Is it the same card that was read then we skip over the rest so we dont
  95. to verify it below
  96. */
  97. int isSame = memcmp(lastCardUid, uid, uidLength);
  98.  
  99. if (isSame == 0) {
  100. Serial.println("Same card detected, skipping");
  101. return;
  102. };
  103.  
  104. memcpy(lastCardUid, uid, uidLength);
  105.  
  106. lastReadSuccess = millis();
  107.  
  108. // Display some basic information about the card
  109. Serial.println("Found an ISO14443A card");
  110. Serial.print(" UID Length: ");
  111. Serial.print(uidLength, DEC);
  112. Serial.println(" bytes");
  113. Serial.print(" UID Value: ");
  114. nfc.PrintHex(uid, uidLength);
  115.  
  116. if (uidLength == 4) {
  117. // We probably have a Mifare Classic card ...
  118.  
  119. char id[100] = {};
  120. int idLen = 0;
  121. getUidChars(uid, uidLength, id, idLen);
  122.  
  123. //String credId = String(id);
  124. }
  125.  
  126. Serial.println("");
  127. return;
  128.  
  129. // In case of there wasn't a successfull attempt then we null the lastCardUid
  130. if ((millis() - lastReadSuccess) > 2000) {
  131. lastCardUid[0] = 0;
  132. }
  133. }
  134. }
  135.  
  136. void loop() {
  137. readRFID();
  138. buzzerTimeout();
  139. }
Advertisement
Add Comment
Please, Sign In to add comment