Advertisement
GUPPYYYY

RFID

Sep 9th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <SPI.h>
  2.  
  3. #include <MFRC522.h>
  4.  
  5.  
  6.  
  7. #define SS_PIN 10
  8.  
  9. #define RST_PIN 9
  10.  
  11.  
  12.  
  13. MFRC522 mfrc522(SS_PIN, RST_PIN);
  14.  
  15. MFRC522::MIFARE_Key key;
  16.  
  17.  
  18.  
  19. int block=2;
  20.  
  21.  
  22.  
  23. byte blockcontent[16] = {"Last-Minute-Engg"};
  24. //byte blockcontent[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  25.  
  26.  
  27. byte readbackblock[18];
  28.  
  29.  
  30.  
  31. void setup()
  32.  
  33. {
  34.  
  35. Serial.begin(9600);
  36. SPI.begin();
  37.  
  38. mfrc522.PCD_Init();
  39. Serial.println("Scan a MIFARE Classic card");
  40.  
  41.  
  42.  
  43.  
  44.  
  45. for (byte i = 0; i < 6; i++) {
  46.  
  47. key.keyByte[i] = 0xFF; //keyByte is defined in the "MIFARE_Key" 'struct' definition in the .h file of the library
  48.  
  49. }
  50.  
  51. }
  52.  
  53.  
  54.  
  55. void loop()
  56.  
  57. {
  58.  
  59.  
  60. if ( ! mfrc522.PICC_IsNewCardPresent()) {
  61.  
  62. return;
  63.  
  64. }
  65.  
  66.  
  67.  
  68.  
  69. if ( ! mfrc522.PICC_ReadCardSerial())
  70.  
  71. {
  72.  
  73. return;
  74.  
  75. }
  76.  
  77. Serial.println("card selected");
  78.  
  79.  
  80.  
  81.  
  82. writeBlock(block, blockcontent);
  83.  
  84.  
  85.  
  86.  
  87. readBlock(block, readbackblock);
  88.  
  89.  
  90.  
  91.  
  92. Serial.print("read block: ");
  93.  
  94. for (int j=0 ; j<16 ; j++)
  95.  
  96. {
  97.  
  98. Serial.write (readbackblock[j]);
  99.  
  100. }
  101.  
  102. Serial.println("");
  103.  
  104. }
  105.  
  106.  
  107.  
  108.  
  109. int writeBlock(int blockNumber, byte arrayAddress[])
  110.  
  111. {
  112.  
  113.  
  114. int largestModulo4Number=blockNumber/4*4;
  115.  
  116. int trailerBlock=largestModulo4Number+3;
  117.  
  118. if (blockNumber > 2 && (blockNumber+1)%4 == 0){Serial.print(blockNumber);Serial.println(" is a trailer block:");return 2;}
  119.  
  120. Serial.print(blockNumber);
  121.  
  122. Serial.println(" is a data block:");
  123.  
  124.  
  125.  
  126. byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  127.  
  128. if (status != MFRC522::STATUS_OK) {
  129.  
  130. Serial.print("PCD_Authenticate() failed: ");
  131.  
  132. Serial.println(mfrc522.GetStatusCodeName(status));
  133.  
  134. return 3;//return "3" as error message
  135.  
  136. }
  137.  
  138.  
  139.  
  140.  
  141. status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16);
  142.  
  143.  
  144.  
  145. if (status != MFRC522::STATUS_OK) {
  146.  
  147. Serial.print("MIFARE_Write() failed: ");
  148.  
  149. Serial.println(mfrc522.GetStatusCodeName(status));
  150.  
  151. return 4;//return "4" as error message
  152.  
  153. }
  154.  
  155. Serial.println("block was written");
  156.  
  157. }
  158.  
  159.  
  160.  
  161.  
  162. int readBlock(int blockNumber, byte arrayAddress[])
  163.  
  164. {
  165.  
  166. int largestModulo4Number=blockNumber/4*4;
  167.  
  168. int trailerBlock=largestModulo4Number+3;//determine trailer block for the sector
  169.  
  170.  
  171.  
  172.  
  173. byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  174.  
  175.  
  176.  
  177. if (status != MFRC522::STATUS_OK) {
  178.  
  179. Serial.print("PCD_Authenticate() failed (read): ");
  180.  
  181. Serial.println(mfrc522.GetStatusCodeName(status));
  182.  
  183. return 3;//return "3" as error message
  184.  
  185. }
  186.  
  187.  
  188.  
  189.  
  190. byte buffersize = 18;
  191. status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize);
  192.  
  193. if (status != MFRC522::STATUS_OK) {
  194.  
  195. Serial.print("MIFARE_read() failed: ");
  196.  
  197. Serial.println(mfrc522.GetStatusCodeName(status));
  198.  
  199. return 4;//return "4" as error message
  200.  
  201. }
  202.  
  203. Serial.println("block was read");
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement