Skip to content

Instantly share code, notes, and snippets.

@ajangrahmat
Created December 1, 2023 07:59
Show Gist options
  • Save ajangrahmat/cbfcc586bfcffd8bc7a041b4a3d44fce to your computer and use it in GitHub Desktop.
Save ajangrahmat/cbfcc586bfcffd8bc7a041b4a3d44fce to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 15
#define RST_PIN 2
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
int blockNum = 2;
int intData = 1000000; // Example integer data
byte blockData[16];
byte bufferLen = 18;
byte readBlockData[18];
MFRC522::StatusCode status;
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Scan a MIFARE 1K Tag to write data...");
}
void loop() {
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.print("\n");
Serial.println("**Card Detected**");
Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.print("\n");
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
Serial.print("\n");
Serial.println("Writing to Data Block...");
intToBytes(intData, blockData);
WriteDataToBlock(blockNum, blockData);
Serial.print("\n");
Serial.println("Reading from Data Block...");
ReadDataFromBlock(blockNum, readBlockData);
int readIntData = bytesToInt(readBlockData);
Serial.print("\n");
Serial.print("Data in Block:");
Serial.print(blockNum);
Serial.print(" --> ");
Serial.println(readIntData);
reselect_Card();
delay(3000);
}
void WriteDataToBlock(int blockNum, byte blockData[]) {
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("Authentication failed for Write: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
} else {
Serial.println("Authentication success");
}
status = mfrc522.MIFARE_Write(blockNum, blockData, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print("Writing to Block failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
} else {
Serial.println("Data was written into Block successfully");
}
}
void ReadDataFromBlock(int blockNum, byte readBlockData[]) {
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("Authentication failed for Read: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
} else {
Serial.println("Authentication success");
}
status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
if (status != MFRC522::STATUS_OK) {
Serial.print("Reading failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
} else {
Serial.println("Block was read successfully");
}
}
void intToBytes(int value, byte* byteArray) {
for (int i = 0; i < 4; ++i) {
byteArray[i] = (value >> (8 * i)) & 0xFF;
}
}
int bytesToInt(byte* byteArray) {
int value = 0;
for (int i = 0; i < 4; ++i) {
value |= byteArray[i] << (8 * i);
}
return value;
}
bool reselect_Card() {
//-------------------------------------------------------
// Can also be used to see if the card is still available,
// true means it is, false means the card isn't there anymore
//-------------------------------------------------------
byte s;
byte req_buff[2];
byte req_buff_size = 2;
mfrc522.PCD_StopCrypto1();
s = mfrc522.PICC_HaltA();
Serial.print("Halt Status: ");
Serial.println(mfrc522.GetStatusCodeName((MFRC522::StatusCode)s));
delay(100);
s = mfrc522.PICC_WakeupA(req_buff, &req_buff_size);
Serial.print("Request: ");
Serial.println(mfrc522.GetStatusCodeName((MFRC522::StatusCode)s));
Serial.print("ATQA : ");
Serial.println(dump_byte_array_to_string(req_buff, req_buff_size));
delay(100);
s = mfrc522.PICC_Select(&(mfrc522.uid), 0);
Serial.print("Selected : ");
Serial.println(mfrc522.GetStatusCodeName((MFRC522::StatusCode)s));
const char* statusCodeName = reinterpret_cast<const char*>(mfrc522.GetStatusCodeName((MFRC522::StatusCode)s));
if (strcmp(statusCodeName, "Timeout in communication.") == 0) {
return false;
}
return true;
}
String dump_byte_array_to_string(byte* buffer, byte size) {
String result = "";
for (int i = 0; i < size; i++) {
result += String(buffer[i], HEX);
if (i < size - 1) {
result += " ";
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment