├── rfid-data-storage.ino └── .gitkeep ├── rfid-transaction.ino ├── .gitignore └── rfid-transaction │ └── rfid-transaction.ino │ ├── data-store │ └── transactions.txt │ ├── readme.md │ ├── saveTransactions.py │ └── rfid-transaction.ino.ino ├── secure-rfid.ino ├── tags.txt ├── readme.md ├── authentication.py └── secure-rfid.ino.ino ├── writing.ino └── writing │ ├── readme.md │ └── writing.ino ├── Screenshot from 2023-11-02 18-52-51.png ├── read-memory-map.ino ├── readme.md └── read-memory-map.ino ├── .gitignore ├── reading-rfid.ino └── reading-rfid │ └── reading-rfid.ino ├── README.md └── LICENSE /rfid-data-storage.ino/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /rfid-transaction.ino/.gitignore: -------------------------------------------------------------------------------- 1 | *.txt -------------------------------------------------------------------------------- /secure-rfid.ino/tags.txt: -------------------------------------------------------------------------------- 1 | 4B 77 D0 14 2 | E6 77 65 AC -------------------------------------------------------------------------------- /writing.ino/writing/readme.md: -------------------------------------------------------------------------------- 1 | The goal of this project is to be able to write to RFID card. 2 | -------------------------------------------------------------------------------- /Screenshot from 2023-11-02 18-52-51.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pacifiquem/RFID/HEAD/Screenshot from 2023-11-02 18-52-51.png -------------------------------------------------------------------------------- /read-memory-map.ino/readme.md: -------------------------------------------------------------------------------- 1 | Goal of this project is to be able to read-memory-map and understand it.
2 | Also to get used to key terms like `blocks`, `sectors` and how informations are stored on a card. 3 | -------------------------------------------------------------------------------- /secure-rfid.ino/readme.md: -------------------------------------------------------------------------------- 1 | # Secure RFID 2 | 3 | Goal of this project is to authenticate a card, when we have tags's uuid in our database (File in version 1) the the card is authorized otherwise it's not. 4 | 5 | In v2 we will use ``sqlite`` database and it's comming soon. -------------------------------------------------------------------------------- /rfid-transaction.ino/rfid-transaction/rfid-transaction.ino/data-store/transactions.txt: -------------------------------------------------------------------------------- 1 | Transaction completed successfully. Mode used is `Points` and paid amount: 1 2 | Transaction completed successfully. Mode used is `Points` and paid amount: 0 3 | Transaction completed successfully. Mode used is `Points` and paid amount: 90 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | *.vscode -------------------------------------------------------------------------------- /rfid-transaction.ino/rfid-transaction/rfid-transaction.ino/readme.md: -------------------------------------------------------------------------------- 1 | ## RFID Transaction Management System 2 | 3 | This project is an RFID Transaction Management System implemented using an Arduino and an RFID module. It allows users to perform transactions using either money or points. The system reads the card, authenticates, deducts the desired amount, and updates the card's data. The remaining money and points are displayed, and a buzzer sound indicates a successful transaction. The code is written in C++ and utilizes the MFRC522 library for RFID functionality. This system provides a simple and efficient solution for managing transactions using RFID technology. 4 | 5 | NB: `In this version we are using SD cards as our data store.` 6 | -------------------------------------------------------------------------------- /read-memory-map.ino/read-memory-map.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define RST_PIN 9 4 | #define SS_PIN 10 5 | MFRC522 mfrc522(SS_PIN, RST_PIN); 6 | void setup() { 7 | Serial.begin(9600); 8 | while (!Serial); 9 | SPI.begin(); 10 | //Enhance the MFRC522 Receiver Gain to maximum value of some 48 dB 11 | mfrc522.PCD_SetRegisterBitMask(mfrc522.RFCfgReg, (0x07<<4)); 12 | mfrc522.PCD_Init(); 13 | delay(4); 14 | mfrc522.PCD_DumpVersionToSerial(); 15 | Serial.println(F("DISPLAYING UID, SAK, TYPE, AND DATA BLOCKS:")); 16 | } 17 | void loop(){ 18 | if(!mfrc522.PICC_IsNewCardPresent()){ 19 | return; 20 | } 21 | if(!mfrc522.PICC_ReadCardSerial()){ 22 | return; 23 | } 24 | /* 25 | Dump debug info about the card. Worry not. PICC_HaltA() will be automatically called at the end. 26 | */ 27 | mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); 28 | } -------------------------------------------------------------------------------- /rfid-transaction.ino/rfid-transaction/rfid-transaction.ino/saveTransactions.py: -------------------------------------------------------------------------------- 1 | import serial 2 | 3 | # Open the serial connection with Arduino 4 | ser = serial.Serial('COM21', 9600) # Put your port here. 5 | 6 | # Open the file to save the transactions 7 | file = open('./data-store/transactions.txt', 'a') 8 | 9 | while True: 10 | # Wait for the Arduino to send transaction information or prompt 11 | data = ser.readline().decode().strip() 12 | 13 | if data.startswith("Transaction completed successfully."): 14 | # Save the transaction in the file 15 | transaction = data 16 | file.write(transaction + '\n') 17 | file.flush() # Ensure data is written immediately to the file 18 | 19 | # Print the transaction 20 | print('Transaction:', transaction) 21 | 22 | elif data.startswith("Enter"): 23 | # Prompt the user to enter a response 24 | response = input(data) 25 | 26 | # Send the response to Arduino 27 | ser.write(response.encode()) 28 | # Close the file and serial connection 29 | file.close() 30 | ser.close() 31 | -------------------------------------------------------------------------------- /secure-rfid.ino/authentication.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import serial 4 | 5 | ser = serial.Serial( 6 | port='COM21', 7 | baudrate=9600, 8 | parity=serial.PARITY_NONE, 9 | stopbits=serial.STOPBITS_ONE, 10 | bytesize=serial.EIGHTBITS, 11 | timeout=1 12 | ) 13 | 14 | ser.flush() 15 | 16 | def readFile(uuid): 17 | print(f"[py:log] Checking for UUID: |{uuid}|") 18 | with open("./tags.txt", "r") as f: 19 | for line in f.readlines(): 20 | if line.rstrip().lstrip() == uuid: 21 | return True 22 | 23 | return False 24 | 25 | 26 | 27 | 28 | if __name__ == '__main__': 29 | while True: 30 | if ser.in_waiting > 0: 31 | line = ser.readline().decode('utf-8').rstrip() 32 | print(line) 33 | if line[0:14] == "[check-access]": 34 | print("Checking Access") 35 | isAllowed = readFile(line[14:].rstrip().lstrip()) 36 | response = "granted" if isAllowed else "denied" 37 | ser.write(response.encode()) -------------------------------------------------------------------------------- /reading-rfid.ino/reading-rfid/reading-rfid.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define RST_PIN 9 4 | #define SS_PIN 10 5 | MFRC522 mfrc522(SS_PIN, RST_PIN); 6 | MFRC522::MIFARE_Key key; 7 | MFRC522::StatusCode card_status; 8 | void setup(){ 9 | Serial.begin(9600); 10 | SPI.begin(); 11 | mfrc522.PCD_Init(); 12 | Serial.println(F("PCD Ready!")); 13 | } 14 | void loop(){ 15 | for (byte i = 0; i < 6; i++){ 16 | key.keyByte[i] = 0xFF; 17 | } 18 | if(!mfrc522.PICC_IsNewCardPresent()){ 19 | return; 20 | } 21 | if(!mfrc522.PICC_ReadCardSerial()){ 22 | return; 23 | } 24 | 25 | Serial.println(F("\n*** Balance on the PICC ***\n")); 26 | String balance = readBytesFromBlock(); 27 | Serial.println(balance); 28 | Serial.println(F("\n***************************\n")); 29 | delay(1000); 30 | 31 | mfrc522.PICC_HaltA(); 32 | mfrc522.PCD_StopCrypto1(); 33 | } 34 | String readBytesFromBlock(){ 35 | byte blockNumber = 8; 36 | 37 | card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNumber, &key, &(mfrc522.uid)); 38 | if(card_status != MFRC522::STATUS_OK){ 39 | Serial.print(F("Authentication failed: ")); 40 | Serial.println(mfrc522.GetStatusCodeName(card_status)); 41 | return; 42 | } 43 | byte arrayAddress[18]; 44 | byte buffersize = sizeof(arrayAddress); 45 | card_status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize); 46 | if(card_status != MFRC522::STATUS_OK){ 47 | Serial.print(F("Reading failed: ")); 48 | Serial.println(mfrc522.GetStatusCodeName(card_status)); 49 | return; 50 | } 51 | 52 | String value = ""; 53 | for (uint8_t i = 0; i < 16; i++){ 54 | value += (char)arrayAddress[i]; 55 | } 56 | value.trim(); 57 | return value; 58 | } -------------------------------------------------------------------------------- /writing.ino/writing/writing.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define RST_PIN 9 4 | #define SS_PIN 10 5 | MFRC522 mfrc522(SS_PIN, RST_PIN); 6 | MFRC522::MIFARE_Key key; 7 | MFRC522::StatusCode card_status; 8 | void setup(){ 9 | Serial.begin(9600); 10 | SPI.begin(); 11 | mfrc522.PCD_Init(); 12 | Serial.println(F("Enter data, ending with #")); 13 | Serial.println(""); 14 | } 15 | void loop(){ 16 | for(byte i = 0; i < 6; i++){ 17 | key.keyByte[i] = 0xFF; 18 | } 19 | if(!mfrc522.PICC_IsNewCardPresent()){ 20 | return; 21 | } 22 | 23 | if (!mfrc522.PICC_ReadCardSerial()){ 24 | Serial.println("[Bring PIIC closer to PCD]"); 25 | return; 26 | } 27 | 28 | byte buffr[16]; 29 | byte block = 8; 30 | byte len; 31 | 32 | Serial.setTimeout(1000L); 33 | /* 34 | * Read data from serial 35 | */ 36 | len = Serial.readBytesUntil('#', (char *) buffr, 16) ; 37 | Serial.println("[PIIC ready!]"); 38 | if(len>0){ 39 | for(byte i = len; i < 16; i++){ 40 | buffr[i] = ' '; //We have to pad array items with spaces. 41 | } 42 | String mString; 43 | mString = String((char*)buffr); 44 | 45 | Serial.println(" "); 46 | writeBytesToBlock(block, buffr); 47 | Serial.println(" "); 48 | mfrc522.PICC_HaltA(); 49 | mfrc522.PCD_StopCrypto1(); 50 | Serial.print("Saved on block "); 51 | Serial.print(block); 52 | Serial.print(":"); 53 | Serial.println(mString); 54 | Serial.println("Note: To rewrite to this PICC, take it away from the PCD, and bring it closer again."); 55 | } 56 | delay(500); 57 | } 58 | void writeBytesToBlock(byte block, byte buff[]){ 59 | card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); 60 | 61 | if(card_status != MFRC522::STATUS_OK) { 62 | Serial.print(F("PCD_Authenticate() failed: ")); 63 | Serial.println(mfrc522.GetStatusCodeName(card_status)); 64 | return; 65 | } 66 | 67 | else{ 68 | Serial.println(F("PCD_Authenticate() success: ")); 69 | } 70 | // Write block 71 | card_status = mfrc522.MIFARE_Write(block, buff, 16); 72 | 73 | if (card_status != MFRC522::STATUS_OK) { 74 | Serial.print(F("MIFARE_Write() failed: ")); 75 | Serial.println(mfrc522.GetStatusCodeName(card_status)); 76 | return; 77 | } 78 | else{ 79 | Serial.println(F("Data saved.")); 80 | } 81 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RFID Sample Projects Repository 2 | 3 | This repository contains sample projects for working with RFID technology. RFID, or radio-frequency identification, is a technology that uses radio waves to automatically identify and track tags attached to objects. It has a wide range of applications, from inventory management to access control systems. 4 | 5 | The projects in this repository are intended to serve as examples and starting points for developers who want to work with RFID. 6 | 7 | # Table Of Content 8 | 9 | | Folder | Link | 10 | |-------------------------------------------------------------|--------------------------------------------------------------------------------------------| 11 | | reading-memory-map | [reading-memory-map](https://github.com/pacifiquem/RFID/tree/main/read-memory-map.ino) | 12 | | reading-data-from-card | [reading-data-from-card](https://github.com/pacifiquem/RFID/tree/main/reading-rfid.ino/reading-rfid) | 13 | | secure rfid or authentication with rfid | [secure-rfid](https://github.com/pacifiquem/RFID/tree/main/secure-rfid.ino) | 14 | | writing data | [writing-data](https://github.com/pacifiquem/RFID/tree/main/writing.ino/writing) | 15 | | rfid transaction system | [rfid-transaction-system](https://github.com/pacifiquem/RFID/tree/main/rfid-transaction.ino/rfid-transaction/rfid-transaction.ino) | 16 | 17 | 18 | # Getting Started 19 | To get started with these projects, clone this repository to your local machine and follow the instructions provided in the project's README file. Each project has its own dependencies and setup requirements, so be sure to read the instructions carefully before attempting to run any code. 20 | 21 | # Connecting PCD to Arduino Uno 22 | 23 | This image shows how you can connect your PCD to an Arduino Uno board. 24 | ![PCD to Arduino Uno picture](https://github.com/pacifiquem/RFID/blob/main/Screenshot%20from%202023-11-02%2018-52-51.png) 25 | 26 | # Contributing 27 | Contributions to this repository are welcome! If you have a sample project you'd like to add, please fork this repository and submit a pull request with your changes. Please include a README file with instructions for running your project. 28 | 29 | # License 30 | This repository is licensed under the MIT License. See the LICENSE file for more information. 31 | -------------------------------------------------------------------------------- /secure-rfid.ino/secure-rfid.ino.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #define SS_PIN 10 6 | #define RST_PIN 9 7 | MFRC522 rfid(SS_PIN, RST_PIN); 8 | MFRC522::MIFARE_Key key; 9 | byte nuidPICC[4]; 10 | char ACCESS_GRANTED[] = "granted"; 11 | 12 | const int SUCCESS_PIN = 7; 13 | const int ERROR_PIN = 8; 14 | const int BUZZER_PIN = 6; 15 | 16 | void setup() { 17 | Serial.begin(9600); 18 | SPI.begin(); 19 | rfid.PCD_Init(); 20 | pinMode(SUCCESS_PIN, OUTPUT); 21 | pinMode(ERROR_PIN, OUTPUT); 22 | pinMode(BUZZER_PIN, OUTPUT); 23 | 24 | Serial.println(F("READING THE CARD UNIQUE ID:")); 25 | for (byte i = 0; i < 6; i++) { 26 | key.keyByte[i] = 0xFF; 27 | } 28 | } 29 | void loop() { 30 | if (!rfid.PICC_IsNewCardPresent()) { 31 | return; 32 | } 33 | if (!rfid.PICC_ReadCardSerial()) { 34 | return; 35 | } 36 | if (rfid.uid.uidByte[0] != nuidPICC[0] || 37 | rfid.uid.uidByte[1] != nuidPICC[1] || 38 | rfid.uid.uidByte[2] != nuidPICC[2] || 39 | rfid.uid.uidByte[3] != nuidPICC[3]) { 40 | for (byte i = 0; i < 4; i++) { 41 | nuidPICC[i] = rfid.uid.uidByte[i]; 42 | } 43 | Serial.print(F("[check-access]")); 44 | printHex(rfid.uid.uidByte, rfid.uid.size); 45 | Serial.println(F("\n")); 46 | Serial.setTimeout(500); 47 | String res = Serial.readString(); 48 | res.trim(); 49 | Serial.print("|"); 50 | Serial.print(res); 51 | Serial.println("|"); 52 | if(res.equals(ACCESS_GRANTED)){ 53 | Serial.println("ACCESS GRANTED"); 54 | access_granted(); 55 | }else { 56 | Serial.println("ACCESS DENIED"); 57 | access_denied(); 58 | } 59 | } else { 60 | Serial.println(F("This car was lastly detected .")); 61 | buzz("once"); 62 | } 63 | /* 64 | * Halt PICC 65 | * Stop encryption on PCD 66 | */ 67 | rfid.PICC_HaltA(); 68 | rfid.PCD_StopCrypto1(); 69 | } 70 | void printHex(byte * buffer, byte bufferSize) { 71 | for (byte i = 0; i < bufferSize; i++) { 72 | Serial.print(buffer[i] < 0x10 ? " 0" : " "); 73 | Serial.print(buffer[i], HEX); 74 | } 75 | } 76 | 77 | void access_granted() { 78 | digitalWrite(SUCCESS_PIN, HIGH); 79 | digitalWrite(ERROR_PIN, LOW); 80 | buzz("twice"); 81 | delay(1000); 82 | digitalWrite(SUCCESS_PIN, LOW); 83 | } 84 | 85 | void access_denied() { 86 | digitalWrite(ERROR_PIN, HIGH); 87 | digitalWrite(SUCCESS_PIN, LOW); 88 | buzz("long"); 89 | digitalWrite(ERROR_PIN, LOW); 90 | } 91 | 92 | void buzz(String type){ 93 | if(type.equals("once")) { 94 | analogWrite(BUZZER_PIN, 60); 95 | delay(60); 96 | analogWrite(BUZZER_PIN, 0); 97 | }else if(type.equals("twice")){ 98 | analogWrite(BUZZER_PIN, 60); 99 | delay(60); 100 | analogWrite(BUZZER_PIN, 0); 101 | delay(60); 102 | analogWrite(BUZZER_PIN, 60); 103 | delay(60); 104 | analogWrite(BUZZER_PIN, 0); 105 | }else if(type.equals("long")){ 106 | analogWrite(BUZZER_PIN, 60); 107 | delay(1000); 108 | analogWrite(BUZZER_PIN, 0); 109 | } else { 110 | analogWrite(BUZZER_PIN, 60); 111 | delay(60); 112 | analogWrite(BUZZER_PIN, 0); 113 | } 114 | } -------------------------------------------------------------------------------- /rfid-transaction.ino/rfid-transaction/rfid-transaction.ino/rfid-transaction.ino.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define RST_PIN 9 6 | #define SS_PIN 10 7 | #define SD_CHIP_SELECT 4 8 | 9 | MFRC522 mfrc522(SS_PIN, RST_PIN); 10 | MFRC522::MIFARE_Key key; 11 | MFRC522::StatusCode card_status; 12 | 13 | int moneyAmount = 0; 14 | int pointsAmount = 0; 15 | 16 | File myFile; 17 | 18 | void setup() { 19 | Serial.begin(9600); // initialize Serial 20 | SPI.begin(); // initialize SPI 21 | mfrc522.PCD_Init(); // initialize RFID Read (PCD) 22 | 23 | Serial.println("Initializing SD card..."); 24 | 25 | if (!SD.begin(SD_CHIP_SELECT)) { 26 | Serial.println("SD card initialization failed."); 27 | while (1); 28 | } 29 | 30 | Serial.println("SD card initialized.\n"); 31 | 32 | Serial.println(F("\n ==== Place the card near the reader. ==== \n")); 33 | } 34 | 35 | void loop() { 36 | for (byte i = 0; i < 6; i++) { 37 | key.keyByte[i] = 0xFF; 38 | } 39 | 40 | if (!mfrc522.PICC_IsNewCardPresent()) { 41 | return; 42 | } 43 | 44 | if (!mfrc522.PICC_ReadCardSerial()) { 45 | Serial.println(F("Error reading card.")); 46 | return; 47 | } 48 | 49 | byte buffer[18]; // Increased buffer size to account for null character 50 | byte moneyBlock = 4; 51 | byte pointsBlock = 8; 52 | byte len; 53 | 54 | Serial.println(F("Card detected.")); 55 | 56 | // Authenticate moneyBlock to read money amount 57 | card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, moneyBlock, &key, &(mfrc522.uid)); 58 | if (card_status != MFRC522::STATUS_OK) { 59 | Serial.println(F("Authentication error for moneyBlock.")); 60 | return; 61 | } 62 | 63 | // Read money amount from moneyBlock 64 | card_status = mfrc522.MIFARE_Read(moneyBlock, buffer, &len); 65 | if (card_status != MFRC522::STATUS_OK) { 66 | Serial.println(F("Error reading moneyBlock.")); 67 | return; 68 | } 69 | 70 | buffer[len] = '\0'; // Null-terminate the buffer 71 | moneyAmount = atoi((char*)buffer); 72 | 73 | // Authenticate pointsBlock to read points amount 74 | card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, pointsBlock, &key, &(mfrc522.uid)); 75 | if (card_status != MFRC522::STATUS_OK) { 76 | Serial.println(F("Authentication error for pointsBlock.")); 77 | return; 78 | } 79 | 80 | // Read points amount from pointsBlock 81 | card_status = mfrc522.MIFARE_Read(pointsBlock, buffer, &len); 82 | if (card_status != MFRC522::STATUS_OK) { 83 | Serial.println(F("Error reading pointsBlock.")); 84 | return; 85 | } 86 | 87 | buffer[len] = '\0'; // Null-terminate the buffer 88 | pointsAmount = atoi((char*)buffer); 89 | 90 | Serial.print(F("Available Money: $")); 91 | Serial.println(moneyAmount); 92 | Serial.print(F("Available Points: ")); 93 | Serial.println(pointsAmount); 94 | 95 | Serial.println(F("Enter 'm' to use money, 'p' to use points:")); 96 | 97 | while (!Serial.available()) { 98 | // Wait for input 99 | } 100 | 101 | char input = Serial.read(); 102 | int amount = 0; 103 | 104 | if (input == 'm' || input == 'M') { 105 | Serial.println(F("Enter the amount to deduct from money: ")); 106 | 107 | while (!Serial.available()) { 108 | // Wait for input 109 | } 110 | 111 | amount = Serial.parseInt(); 112 | 113 | if (amount > moneyAmount) { 114 | Serial.println(F("Insufficient funds.")); 115 | return; 116 | } 117 | 118 | moneyAmount -= amount; 119 | pointsAmount += 10; // Increased points by 10 because he/she used money 120 | 121 | bool saved_data_to_file = writeToSDCard(amount, "Money", moneyAmount); // save transaction to file 122 | 123 | // Send transaction information to Python code 124 | Serial.print(F("Transaction: Money, Deducted: $")); 125 | Serial.println(amount); 126 | 127 | } else if (input == 'p' || input == 'P') { 128 | Serial.println(F("Enter the amount to deduct from points: ")); 129 | 130 | while (!Serial.available()) { 131 | // Wait for input 132 | } 133 | 134 | amount = Serial.parseInt(); 135 | 136 | if (amount > pointsAmount) { 137 | Serial.println(F("Insufficient points.")); 138 | return; 139 | } 140 | 141 | pointsAmount -= amount; 142 | 143 | bool saved_data_to_file = writeToSDCard(amount, "Points", pointsAmount); // save transaction to file 144 | 145 | } else { 146 | Serial.println("Invalid Input."); 147 | return; 148 | } 149 | 150 | sprintf((char*)buffer, "%d", moneyAmount); 151 | card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, moneyBlock, &key, &(mfrc522.uid)); 152 | if (card_status != MFRC522::STATUS_OK) { 153 | Serial.println(F("Authentication error for moneyBlock.")); 154 | return; 155 | } 156 | card_status = mfrc522.MIFARE_Write(moneyBlock, buffer, 16); 157 | if (card_status != MFRC522::STATUS_OK) { 158 | Serial.println(F("Error writing to moneyBlock.")); 159 | return; 160 | } 161 | 162 | sprintf((char*)buffer, "%d", pointsAmount); 163 | card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, pointsBlock, &key, &(mfrc522.uid)); 164 | 165 | if (card_status != MFRC522::STATUS_OK) { 166 | Serial.println(F("Authentication error for pointsBlock.")); 167 | return; 168 | } 169 | 170 | card_status = mfrc522.MIFARE_Write(pointsBlock, buffer, 16); 171 | 172 | if (card_status != MFRC522::STATUS_OK) { 173 | Serial.println(F("Error writing to pointsBlock.")); 174 | return; 175 | } 176 | 177 | Serial.print(F("Remaining Money: $")); 178 | Serial.println(moneyAmount); 179 | Serial.print(F("Remaining Points: ")); 180 | Serial.println(pointsAmount); 181 | 182 | mfrc522.PICC_HaltA(); 183 | mfrc522.PCD_StopCrypto1(); 184 | 185 | Serial.println("\n \n"); 186 | delay(5000); 187 | } 188 | 189 | bool writeToSDCard(int initial_balance, String mode, int new_balance) { 190 | myFile = SD.open("data.txt", FILE_WRITE); 191 | if (myFile) { 192 | myFile.println(""); 193 | myFile.print("Initial Balance: "); 194 | myFile.println(initial_balance); 195 | myFile.print("Mode Used: "); 196 | myFile.println(mode); 197 | myFile.print("New Balance: "); 198 | myFile.println(new_balance); 199 | myFile.println("\n"); 200 | myFile.close(); 201 | return true; 202 | } else { 203 | Serial.println("error opening data.txt"); 204 | return false; 205 | } 206 | } 207 | 208 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------