Advertisement
Guest User

Anon's NFC project

a guest
Jan 2nd, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.04 KB | None | 0 0
  1. Anon's Arduino NFC Project
  2.  
  3. ====Contents====
  4. 1.1 - Design
  5. 1.2 - Parts used
  6. 1.3 - Source code
  7. 1.3.1 - Tag setup
  8. 1.3.2 - Tag write
  9. 1.3.3 - Tag read
  10. ================
  11.  
  12. ===1.1===
  13. Aim of the project is to create an NFC reader that will be used as a sign in/sign out system in which the log of people leaving and entering is written to a log paired with a timestamp; one tap, and you're signed out - another tap, and you're signed back in. All users have a personalized NFC tag that has their name on it. Once the tag is read, a timestamp is fetched and attached to the user's interaction, and saved to a .csv file.
  14.  
  15. ===1.2===
  16. -Arduino Uno
  17. -Adafruit PN532_I2C NFC shield
  18. -Mifare classic 1k tags
  19. -USB A-B cable
  20. -Windows 7/10 PC
  21.  
  22. ===1.3===
  23.  
  24. ===1.3.1===
  25. NFC tag Setup - formats new tags into the NDEF standard
  26. [Section Complete]
  27.  
  28. #include <PN532.h>
  29. #include <PN532_I2C.h>
  30. #include <NfcAdapter.h>
  31.  
  32. PN532_I2C pn532_i2c(Wire);
  33. NfcAdapter nfc = NfcAdapter(pn532_i2c);
  34.  
  35. void setup(){
  36.   Serial.begin(9600);
  37.   Serial.println("Mifare Classic tag formatter");
  38.   nfc.begin();
  39.  
  40. } //End of setup
  41.  
  42. void loop(){
  43.   Serial.println("\nPlace and hold a MiFare Classic tag on the reader...");
  44.  
  45.   if (nfc.tagPresent()){
  46.  
  47.     bool success = nfc.format(); //Use nfc.format() to format into NDEF, and nfc.clean() to restore it back to its original format
  48.     if (success){
  49.       Serial.println("Success!");
  50.     }
  51.     else{
  52.       Serial.println("Fail!");
  53.     }
  54.   }
  55.   delay(2000);
  56. } //End of loop
  57.  
  58. ===1.3.2===
  59. NFC tag write - Writes desired data into tags
  60. [Section complete]
  61.  
  62. #include <Wire.h>
  63.  
  64. #include <PN532.h>
  65. #include <PN532_I2C.h>
  66. #include <NfcAdapter.h>
  67.  
  68. PN532_I2C pn532_i2c(Wire);
  69. NfcAdapter nfc = NfcAdapter(pn532_i2c);
  70.  
  71. void setup() {
  72.   Serial.begin(9600);
  73.   Serial.println("NFC Tag setup v1.0.0"); //Header
  74.   nfc.begin();
  75.  
  76. }//End of setup
  77.  
  78. void loop() {
  79.   Serial.println("\nPlace and hold a tag to write...");
  80.  
  81.   if(nfc.tagPresent()){ //If a tag is present, this function will run
  82.      
  83.      NdefMessage message = NdefMessage();
  84.      message.addUriRecord("Anon"); //First name
  85.      message.addUriRecord("Anonson"); //Last name
  86.      message.addUriRecord("13.2"); //Tutor
  87.      message.addUriRecord("No lessons"); //Reason
  88.      
  89.      
  90.      bool writeSuccess = nfc.write(message); //Boolean variable that stores the condition for writing data to the tag; intended to differentiate from "formatSuccess"
  91.      if (writeSuccess){
  92.       Serial.println("Data write successful!"); //This will display if the write was successful
  93.      }
  94.      else{
  95.       Serial.println("Data write failed!"); //This will display if the write was not successful
  96.      }//End of success function
  97.    
  98.     }//End of nfc.tagPresent() function
  99.     delay(3000);
  100.   }//End of loop
  101.  
  102. ===1.3.3===
  103. NFC tag read - Reads data from tags and pairs with timestamp, then prepares for it to be logged on the PC
  104. [Still needs to implement time stamps]
  105.  
  106. #include <Wire.h> //Required to interface with I2C devices, in this case the NFC shield
  107. #include <time.h> //Library used to get the time in order to attach a timestamp for each scan
  108.  
  109. #include <PN532.h>
  110. #include <PN532_I2C.h>
  111. #include <NfcAdapter.h>
  112.  
  113. PN532_I2C pn532_i2c(Wire);
  114. NfcAdapter nfc = NfcAdapter(pn532_i2c);
  115.  
  116. void setup(){
  117.   Serial.begin(9600); //Set the baud rate to 9,600bps
  118.   Serial.println("NFC Sign in/out v.1.0.0"); //Header displayed at the top of the program
  119.   nfc.begin(); //Initialises the NFC module
  120.  
  121. }//End of setup
  122.  
  123. void loop(){
  124. Serial.println("\nWaiting for tag...\n");
  125.  
  126.   if(nfc.tagPresent()){ //If a tag is detected, this function will run
  127.     Serial.println("Tag found!");
  128.     NfcTag tag = nfc.read();
  129.     Serial.println(tag.getTagType()); //Determines the type of tag, e.g. MiFare Classic
  130.     Serial.print("UID - ");
  131.     Serial.println(tag.getUidString()); //Fetches the tag's UID
  132.  
  133.     if (tag.hasNdefMessage()){ //If the tag contains an NDEF-formatted message, this function will run
  134.       NdefMessage message = tag.getNdefMessage();
  135.       Serial.print("Number of records - ");
  136.       Serial.print(message.getRecordCount()); //Fetches number of stored records
  137.       Serial.println("\n");
  138.  
  139.       //If more than 1 record is present, the program will run through each one
  140.       int recordCount = message.getRecordCount();
  141.       for (int i = 0; i < recordCount; i++){
  142.         Serial.print("Record #");
  143.         Serial.print(i+1);
  144.         NdefRecord record = message.getRecord(i);
  145.  
  146.         int payloadLength = record.getPayloadLength();
  147.         byte payload[payloadLength];
  148.         record.getPayload(payload);
  149.  
  150.         String payloadToString = ""; //Converts the record from Hex to String
  151.         for (int j = 0; j < payloadLength; j++){
  152.           payloadToString += (char)payload[j];
  153.        
  154.         }//End of payloadToString for loop
  155.         Serial.print(" - ");
  156.         Serial.println(payloadToString);
  157.          
  158.       }//End of recordCount for loop  
  159.          
  160.     }//End of tag.hasNdefMessage() function  
  161.      
  162.   }//End of nfc.tagPresent() function  
  163.   delay(3000);
  164. }//End of loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement