Advertisement
Tempist

RFID + Relay + HC-05 [2FA Auth|HC-05|RFID] [3/3]

Jul 12th, 2019
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.    ----------------------------------------------------------------------------
  3.    My twitter is @itsWizardly and my Discord is wizard#7815
  4.    for further details and other examples.
  5.  
  6.    NOTE: If you have any issues, inform me and I will reply asap.
  7.  
  8.    This sketch show a simple locking mechanism using the RC522 RFID module.
  9.    ----------------------------------------------------------------------------
  10.    Typical pin layout used:
  11.    -----------------------------------------------------------------------------------------
  12.                MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
  13.                Reader/PCD   Uno           Mega      Nano v3    Leonardo/Micro   Pro Micro
  14.    Signal      Pin          Pin           Pin       Pin        Pin              Pin
  15.    -----------------------------------------------------------------------------------------
  16.    RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
  17.    SPI SS      SDA(SS)      10            53        D10        10               10
  18.    SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
  19.    SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
  20.    SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
  21.  
  22. */
  23.  
  24. #include <SPI.h>
  25. #include <MFRC522.h>
  26. #define RST_PIN         9           // Configurable, see typical pin layout above
  27. #define SS_PIN          10          // Configurable, see typical pin layout above
  28. MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
  29.  
  30. String read_rfid;                   // Add how many you need and don't forget to include the UID.
  31. String ok_rfid_1 = "e199312d";      // This is for my main RFID Card. aka. The one I will be using to turn on my PC. Can also be used to shut it down if you want to.
  32. String ok_rfid_2 = "fbecb673";      // This is for the RFID Keyfob. aka. Shutdown Keyfob. Not advisable tho. Just shutdown your PC normally.
  33. int switch1 = 7;                    // For the Card.
  34. int switch2 = 7;                    // For the Keyfob.
  35. int BlueCom;                        // Bluetooth Command.
  36. int Thing = 8;                      // Pin number where the Thing is connected.
  37.  
  38. void setup() {
  39.  
  40.   Serial.begin(9600);               // Initialize serial communications with the PC
  41.   while (!Serial);                  // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  42.   SPI.begin();                      // Init SPI bus
  43.   pinMode(Thing, OUTPUT);           // Set Thing Pin as OUTPUT.
  44.   pinMode(switch1, OUTPUT);         // RFID Card to turn ON PC.
  45.   pinMode(switch2, OUTPUT);         // KeyFob to shut it down.
  46.  
  47.  
  48. }
  49. /*
  50.    Helper routine to dump a byte array as hex values to Serial.
  51. */
  52. void dump_byte_array(byte *buffer, byte bufferSize) {
  53.   read_rfid = "";
  54.   for (byte i = 0; i < bufferSize; i++) {
  55.     read_rfid = read_rfid + String(buffer[i], HEX);
  56.   }
  57. }
  58.  
  59. void On_Switch() {                  // Use this routine when working with Relays and Solenoids etc.
  60.   digitalWrite(switch1, HIGH);
  61.   delay(1000);
  62.   digitalWrite(switch1, LOW);
  63. }
  64.  
  65. void Off_Switch() {                // You can also just use the card to shutdown your PC. This is just for those moments that you really need to shut it down quickly.
  66.   digitalWrite(switch2, HIGH);     // Use this routine when working with Relays and Solenoids etc.
  67.   delay(5000);
  68.   digitalWrite(switch2, LOW);
  69. }
  70.  
  71. void BlueOn() {
  72.  
  73.   digitalWrite(Thing, HIGH);
  74.   mfrc522.PCD_Init();
  75. }
  76.  
  77. void BlueOff() {
  78.  
  79.   digitalWrite(Thing, LOW);
  80.   mfrc522.PCD_Reset();
  81. }
  82.  
  83. void loop() {
  84.  
  85.   while (Serial.available()) {
  86.     BlueCom = Serial.read();
  87.     Serial.println(BlueCom);
  88.     {
  89.  
  90.       if (BlueCom == 1)
  91.         BlueOn();
  92.     }
  93.     {
  94.  
  95.       if (BlueCom == 0)
  96.         BlueOff();
  97.     }
  98.   }
  99.  
  100.   // Look for new cards
  101.   if (!mfrc522.PICC_IsNewCardPresent())
  102.     return;
  103.  
  104.   // Select one of the cards
  105.   if (!mfrc522.PICC_ReadCardSerial())
  106.     return;
  107.   dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  108.  
  109.   Serial.println(read_rfid);
  110.   if (read_rfid == ok_rfid_1) { //ok, turn ON PC.
  111.     digitalWrite(switch1, HIGH);
  112.     delay(1000);
  113.     digitalWrite(switch1, LOW);
  114.   }
  115.   Serial.println(read_rfid);
  116.   if (read_rfid == ok_rfid_2) { //ok, turn OFF PC.
  117.     digitalWrite(switch2, HIGH);
  118.     delay(5000);
  119.     digitalWrite(switch2, LOW);
  120.   }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement