Advertisement
RuiViana

RFID

Jan 30th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. //  https://github.com/ljos/MFRC522
  2.  
  3.  
  4. /*
  5. RC522 MODULE    Uno/Nano     MEGA
  6. SDA             D10          D9
  7. SCK             D13          D52
  8. MOSI            D11          D51
  9. MISO            D12          D50
  10. IRQ             N/A          N/A
  11. GND             GND          GND
  12. RST             D9           D8
  13. 3.3V            3.3V         3.3V
  14. */
  15.  
  16. //#define SS_PIN 10   // Uno
  17. //#define RST_PIN 9   // Uno
  18.  
  19. #define SS_PIN 9      // Mega
  20. #define RST_PIN 8      // Mega
  21.  
  22. #include <MFRC522.h>
  23. #include <SPI.h>
  24. String conteudo = "";
  25. int accLib = 0;
  26. String Rf_cadastrados[10]
  27. { "FECDFBB",
  28.   "44587688"
  29. };
  30. MFRC522 nfc(SS_PIN, RST_PIN);
  31. //-------------------------------------------------------
  32. void setup()
  33. {
  34.   SPI.begin();
  35.   Serial.begin(9600);
  36.   nfc.begin();
  37.  
  38.   // Get the firmware version of the RFID chip
  39.   byte version = nfc.getFirmwareVersion();
  40.   if (! version)
  41.   {
  42.     Serial.print("Didn't find MFRC522 board.");
  43.     while (1); //halt
  44.   }
  45.   Serial.print("Firmware ver. 0x");
  46.   Serial.println(version, HEX);
  47. }
  48. //-------------------------------------------------------
  49. void loop()
  50. {
  51.   byte status;
  52.   byte data[MAX_LEN];
  53.   byte serial[5];
  54.   status = nfc.requestTag(MF1_REQIDL, data);
  55.   if (status == MI_OK)
  56.   {
  57.     status = nfc.antiCollision(data);
  58.     memcpy(serial, data, 5);
  59.     Serial.print("O numero de serie da tag e' : ");
  60.     for (int i = 0; i < 4; i++)
  61.     {
  62.       conteudo.concat(String(serial[i], HEX));
  63.     }
  64.     conteudo.toUpperCase();                                     // deixa o texto do conteudo em caixa alta.
  65.     Serial.println(conteudo);
  66.     if (conteudo != "")                                         // Se conteudo for nulo não leu RF, se tiver qualquer coisa leu.
  67.     {
  68.       for (byte i = 0; i < 10; i++)
  69.       {
  70.         // Percorre o indice Rf_cadastrados para ver se o valor lido esta cadastrado.
  71.         if (conteudo == Rf_cadastrados[i] )
  72.         {
  73.           accLib = 1;
  74.           // Serial.println("Acesso liberado");
  75.         }
  76.       }
  77.       if (accLib == 1)
  78.       {
  79.         Serial.println("Acesso liberado");
  80.         accLib = 0;
  81.       }
  82.       else
  83.       Serial.println("Acesso nao liberado");
  84.         conteudo = "";                                            //Limpa variavel conteudo.
  85.     }
  86.  
  87.     // Select the tag that we want to talk to. If we don't do this the
  88.     // chip does not know which tag it should talk if there should be
  89.     // any other tags in the area..
  90.     nfc.selectTag(serial);
  91.  
  92.     // Stop the tag and get ready for reading a new tag.
  93.     nfc.haltTag();
  94.   }
  95.   delay(100);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement