SMART HOME LOCK USING RFID AND OTP WITH BHARAT PI

by shahide in Circuits > Arduino

171 Views, 0 Favorites, 0 Comments

SMART HOME LOCK USING RFID AND OTP WITH BHARAT PI

WhatsApp Image 2023-09-19 at 9.51.57 PM.jpeg

In our ever-advancing world, we're witnessing the transformation of the humble front door into a smart and secure portal. These Smart Door Locks, whether using RFID cards or OTP (One-Time Passwords), redefine home security. RFID card systems provide convenience with a simple swipe or tap, while OTP codes bolster security by allowing single-use access. These technologies enable remote control, entry monitoring, and integration with your smart home setup. Whether opting for RFID or OTP, these innovations are redefining home security, making your door not just an entrance but a dynamic and intelligent guardian of your home.

Supplies

Bharat-Pi.png
Screenshot 2023-09-19 204518.png
Screenshot 2023-09-19 205026.png
Screenshot 2023-09-19 205249.png
Screenshot 2023-09-19 224219.png
Screenshot 2023-09-19 204839.png
Screenshot 2023-09-19 205104.png

COMPONENTS REQUIRED :

  1. Bharat Pi (board)
  2. Servo Motor
  3. RFID Reader
  4. LEDS
  5. Jumper wires
  6. Buzzer Module

Software Part:

We'll be using the Arduino IDE to program the Bharat Pi and you can connect it through a USB Type-C cable. To make sure your computer can recognize the Bharat Pi you might need to install some drivers. The specific drivers you need depend on your computer's operating system.

  1.  Arduino IDE
  2. CH341 serial drivers for Bharat Pi

FEATURES

  1. Dual Authentication: This smart lock combines two layers of security, utilizing RFID card access and OTP codes, ensuring enhanced protection for your home.
  2. RFID Convenience: The RFID card functionality offers a hassle-free way to enter your home. Simply swipe or tap the card for quick access, perfect for daily use.
  3. OTP for Added Security: One-Time Passwords (OTPs) provide an extra level of security. These unique, time-sensitive codes are ideal for situations where you need heightened protection.
  4. Auto-Locking: Some models offer auto-locking features, ensuring your door is securely locked behind you, providing peace of mind.
  5. Customized Access : Assign different access permissions to various users. For instance, family members might have full access, while guests receive limited access for a specific duration.

COMPONENTS EXPLAINATION

BHARAT PI (BOARD) :

we're using the Bharat Pi, a handy device that combines an ESP32 microcontroller with a SimCom A7672S 4G/LTE module. It's like a smart gadget for people who want to create things. Whether you're a student, innovator, startup, or developer, Bharat Pi is perfect for making quick prototypes. It's designed to be super easy to use and fits with all sorts of programs and accessories, just like those for Arduino and ESP32. You can use it to connect wirelessly, either with WiFi or a Sim card for 4G/LTE. Plus, it's tough and reliable, suitable for both testing ideas and even using it in real-life projects.

SERVO MOTOR :

A servo motor is like a little helper that can turn and move things very precisely. It's often used in machines, toys, and even doors to make them go exactly where you want. It's a bit like a robot arm that can follow your instructions, making it great for tasks that need careful control, like opening and closing doors, steering remote-control cars, or moving things with great accuracy.

RFID READER :

An RFID reader is like a special device that talks to two types of cards: "access cards" and "denied cards." The "access card" works like a magic key – when it's near the reader, it opens doors. In contrast, the "denied card" is like a superhero shield; when it's used with the reader, it says, "No entry," and the door stays locked. Essentially, the RFID reader decides who can come in, making it a valuable tool for controlling access and ensuring security.

LEDS :

LEDs (Light-Emitting Diodes) are small, energy-efficient lights that glow when electricity flows through them. They come in different colors and are used in devices like phones, TVs, and as energy-saving bulbs in homes.

JUMPER WIRES AND BUZZER MODULE :

Jumper wires are like flexible electric strings used in electronics projects to connect different components. They make it easy to link things like sensors and LEDs on a breadboard.A buzzer module is a small sound-producing device that beeps or buzzes when you send it an electrical signal. It's often used in projects to create sound alerts or alarms and can make different tones depending on the signal it receives.

CIRCUIT AND CONNECTION

