RFID Access Control System, Arduino and App Inventor

print


#include <SPI.h>
#include <MFRC522.h>
#include <SoftwareSerial.h>

#include <Wire.h>
#include "rgb_lcd.h"

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
SoftwareSerial bluetoothSerial(6, 5); // RX, TX Bluetooth 

rgb_lcd lcd;

void setup()
{
  Serial.begin(9600); // Initialise serial communication at 9600bps
  bluetoothSerial.begin(9600); // Initialise bluetooth communication at 9600kbps
  
  while (!Serial) {
    ; // Wait for serial port to connect
  }
  
  while (!bluetoothSerial) {
    ; // Wait for bluetooth  port to connect. 
   
  }
    
  
  // Initialising RFID module
  SPI.begin(); //Initialise SPI bus
  mfrc522.PCD_Init(); // Initialise MFRC522

  // Initialising RGB LCD
  lcd.begin(16, 2);   //Initialise LCD screen 
  lcd.clear();
  
}




void loop()
{
  
  printToLCD("Pass your card", "Please scan now", 255,255,255);  //Print to the LCD screen the message "Pass your card", "Please scan now"

  waitForCard(); //Execution of the subroutine waitForCard
  waitForMessageFromBluetooth(); //Execution of the subroutine waitForMessageFromBluetooth

  
}

void waitForCard(){
   
  // Look for new RFID card and since a PICC placed get the serial
      if (  mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
         
  //Get RFID card UID
          String UID=cardUID(); //Incoming data as string
      printToLCD("Card read OK", "Please wait", 255,255,255); //Print to the LCD screen the message "Card read OK", "Please wait"
    
  // Send UID string to Android 
 //  bluetoothSerial.print('b', HEX);      
      bluetoothSerial.print(UID);
      delay(5000); //wait for 5sec
      }
    
 }
 // My Android sends a string to my Arduino via Bluetooth and the Arduino will perform a series of actions depending on the input received by the Android
void  waitForMessageFromBluetooth(){
   if(bluetoothSerial.available()> 0) {
     String incomingMEssage = "";
     
      while (bluetoothSerial.available()> 0){
      
        int input = bluetoothSerial.read(); // Save the received number to input
        char letter = input;
        incomingMEssage = incomingMEssage + letter; 
        
     
      }
      handleMessage(incomingMEssage);
  }
}
  void handleMessage(String action){
 
      if(action ==  "allow_pass"){
          allowUserToPass(); //calling void allowUserToPass()
         
        } else if(action == "deny_pass"){
          denyUserToPass(); //calling void denyUserToPass()
          
        } 
        else {
          elseCase(); //calling void elseCase()
         
        }
   
}
//Get Android response (access), printing appropriate message to LCD (case -> Access Granted)
void allowUserToPass(){
  //open door and write appropriate message to lcd
  printToLCD("Access Granted", "", 0, 255, 0);
  delay(10000); //wait for 10sec
}
//Get Android response (no access), printing appropriate message to LCD (case -> Access Denied)
void denyUserToPass(){
  printToLCD("Access Denied", "", 255, 0, 0);
    delay(10000); //wait for 10sec
}
//Get Android response (any other case), printing appropriate message to LCD (case -> Error)
void elseCase(){
 printToLCD("Error", "", 218, 10, 180);
 delay(10000); //wait for 10sec
}

// ------------------------------------------------------------------------------------------
// Creation of the RFID card UID string in order to create the sequence of characters of the UID and provide it to the Android
String cardUID() {
  String content= "";
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? "_0" : "_")); //Appending this parameter to the string content.
    //If the byte is less than 10 (one-digit number)then place O in front of it, each byte pair is connected with the next one with underscore
    content.concat(String(mfrc522.uid.uidByte[i], HEX)); //The UID in HEX
  }
  return content;
}

//Printing messages to the LCD RGB screen
void printToLCD(String line0String, String line1String, int R, int G, int B) {
    lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner
    lcd.setRGB(R, G, B);
    lcd.setCursor(0, 0);
    lcd.print(line0String);
    lcd.setCursor(0, 1);
    lcd.print(line1String);
}

// ------------------------------------------------------------------------------------------

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.