Smart Brankas using RFID, Password and Motion Sensor

About This Project

Smart Brankas with high Security

early notification that can detect movement from human presence using Passive Infra Red (PIR) Sensors, password combinations and RFID

when owner/sneak try to open, the motion sensor will detect and give notification. to open the door, owner must tap the RFID, if true the system will request the Password and owner can input the password from Keypad. if the password true, Relay will LOW and the Solenoid will OPENED and send notification.

What’s Connected

ESP8266
Arduino Mega,
Motion Sensor
RFID
Keypad
LCD
Modul Relay
Solenoid Door Lock
Buzzer
Adaptor 12V.

Triggers & Alerts

Email and Text Message
When the Motion detected, ESP8266 will send notification
image
when Solenoid is opened, ESP8266 will send notification
image

Dashboard Screenshots

image

Photos of the Project

When Motion Sensor Detected
image

When Solenoid Opened
image

Source Code

#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 53
#define RST_PIN 2
#define SENSOR_PIN 12
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>
#define EspSerial Serial1
#define VIRTUAL_CHANNEL 1

// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char passwordCayenne[] = "";
char clientID[] = "";

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key; 

byte nuidPICC[3];
String uid="";

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
Password password = Password( "1234" );

const byte ROWS = 4; 
const byte COLS = 3; 
String passKode="";
int relay = 13;
int buzzer = 32;

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = { 9,8,7,6 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 5,4,3, };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.


// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int proses=1;
int state=0;

ESP8266 wifi(&EspSerial);

void setup()
{
  koneksi();
  pinMode(buzzer, OUTPUT);
  digitalWrite(buzzer, LOW);
  
  
  //----------------------------------RFID
  Serial.begin(9600);
 
  
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522 
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
 
  //--------------------------------------
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
  // initialize the LCD
  lcd.begin();
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad

  // Turn on the blacklight and print a message.
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Selamat Datang");
  lcd.setCursor(0,1);
  lcd.print("Scan Kartu Anda");
}

void loop()
{
   
  
  checkSensor();
  if(proses == 1){
    checkRfid();   
  }
  //---------------------------
  else if(proses == 2){
   keypad.getKey(); 
  }
  else{
  
  }
}

void checkRfid(){
  uid = "";
  // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;

  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    return;
  }

  if (rfid.uid.uidByte[0] != nuidPICC[0] || 
    rfid.uid.uidByte[1] != nuidPICC[1] || 
    rfid.uid.uidByte[2] != nuidPICC[2] || 
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
    //Serial.println(F("A new card has been detected."));

    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
   
    printHex(rfid.uid.uidByte, rfid.uid.size);
    printDec(rfid.uid.uidByte, rfid.uid.size);
    if(uid == "513722945"){
      digitalWrite(buzzer, LOW); //buz mati
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Scan Sukses");
      lcd.setCursor(0,1);
      lcd.print("Masukan Password");
      delay(1000);
      proses = 2;
      passKode="";
      state = 0;      
    }
    else{
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Kartu Salah");
      lcd.setCursor(0,1);
      lcd.print("Scan Ulang Kartu");
      digitalWrite(buzzer, HIGH);
      delay(2000);
     
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Selamat Datang");
      lcd.setCursor(0,1);
      lcd.print("Scan Kartu Anda");
    }
  }
  //else Serial.println(F("Card read previously."));

  // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
}

void koneksi(){

  EspSerial.begin(115200);
  delay(10);
  Cayenne.begin(username, passwordCayenne, clientID, wifi, ssid, wifiPassword);
  
}

void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
  case PRESSED:
  digitalWrite(buzzer, LOW);
  state = state + 1;
  if(state <= 1){
    lcd.clear();
    lcd.setCursor (0,0);
    lcd.print("Tekan # ");
  }
  else{
  lcd.clear();
  lcd.setCursor (0,0);
  lcd.print("Password Anda :");
  }
  if(state > 1){
  lcd.setCursor (0,1);
  passKode+="*";
  lcd.print(passKode);
  }
  switch (eKey){
    case '*': checkPassword(); break;
    case '#': 
    passKode="";
    lcd.clear();
    lcd.setCursor (0,0);
    lcd.print("Password Anda :");
    password.reset(); 
    digitalWrite(buzzer, LOW);
    break;
    default: password.append(eKey);
     }
  }
}

int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;

void checkSensor()
{
  unsigned long currentMillis = millis();
  // Check sensor data every 250 milliseconds
  if (currentMillis - previousMillis >= 250) {
    // Check the sensor state and send data when it changes.
    currentState = digitalRead(SENSOR_PIN);
    if (currentState != previousState) {
      Cayenne.virtualWrite(VIRTUAL_CHANNEL, currentState);
      previousState = currentState;
      if(currentState ==1){
       digitalWrite(buzzer, HIGH); //buz hidup 
      }else{
        //digitalWrite(buzzer, LOW);
      }
      
    }
        previousMillis = currentMillis;
  }
}

void checkPassword(){
  if (password.evaluate()){
    digitalWrite(buzzer, LOW);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Password Cocok");
    lcd.setCursor(0,1);
    lcd.print("Terima Kasih");
    digitalWrite(relay, LOW);
    Cayenne.virtualWrite(5, 1, "digital_sensor", "d");
    passKode="";
    password.reset();
    delay(3000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Pintu Terbuka");
    delay(5000);
    lcd.clear();
    digitalWrite(relay, HIGH);
    Cayenne.virtualWrite(5, 0, "digital_sensor", "d");
    lcd.setCursor(0,0);
    lcd.print("Selamat Datang");
    lcd.setCursor(0,1);
    lcd.print("Scan Kartu Anda");
    state = 0;
    proses = 1;
    //Add code to run if it works
  }else{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Password Salah");
    digitalWrite(relay, HIGH);
    digitalWrite(buzzer, HIGH);
    delay(3000);
    state = 0;
    proses = 1;
    lcd.setCursor(0,0);
    lcd.print("Selamat Datang");
    lcd.setCursor(0,1);
    lcd.print("Scan Kartu Anda");
    
  }
}

void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
    
  }
}

void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
    uid += buffer[i];
  }
}

int statusrelay =digitalRead(relay);

void balikKeAwal(){
  if(statusrelay == 1)
  {
   delay(3000);
   digitalWrite(relay, HIGH);
   Cayenne.virtualWrite(6, 0, "digital_sensor", "d");
   lcd.clear();
  } proses = 1;
  state = 0;
}

void autoLock(){
  lcd.print(F("AutoLock After"));
           for(int i=5; i>0; i--){
            lcd.setCursor (0,1); lcd.print(i);
            delay (1000);
           }      
}
2 Likes

nice project @mfajar_12 and thank you for sharing the project with the community.

1 Like

thank you for helping me sir :slight_smile:

2 Likes