WhatsApp Image 2023-09-21 at 17.54.34.jpg
  1. Connect the RFID SS pin to the (29) GPIO5 Digital I/O pin on the Bharat Pi board.
  2. Connect the RFID SCK pin to the (30) GPIO18 Digital I/O pin on the Bharat Pi board.
  3. Connect the RFID MOSI pin to the (37) GPIO23 Digital I/O pin on the Bharat Pi board.
  4. Connect the RFID MISO pin to the (31) GPIO19 Digital I/O pin on the Bharat Pi board.
  5. Connect the RFID RST pin to the (11) GPIO27 Digital I/O pin on the Bharat Pi board.
  6. Connect the RFID GND (ground) pin to the GND pin on the Bharat Pi board.
  7. Connect the RFID VCC (power) pin to the 3.3V pin on the Bharat Pi board.
  8. Connect the Servo Motor VCC (power) pin to the 5V pin on the Bharat Pi board.
  9. Connect the Servo Motor GND (ground) pin to the GND pin on the Bharat Pi board.
  10. Connect the Servo Motor Signal pin to a Digital I/O pin on the Bharat Pi board, such as pin 33.
  11. Connect the longer leg (anode) of the GREEN LED to a Digital I/O pin on the Bharat Pi board, such as pin 13.
  12. Connect the longer leg (anode) of the RED LED to a Digital I/O pin on the Bharat Pi board, such as pin 15.
  13. Connect the shorter leg (cathode) of the GREEN LED and RED LED to the GND pin on the Bharat Pi board.
  14. Connect the positive terminal of the Buzzer to a Digital I/O pin on the Bharat Pi board, such as pin 2.
  15. Connect the negative terminal of the Buzzer to the GND pin on the Bharat Pi board.

CODE

Screenshot 2023-09-19 220054.png
#include <Arduino.h>
#include <ESP32Servo.h> // Library for controlling Servo motor
#include <SPI.h>     // SPI communication library
#include <MFRC522.h>   // Library for the MFRC522 RFID module

#define SS_PIN SDA    // SS_PIN is defined as SDA
#define RST_PIN 27    // RST_PIN is defined as 27
#define LED_G 13      // Green LED pin
#define LED_R 15      // Red LED pin
#define BUZZER 2      // Buzzer pin

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
Servo myServo;           // Define servo name

int generatedOTP = 0; // Variable to store generated OTP
int greenLEDPin = 13; // Green LED pin
int redLEDPin = 15;  // Red LED pin
int buzzerPin = 2;  // Buzzer pin
int tries = 0;     // Counter for OTP verification attempts

void setup() {
 Serial.begin(115200); // Initialize serial communication at 115200 baud
 randomSeed(analogRead(0)); // Initialize random number generator
 pinMode(greenLEDPin, OUTPUT); // Set green LED pin as output
 pinMode(redLEDPin, OUTPUT);  // Set red LED pin as output
 pinMode(buzzerPin, OUTPUT);  // Set buzzer pin as output
 myServo.attach(33);      // Attaches the servo on pin 33

 SPI.begin();       // Initialize SPI bus
 mfrc522.PCD_Init();   // Initialize MFRC522
 pinMode(LED_G, OUTPUT); // Set green LED pin as output
 pinMode(LED_R, OUTPUT); // Set red LED pin as output
 pinMode(BUZZER, OUTPUT); // Set buzzer pin as output
 noTone(BUZZER);     // Turn off the buzzer initially
 Serial.println("Put your card to the reader..."); // Display message on Serial Monitor
 Serial.println();

 generateOTP(); // Generate the initial OTP
}

