RFID and Keypad based Security Device with Alert System using Arduino & ESP32
This project is equipped with two security features that help users to protect their rooms from unauthorized users. An additional feature, alert notifications to the users through Telegram that allows them to know their room door status was also included.
Components and supplies
5mm Red LED
Relay Module 5V 1-Channel
16x2 I2C LCD
3.7V 2000mAh Li-Po Battery
Magnetic Door Sensor
5V Piezo Buzzer
Slot Battery Casing
12V Solenoid Door Lock
Male/Male Jumper Wires
Arduino Uno Rev3
Membrane 3x4 Matrix Keypad
Mifare RC522 RFID Kit
NodeMCU ESP32
Male/Female Jumper Wires
1k/10k Resistor
Push Button
Tools and machines
60W Soldering Iron
Screwdrivers
Apps and platforms
Telegram
Arduino IDE 1.8
Telegram BotFather (Bot)
Autodesk Tinkercad
Telegram bot IDBot
Project description
Code
Arduino_Door Lock
cpp
Code for door lock (Upload at Arduino)
1#include <MFRC522.h> 2#include <SoftwareSerial.h> 3#include <Wire.h> 4#include <SPI.h> 5#include <Keypad.h> 6#include <Servo.h> 7#include <LiquidCrystal_I2C.h> 8 9MFRC522 mfrc522 (10, 9); 10LiquidCrystal_I2C lcd(0x27, 16, 2); 11 12#define Password_Length 10 13 14char userInput[Password_Length]; 15char Master[Password_Length] = "123123123"; 16char customKey; 17byte pressCount = 0; 18int position = 0; 19const int ButtonPin = 4; 20int buttonState = 0; 21int Doorrelay = 3; 22int RedPinLock = 5; 23int BuzzPin = 8; 24 25#define NOTE_G4 392 26#define NOTE_C5 523 27#define NOTE_G5 784 28#define NOTE_C6 1047 29 30int TrueMelody[] = {NOTE_G5, NOTE_C6}; 31int TrueNoteDurations[] = {12, 8}; 32 33int FalseMelody[] = {NOTE_C6, NOTE_G5}; 34int FalseNoteDurations[] = {12, 8}; 35 36#define playTrueMelody() playMelody(TrueMelody, TrueNoteDurations, 2) 37#define playFalseMelody() playMelody(FalseMelody, FalseNoteDurations, 2) 38 39const byte ROWS = 4; 40const byte COLS = 3; 41 42char twelveKeys[ROWS][COLS] = { 43 {'1', '2', '3'}, 44 {'4', '5', '6'}, 45 {'7', '8', '9'}, 46 {'*', '0', '#'} // * = CLEAR, # = ENTER 47}; 48 49const byte rowPins[ROWS] = { 2, A3, A2, A1 }; 50const byte colPins[COLS] = { A0, 6, 7 }; 51 52Keypad customKeypad = Keypad( makeKeymap(twelveKeys), rowPins, colPins, ROWS, COLS ); 53 54String tagUID = "93 B0 83 0A"; // String to store UID of tag. 55boolean RFIDMode = true; // boolean to change modes 56boolean NormalMode = true; // boolean to change modes 57 58void setup() 59{ 60 lcd.init(); 61 lcd.backlight(); 62 lcd.clear(); 63 Serial.begin(9600); 64 Serial.println("Scan your Tag..... "); 65 pinMode(ButtonPin, INPUT); 66 pinMode(RedPinLock, OUTPUT); 67 pinMode(BuzzPin, OUTPUT); 68 pinMode(Doorrelay, OUTPUT); 69 LockedPosition(true); 70 SPI.begin(); // Init SPI bus 71 mfrc522.PCD_Init(); // Init MFRC522 72 lcd.begin(16, 2); 73 lcd.setCursor(0, 0); 74 lcd.print(" WELCOME !!"); 75 lcd.setCursor(0, 1); 76 lcd.print(" SCAN THE TAG"); 77} 78 79// MAIN PROGRAM LOOP 80void loop() 81{ 82 buttonState = digitalRead(ButtonPin); 83 84 if (buttonState == HIGH) 85 { 86 digitalWrite(Doorrelay, HIGH); 87 delay(7000); 88 digitalWrite(Doorrelay, LOW); 89 mfrc522.PCD_Init(); // Init MFRC522 90 } 91 else 92 { 93 digitalWrite(Doorrelay, LOW); 94 } 95 96 if ( NormalMode == true ) //System will first look for mode 97 { 98 if ( RFIDMode == true ) //Function to receive message 99 { 100 // Look for new cards 101 if ( ! mfrc522.PICC_IsNewCardPresent() ) 102 { 103 return; 104 } 105 // Select one of the cards 106 if ( ! mfrc522.PICC_ReadCardSerial() ) 107 { 108 return; 109 } 110 // Reading from the card 111 String tag = "" ; 112 for ( byte j = 0; j < mfrc522.uid.size; j++) 113 { 114 tag.concat(String(mfrc522.uid.uidByte[j] < 0x10 ? " 0" : " " )); 115 tag.concat(String(mfrc522.uid.uidByte[j], HEX)); 116 } 117 tag.toUpperCase(); 118 // Checking the card 119 if (tag.substring(1) == tagUID ) 120 { 121 playTrueMelody(); 122 Serial.println(); 123 Serial.println("Tag Matched !!"); 124 Serial.println("Enter Your Password: "); 125 Serial.println(" "); 126 lcd.clear(); 127 lcd.setCursor(0,0); 128 lcd.print("Your Tag Matched"); 129 lcd.setCursor(0,1); 130 lcd.print(" Access Granted"); 131 delay(500); 132 lcd.clear(); 133 lcd.setCursor(0,0); 134 lcd.print(" Type Password:"); 135 RFIDMode = false; // Make RFID mode false 136 } 137 else 138 { 139 playFalseMelody(); 140 Serial.println(" "); 141 Serial.println("Wrong Tag Shown"); 142 Serial.println(" Access Denied"); 143 tone(BuzzPin, 400, 500); 144 lcd.clear(); 145 lcd.setCursor(0,0); 146 lcd.print(" Wrong Tag !!"); 147 lcd.setCursor(0,1); 148 lcd.print(" Access Denied"); 149 delay(500); 150 lcd.clear(); 151 lcd.setCursor(0, 0); 152 lcd.print(" WELCOME !!"); 153 lcd.setCursor(0, 1); 154 lcd.print(" SCAN THE TAG"); 155 RFIDMode = true; 156 } 157 } 158 159 // If RFID mode is false, it will look for keys from keypad 160 161 if ( RFIDMode == false ) 162 { 163 customKey = customKeypad.waitForKey(); 164 165 if ( customKey != NO_KEY && customKey != '*' && customKey != '#') 166 { 167 userInput[pressCount] = customKey; 168 lcd.setCursor(pressCount + 4, 1); 169 lcd.print("*"); 170 pressCount++; 171 tone(BuzzPin, 400, 100); 172 } 173 else if ( customKey == '*' ) 174 { 175 tone(BuzzPin, 400, 100); 176 lcd.clear(); 177 clearData(); 178 RFIDMode = true; 179 } 180 else if ( customKey == '#' ) 181 { 182 tone(BuzzPin, 400, 100); 183 lcd.clear(); 184 lcd.setCursor(0,0); 185 tone(BuzzPin, 800, 1000); 186 lcd.print("PASSWORD INVALID"); 187 delay(2000); 188 lcd.clear(); 189 clearData(); 190 RFIDMode = true; 191 } 192 if ( pressCount == 9 ) 193 { 194 lcd.clear(); 195 waitHere(); 196 } 197 } 198 } 199} 200 201// THIS FUNCTION CHECKS THE PASSWORD STRING 202void waitHere() 203{ 204 lcd.setCursor(0,0); 205 lcd.print(" Type Password:"); 206 lcd.setCursor(0,1); 207 lcd.print(" ********* "); 208 209 customKey = customKeypad.waitForKey(); // Program will halt here until a key is pushed 210 211 if ( customKey != NO_KEY && customKey == '#' ) // The ENTER is pushed and the password goes through the routine 212 { 213 lcd.clear(); 214 lcd.setCursor(0,0); 215 216 if ( !strcmp(userInput, Master)) 217 { 218 lcd.setCursor(0,0); 219 lcd.print(" ACCESS GRANTED"); 220 lcd.setCursor(0,1); 221 lcd.print(" WELCOME !!"); 222 LockedPosition(false); 223 digitalWrite(Doorrelay, HIGH); 224 delay(7000); 225 lcd.clear(); 226 clearData(); 227 RFIDMode = true; 228 } 229 else if ( strcmp(userInput, Master)) 230 { 231 lcd.setCursor(0,0); 232 lcd.print(" ACCESS DENIED"); 233 LockedPosition(true); 234 digitalWrite(Doorrelay, LOW); 235 tone(BuzzPin, 800, 2000); 236 delay(500); 237 lcd.clear(); 238 clearData(); 239 RFIDMode = true; 240 } 241 } 242 243 if (customKey != NO_KEY && customKey == '*') // If the CLEAR button is pushed, the data is cleared and the program starts over. 244 { 245 lcd.clear(); 246 clearData(); 247 RFIDMode = true; 248 } 249 if (customKey != NO_KEY && customKey == '0') // This numberical button has no purpose and does nothing when pressed. 250 { 251 waitHere(); 252 } 253 254 if (customKey != NO_KEY && customKey == '1') // This numberical button has no purpose and does nothing when pressed. 255 { 256 waitHere(); 257 } 258 259 if (customKey != NO_KEY && customKey == '2') // This numberical button has no purpose and does nothing when pressed. 260 { 261 waitHere(); 262 } 263 264 if (customKey != NO_KEY && customKey == '3') // This numberical button has no purpose and does nothing when pressed. 265 { 266 waitHere(); 267 } 268 269 if (customKey != NO_KEY && customKey == '4') // This numberical button has no purpose and does nothing when pressed. 270 { 271 waitHere(); 272 } 273 274 if (customKey != NO_KEY && customKey == '5') // This numberical button has no purpose and does nothing when pressed. 275 { 276 waitHere(); 277 } 278 279 if (customKey != NO_KEY && customKey == '6') // This numberical button has no purpose and does nothing when pressed. 280 { 281 waitHere(); 282 } 283 if (customKey != NO_KEY && customKey == '7') // This numberical button has no purpose and does nothing when pressed. 284 { 285 waitHere(); 286 } 287 288 if (customKey != NO_KEY && customKey == '8') // This numberical button has no purpose and does nothing when pressed. 289 { 290 waitHere(); 291 } 292 293 if (customKey != NO_KEY && customKey == '9') // This numberical button has no purpose and does nothing when pressed. 294 { 295 waitHere(); 296 } 297} 298 299 300void LockedPosition(int locked) 301{ 302 if (locked) 303 { 304 digitalWrite(RedPinLock, HIGH); 305 digitalWrite(Doorrelay, LOW); 306 } 307 else 308 { 309 digitalWrite(RedPinLock, LOW); 310 digitalWrite(Doorrelay, HIGH); 311 } 312 delay(7000); 313} 314 315 316// CLEARS THE ARRAY DATA, STARTS ALL OVER AGAIN 317void clearData() 318{ 319 while ( pressCount != 0 ) 320 { 321 userInput[pressCount--] = 0; // Clears out the user input data, both digit and string data are reset to zero 322 } 323 setup(); // Returns program back to the beginning 324} 325 326void playMelody(int *melody, int *noteDurations, int notesLength) 327{ 328 pinMode(BuzzPin, OUTPUT); 329 330 for (int thisNote = 0; thisNote < notesLength; thisNote++) { 331 int noteDuration = 1000 / noteDurations[thisNote]; 332 tone(BuzzPin, melody[thisNote], noteDuration); 333 int pauseBetweenNotes = noteDuration * 1.30; 334 delay(pauseBetweenNotes); 335 noTone(BuzzPin); 336 } 337}
ESP32_Door Status
cpp
Code for door status (Upload at ESP32)
1#include <WiFi.h> 2#include <WiFiClientSecure.h> 3#include <UniversalTelegramBot.h> 4#include <ArduinoJson.h> 5 6// Set GPIOs for LED and reedswitch 7const int reedSwitch = 19; 8const int led = 2; //optional 9 10// Detects whenever the door changed state 11bool changeState = false; 12 13// Holds reedswitch state (1 = opened, 0 = close) 14bool state; 15String doorState; 16 17// Auxiliary variables (it will only detect changes that are 1500 milliseconds apart) 18unsigned long previousMillis = 0; 19const long interval = 1500; 20 21const char* ssid = "Huawei Y9 Prime"; 22const char* password = "password"; 23 24// Telegram BOT 25#define BOTtoken "5622530230:AAEiy-XuQHy4a2knwZAPl5mBe_QoGl56950" // Bot Token (From Botfather) 26 27#define CHAT_ID "1394606528" 28 29WiFiClientSecure client; 30UniversalTelegramBot bot(BOTtoken, client); 31 32// Runs whenever the reedswitch changes state 33ICACHE_RAM_ATTR void changeDoorStatus() { 34 Serial.println("State changed"); 35 changeState = true; 36} 37 38void setup() { 39 // Serial port for debugging purposes 40 Serial.begin(115200); 41 42 // Read the current door state 43 pinMode(reedSwitch, INPUT_PULLUP); 44 state = digitalRead(reedSwitch); 45 46 // Set LED state to match door state 47 pinMode(led, OUTPUT); 48 digitalWrite(led, !state); 49 50 // Set the reedswitch pin as interrupt, assign interrupt function and set CHANGE mode 51 attachInterrupt(digitalPinToInterrupt(reedSwitch), changeDoorStatus, CHANGE); 52 53 // Connect to Wi-Fi 54 WiFi.mode(WIFI_STA); 55 WiFi.begin(ssid, password); 56 client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org 57 while (WiFi.status() != WL_CONNECTED) { 58 delay(5000); 59 Serial.print("."); 60 } 61 Serial.println(""); 62 Serial.println("WiFi connected"); 63 64 bot.sendMessage(CHAT_ID, "Bot started up", ""); 65} 66 67void loop() { 68 if (changeState){ 69 unsigned long currentMillis = millis(); 70 if(currentMillis - previousMillis >= interval) { 71 previousMillis = currentMillis; 72 // If a state has occured, invert the current door state 73 state = !state; 74 if(state) { 75 doorState = "open"; 76 } 77 else{ 78 doorState = "closed"; 79 } 80 digitalWrite(led, !state); 81 changeState = false; 82 Serial.println(state); 83 Serial.println(doorState); 84 85 //Send notification 86 bot.sendMessage(CHAT_ID, "The door is " + doorState, ""); 87 } 88 } 89}
Downloadable files
Libraries
Download all libraries before start the project
libraries.zip
Project Library (Keypad, RFID, LCD)
Library for 3x4 Keypad, RFID and 16x2 I2C LCD
Project Library.zip
Comments
Only logged in users can leave comments