Skip to content

Commit 01db2a8

Browse files
committed
feat: added rfid transaction system
1 parent e50a5e1 commit 01db2a8

File tree

4 files changed

+263
-99
lines changed

4 files changed

+263
-99
lines changed

reading-rfid.ino/reading-rfid.ino renamed to reading-rfid.ino/reading-rfid/reading-rfid.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void loop(){
3232
mfrc522.PCD_StopCrypto1();
3333
}
3434
String readBytesFromBlock(){
35-
byte blockNumber = 4;
35+
byte blockNumber = 8;
3636

3737
card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNumber, &key, &(mfrc522.uid));
3838
if(card_status != MFRC522::STATUS_OK){
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Project Name: RFID Transaction Management System
3+
* Author: Murangwa Pacifique
4+
* Co-Authors: Ighor - Tresor
5+
*/
6+
7+
8+
#include <SPI.h>
9+
#include <MFRC522.h>
10+
11+
#define RST_PIN 9
12+
#define SS_PIN 10
13+
14+
MFRC522 mfrc522(SS_PIN, RST_PIN);
15+
MFRC522::MIFARE_Key key;
16+
MFRC522::StatusCode card_status;
17+
18+
int moneyAmount = 0;
19+
int pointsAmount = 0;
20+
21+
void setup() {
22+
Serial.begin(9600);
23+
SPI.begin();
24+
mfrc522.PCD_Init();
25+
26+
Serial.println(F("\n ==== Place the card near the reader. ==== \n"));
27+
28+
}
29+
30+
void loop() {
31+
32+
33+
34+
for (byte i = 0; i < 6; i++) {
35+
key.keyByte[i] = 0xFF;
36+
}
37+
38+
if (!mfrc522.PICC_IsNewCardPresent()) {
39+
return;
40+
}
41+
42+
if (!mfrc522.PICC_ReadCardSerial()) {
43+
Serial.println(mfrc522.PICC_ReadCardSerial());
44+
Serial.println(F("Error reading card."));
45+
return;
46+
}
47+
48+
byte buffer[18]; // Increased buffer size to account for null character
49+
byte moneyBlock = 4;
50+
byte pointsBlock = 8;
51+
byte len;
52+
53+
Serial.println(F("Card detected."));
54+
55+
// Authenticate moneyBlock to read money amount
56+
card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, moneyBlock, &key, &(mfrc522.uid));
57+
if (card_status != MFRC522::STATUS_OK) {
58+
Serial.println(F("Authentication error for moneyBlock."));
59+
return;
60+
}
61+
62+
// Read money amount from moneyBlock
63+
card_status = mfrc522.MIFARE_Read(moneyBlock, buffer, &len);
64+
if (card_status != MFRC522::STATUS_OK) {
65+
Serial.println(F("Error reading moneyBlock."));
66+
return;
67+
}
68+
69+
buffer[len] = '\0'; // Null-terminate the buffer
70+
moneyAmount = atoi((char*)buffer);
71+
72+
// Authenticate block 8 to read pointsBlock amount
73+
card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, pointsBlock, &key, &(mfrc522.uid));
74+
if (card_status != MFRC522::STATUS_OK) {
75+
Serial.println(F("Authentication error for pointsBlock."));
76+
return;
77+
}
78+
79+
// Read points amount from pointsBlock
80+
card_status = mfrc522.MIFARE_Read(pointsBlock, buffer, &len);
81+
if (card_status != MFRC522::STATUS_OK) {
82+
Serial.println(F("Error reading pointsBlock."));
83+
return;
84+
}
85+
86+
buffer[len] = '\0'; // Null-terminate the buffer
87+
pointsAmount = atoi((char*)buffer);
88+
89+
Serial.print(F("Available Money: $"));
90+
Serial.println(moneyAmount);
91+
Serial.print(F("Available Points: "));
92+
Serial.println(pointsAmount);
93+
94+
Serial.println(F("Enter 'm' to use money, 'p' to use points:"));
95+
96+
while (Serial.available() <= 0) {
97+
//Wait for input
98+
}
99+
100+
char input = Serial.read();
101+
int amount = 0;
102+
103+
if (input == 'm') {
104+
Serial.println(F("Enter the amount to deduct from money: "));
105+
106+
while (Serial.available() <= 1) {
107+
// Wait for input
108+
}
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+
Serial.println(F("Transaction completed successfully."));
122+
} else if (input == 'p') {
123+
Serial.println(F("Enter the amount to deduct from points: "));
124+
125+
while (Serial.available() <= 1) {
126+
// Wait for input
127+
}
128+
129+
amount = Serial.parseInt();
130+
131+
if (amount > pointsAmount) {
132+
Serial.println(F("Insufficient funds."));
133+
return;
134+
}
135+
136+
pointsAmount -= amount;
137+
Serial.println(F("Transaction completed successfully."));
138+
} else {
139+
Serial.println("Invalid Input.");
140+
return;
141+
}
142+
143+
sprintf((char*)buffer, "%d", moneyAmount);
144+
card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, moneyBlock, &key, &(mfrc522.uid));
145+
if (card_status != MFRC522::STATUS_OK) {
146+
Serial.println(F("Authentication error for moneyBlock."));
147+
return;
148+
}
149+
card_status = mfrc522.MIFARE_Write(moneyBlock, buffer, 16);
150+
if (card_status != MFRC522::STATUS_OK) {
151+
Serial.println(F("Error writing to moneyBlock."));
152+
return;
153+
}
154+
155+
sprintf((char*)buffer, "%d", pointsAmount);
156+
card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, pointsBlock, &key, &(mfrc522.uid));
157+
158+
if (card_status != MFRC522::STATUS_OK) {
159+
Serial.println(F("Authentication error for block 8."));
160+
return;
161+
}
162+
163+
card_status = mfrc522.MIFARE_Write(pointsBlock, buffer, 16);
164+
165+
if (card_status != MFRC522::STATUS_OK) {
166+
Serial.println(F("Error writing to pointsBlock."));
167+
return;
168+
}
169+
170+
Serial.print(F("Remaining Money: $"));
171+
Serial.println(moneyAmount);
172+
Serial.print(F("Remaining Points: "));
173+
Serial.println(pointsAmount);
174+
175+
mfrc522.PICC_HaltA();
176+
mfrc522.PCD_StopCrypto1();
177+
178+
Serial.println("Wait 10 seconds before making another transaction");
179+
180+
delay(10000); // Delay to allow card removal before the next transaction
181+
}

writing-rfid.ino/writing.ino

-98
This file was deleted.

writing.ino/writing/writing.ino

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <SPI.h>
2+
#include <MFRC522.h>
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+
}

0 commit comments

Comments
 (0)