void loop() {
 // Look for new cards
 if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
  // Show UID on serial monitor
  Serial.print("UID tag: ");
  String content = "";
  for (byte i = 0; i < mfrc522.uid.size; i++) {
   Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); // Print UID bytes
   Serial.print(mfrc522.uid.uidByte[i], HEX);
   content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")); // Concatenate UID bytes as a string
   content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message: ");
  content.toUpperCase();

  // Check if the RFID card is authorized
  if (content.substring(1) == "96 2C 71 06") { // Change the UID of the authorized card
   Serial.println("Authorized access"); // Display message
   Serial.println();
   delay(500);
   digitalWrite(LED_G, HIGH); // Turn on green LED
   tone(BUZZER, 500); // Sound the buzzer at 500 Hz
   delay(300);
   noTone(BUZZER); // Turn off the buzzer
   myServo.write(180); // Rotate the servo to 180 degrees
   delay(5000); // Wait for 5 seconds
   myServo.write(0); // Return the servo to the initial position (0 degrees)
   digitalWrite(LED_G, LOW); // Turn off green LED
  } else {
   Serial.println("Access denied"); // Display message
   digitalWrite(LED_R, HIGH); // Turn on red LED
   tone(BUZZER, 300); // Sound the buzzer at 300 Hz
   delay(1000);
   digitalWrite(LED_R, LOW); // Turn off red LED
   noTone(BUZZER); // Turn off the buzzer
  }

  // Clear the UID read
  mfrc522.PICC_HaltA();
 }

 // Print the generated OTP to the Serial Monitor
 Serial.println("Generated OTP: " + String(generatedOTP));

 // Wait for user input
 Serial.println("Enter OTP:");
 while (!Serial.available()) {
  // Wait for user input
 }

 // Read user input
 String userOTPString = Serial.readString();
 int userOTP = userOTPString.toInt();

 // Verify OTP
 if (userOTP == generatedOTP) {
  Serial.println("OTP Verified!"); // Display message

  // Rotate the servo to 180 degrees
  myServo.write(180);
  digitalWrite(greenLEDPin, HIGH); // Turn on green LED
  delay(5000); // Wait for 5 seconds

  // Return the servo to the initial position (e.g., 0 degrees)
  myServo.write(0);

  // Blink green LED
  digitalWrite(greenLEDPin, LOW); // Turn off green LED
  digitalWrite(redLEDPin, HIGH); // Turn on red LED

  // Generate a new OTP
  generateOTP();
 } else {
  Serial.println("OTP Verification Failed. Try Again."); // Display message

  // Beep the buzzer when access is denied
  digitalWrite(greenLEDPin, LOW); // Turn off green LED
  delay(500);
  digitalWrite(redLEDPin, HIGH); // Turn on red LED
  digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
  delay(500);
  digitalWrite(buzzerPin, LOW); // Turn off the buzzer
  digitalWrite(redLEDPin, LOW); // Turn off red LED
  tries++;

  // Beep for a longer duration when access is denied after 3 tries
  if (tries == 3) {
   Serial.println("OTP invalid"); // Display message
   digitalWrite(redLEDPin, HIGH); // Turn on red LED
   digitalWrite(buzzerPin, HIGH); // Turn on the buzzer for a longer duration
   delay(1000);
   digitalWrite(buzzerPin, LOW); // Turn off the buzzer
   digitalWrite(redLEDPin, LOW); // Turn off red LED
  }
 }
}

void generateOTP() {
 // Generate a new 6-digit OTP
 generatedOTP = 0;
 for (int i = 0; i < 6; i++) {
  generatedOTP = generatedOTP * 10 + random(0, 10); // Generate a random digit between 0 and 9
 }
}

GitHub

WORKING

WhatsApp Image 2023-09-19 at 9.30.32 PM.jpeg
WhatsApp Image 2023-09-19 at 10.13.16 PM.jpeg
WhatsApp Image 2023-09-19 at 10.13.17 PM.jpeg
WhatsApp Image 2023-09-21 at 21.29.46.jpg
WhatsApp Image 2023-09-21 at 21.26.39.jpg

In the smart home lock they having two distinct authentication methods: RFID card detection and OTP generation. For RFID authentication, when the user presses the lowercase letter 'a,' in the serial monitor the system initiates RFID card detection. Upon placing an RFID access card on the RFID reader, the system responds by activating a servo motor, causing it to rotate 180 degrees, and illuminating a Green LED as an indicator of successful card detection. After a brief 5-second pause, the servo motor returns to its initial position at 0 degrees. In cases where an unauthorized RFID card is presented, the system denies access by keeping the servo motor stationary and turning on a Red LED. On the other hand, for OTP authentication, the user initiates the process by pressing the capital letter 'A,' in the serial monitor and the system to generate a random one-time password (OTP) displayed on the serial monitor. To gain access, the user must accurately input the displayed OTP. If they succeed, a Green LED is activated, and the servo motor rotates 180 degrees before returning to 0 degrees after a 5-second delay. However, if the user inputs an incorrect OTP, the system displays an OTP verification failure message and activates the Red LED to signify denied access.

VIDEO

Smart Home Lock Using OTP and RFID