Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Anon's Arduino NFC Project
- ====Contents====
- 1.1 - Design
- 1.2 - Parts used
- 1.3 - Source code
- 1.3.1 - Tag setup
- 1.3.2 - Tag write
- 1.3.3 - Tag read
- ================
- ===1.1===
- 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.
- ===1.2===
- -Arduino Uno
- -Adafruit PN532_I2C NFC shield
- -Mifare classic 1k tags
- -USB A-B cable
- -Windows 7/10 PC
- ===1.3===
- ===1.3.1===
- NFC tag Setup - formats new tags into the NDEF standard
- [Section Complete]
- #include <PN532.h>
- #include <PN532_I2C.h>
- #include <NfcAdapter.h>
- PN532_I2C pn532_i2c(Wire);
- NfcAdapter nfc = NfcAdapter(pn532_i2c);
- void setup(){
- Serial.begin(9600);
- Serial.println("Mifare Classic tag formatter");
- nfc.begin();
- } //End of setup
- void loop(){
- Serial.println("\nPlace and hold a MiFare Classic tag on the reader...");
- if (nfc.tagPresent()){
- bool success = nfc.format(); //Use nfc.format() to format into NDEF, and nfc.clean() to restore it back to its original format
- if (success){
- Serial.println("Success!");
- }
- else{
- Serial.println("Fail!");
- }
- }
- delay(2000);
- } //End of loop
- ===1.3.2===
- NFC tag write - Writes desired data into tags
- [Section complete]
- #include <Wire.h>
- #include <PN532.h>
- #include <PN532_I2C.h>
- #include <NfcAdapter.h>
- PN532_I2C pn532_i2c(Wire);
- NfcAdapter nfc = NfcAdapter(pn532_i2c);
- void setup() {
- Serial.begin(9600);
- Serial.println("NFC Tag setup v1.0.0"); //Header
- nfc.begin();
- }//End of setup
- void loop() {
- Serial.println("\nPlace and hold a tag to write...");
- if(nfc.tagPresent()){ //If a tag is present, this function will run
- NdefMessage message = NdefMessage();
- message.addUriRecord("Anon"); //First name
- message.addUriRecord("Anonson"); //Last name
- message.addUriRecord("13.2"); //Tutor
- message.addUriRecord("No lessons"); //Reason
- bool writeSuccess = nfc.write(message); //Boolean variable that stores the condition for writing data to the tag; intended to differentiate from "formatSuccess"
- if (writeSuccess){
- Serial.println("Data write successful!"); //This will display if the write was successful
- }
- else{
- Serial.println("Data write failed!"); //This will display if the write was not successful
- }//End of success function
- }//End of nfc.tagPresent() function
- delay(3000);
- }//End of loop
- ===1.3.3===
- NFC tag read - Reads data from tags and pairs with timestamp, then prepares for it to be logged on the PC
- [Still needs to implement time stamps]
- #include <Wire.h> //Required to interface with I2C devices, in this case the NFC shield
- #include <time.h> //Library used to get the time in order to attach a timestamp for each scan
- #include <PN532.h>
- #include <PN532_I2C.h>
- #include <NfcAdapter.h>
- PN532_I2C pn532_i2c(Wire);
- NfcAdapter nfc = NfcAdapter(pn532_i2c);
- void setup(){
- Serial.begin(9600); //Set the baud rate to 9,600bps
- Serial.println("NFC Sign in/out v.1.0.0"); //Header displayed at the top of the program
- nfc.begin(); //Initialises the NFC module
- }//End of setup
- void loop(){
- Serial.println("\nWaiting for tag...\n");
- if(nfc.tagPresent()){ //If a tag is detected, this function will run
- Serial.println("Tag found!");
- NfcTag tag = nfc.read();
- Serial.println(tag.getTagType()); //Determines the type of tag, e.g. MiFare Classic
- Serial.print("UID - ");
- Serial.println(tag.getUidString()); //Fetches the tag's UID
- if (tag.hasNdefMessage()){ //If the tag contains an NDEF-formatted message, this function will run
- NdefMessage message = tag.getNdefMessage();
- Serial.print("Number of records - ");
- Serial.print(message.getRecordCount()); //Fetches number of stored records
- Serial.println("\n");
- //If more than 1 record is present, the program will run through each one
- int recordCount = message.getRecordCount();
- for (int i = 0; i < recordCount; i++){
- Serial.print("Record #");
- Serial.print(i+1);
- NdefRecord record = message.getRecord(i);
- int payloadLength = record.getPayloadLength();
- byte payload[payloadLength];
- record.getPayload(payload);
- String payloadToString = ""; //Converts the record from Hex to String
- for (int j = 0; j < payloadLength; j++){
- payloadToString += (char)payload[j];
- }//End of payloadToString for loop
- Serial.print(" - ");
- Serial.println(payloadToString);
- }//End of recordCount for loop
- }//End of tag.hasNdefMessage() function
- }//End of nfc.tagPresent() function
- delay(3000);
- }//End of loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement