Smart Bank Security System using ESP32

  • view59
  • like0
  • comment0

Hello friends, I hope you all are doing great. Welcome to another exciting project in the ESP32 learning series. In today’s tutorial, we will design a Smart Bank Security System using ESP32.

The evolution of IoT has improved the security systems manifolds. Modern security systems have real-time communication with the servers and upload sensitive data to the cloud. It enables the security personnel to constantly monitor and act immediately in case of any breach.

We can use different IoT modules for designing security systems i.e. Raspberry Pi, STM32, ESP32, BeagleBone etc. For today’s project, we are going to use an ESP32 Microcontroller board to provide WiFi capability to our project.

Components Free Worldwide Shipping

Project Overview/Working

Here’s the final output of today’s project i.e. Smart Bank Security System using ESP32:

As you can see in the above figure, we have designed a Bank model using acrylic sheets. This model will have 3 sections named:

  1. Counter Section: To handle Bank Customers.
  2. Employees Section: For Bank Employees.
  3. Vault Section: A highly secured Bank Vault to protect the money.

In the below figure, I have labelled these 3 sections:

Based on the functionality of these sections, we have added 2 modes in our project, named:

  1. Day Mode: In this mode, the Counter & Employees section will operate normally i.e. people can enter/leave these premises, while the vault section will be highly secured and only authorized persons with the PinCode will be allowed to enter.
  2. Night Mode: In this mode, all 3 sections will be completely secured and authorities will be alerted if any unauthorized entry is detected.

Security Sensors

We have used the below 5 sensors to secure our Bank:

  • Fire Sensor: To detect the fire.
  • Smoke Sensor: To detect the smoke.

  • Vault IR Sensor: To detect unauthorized entry in the Vault.

  • Employee IR Sensor: To detect unauthorized entry in the Employee Section.

  • Counter IR Sensor: To detect unauthorized entry in the Counter Section.

IR Sensors are placed at the doors of these sections to constantly monitor the people’s flow.

Keypad for Vault PinCode

The Bank Vault is the most secure section of the bank as it stores money, so only bank officials should be allowed in this section. For that purpose, we have placed a Keypad at the door with an authorized PinCode. The vault will be unlocked only if the correct PinCode is entered using the Keypad. If someone enters the wrong PinCode, the Vault will get locked and authorities will be informed via WebServer.

ESP32 WebServer

The brain of this project is ESP32 Microcontroller board, shown in the below figure:

We have created a WebServer in the ESP32 Microcontroller Board. This WebServer will send the sensors’ data and notifications/alarms to the control room. The WebServer interface is shown in the below figure:

Components Used

We have used the below components for designing this Smart Bank Security System using ESP32:

  • ESP32 Microcontroller Board.
  • I2C LCD 16x2
  • Keypad 4x4
  • Fire Sensor
  • Smoke Sensor
  • IR Sensor x 3
  • Jumper Wires
  • Breadboard

I hope now you have understood what we are trying to design and the components required to design it. So, let’s interface these components together:

Circuit Diagram

The brain of our project is the ESP32 Microcontroller board. So, we need to interface all of our modules with the ESP32 board. Let’s first have a look at their circuit diagrams one by one, after that will design the ESP32 programming code.

ESP32 with I2C LCD

The Circuit Diagram of interfacing ESP32 with I2C LCD is shown in the below figure:

As you can see in the above figure, we have connected the SDA and SCL pins of LCD with Pins 21 and 22 of ESP32 respectively. The below table shows the ESP32 LCD Pinout:

ESP32

I2C LCD 16x2

GND

GND

VIN

Vcc

Pin 21

SDA

Pin 22

SCL

ESP32 with Keypad 4x4

We have used a 4x4 matrix Keypad for Vault PinCode authentication. 4x4 Keypad has 8 pins in total and we can connect these pins with any 8 I/O Pins of ESP32, as shown in the below figure:

ESP32 with Sensors

Lastly, we need to interface the sensors with ESP32 board. The Vcc and GND pins of all these sensors needs to be connected with the Vin and GND of the ESP32 board. The connection of Signal Pins of these sensors is shown in the below table:

ESP32

Sensors

Pin 23

Fire Sensor OUT Pin

Pin 18

Smoke Sensor OUT Pin

Pin 19

Vault IR Sensor OUT Pin

Pin 35

Employee Section IR Sensor OUT Pin

Pin 34

Counter Section IR Sensor OUT Pin

 

These connections on the real hardware are shown in the below figure:

We are done with all of our connections, now let’s design the Programming Code to implement our logics:

ESP32 Programming Code

We have used Arduino IDE to write the programming code for ESP32. Here’s the complete code:

 

#include <WiFi.h>

#include <LiquidCrystal_I2C.h>

#include <Keypad.h>

 

const byte ROWS = 4;

const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {

  {'1','2','3','A'},

  {'4','5','6','B'},

  {'7','8','9','C'},

  {'*','0','#','D'}

};

 

byte rowPins[ROWS] = {13, 12, 14, 27};

byte colPins[COLS] = {26, 25, 33, 32};

Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

 

int lcdColumns = 16;

int lcdRows = 2;

LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);  

 

const char* ssid = "SD-549";

const char* password = "123123123";

WiFiServer server(80);

 

String header;

String output26State = "off";

String output27State = "off";

 

char* vaultPinCode = "1234";

int   position = 0;

int   checkPinCode = 0;

String vaultStatus = "Secure";

String employeeSection = "secure";

String counterSection = "secure";

String dayMode = "Day Mode Activated.";

int dayModeCheck = 0;

 

unsigned long currentTime = millis();

unsigned long previousTime = 0;

const long timeoutTime = 2000;

 

int vaultIR = 19;

int vaultIRValue = 0;

String vaultIRText = "Deactivated";

 

int employeeIR = 35;

int employeeIRValue = 0;

String employeeIRText = "Deactivated";

 

int counterIR = 34;

int counterIRValue = 0;

String counterIRText = "Deactivated";

 

int fireSensor = 23;

int fireSensorValue = 0;

String fireSensorText = "Deactivated";

 

int smokeSensor = 18;

int smokeSensorValue = 0;

String smokeSensorText = "Deactivated";

 

void setup() {

  Serial.begin(115200);

 

  pinMode(vaultIR, INPUT_PULLUP);

  pinMode(employeeIR, INPUT_PULLUP);

  pinMode(counterIR, INPUT_PULLUP);

  pinMode(fireSensor, INPUT_PULLUP);

  pinMode(smokeSensor, INPUT_PULLUP);

  lcd.init();                  

  lcd.backlight();

 

  lcd.setCursor(0, 0);

  lcd.print("Connecting to ");

  lcd.setCursor(0,1);

  lcd.print(ssid);

 

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }

 

  lcd.clear();

  lcd.setCursor(0, 0);

  lcd.print("WiFi connected.");

  delay(2000);

  lcd.clear();

  lcd.print("IP address: ");

  lcd.setCursor(0,1);

  lcd.print(WiFi.localIP());

  server.begin();

  customKeypad.addEventListener(keypadEvent);

}

 

void loop(){

 

  char customKey = customKeypad.getKey();

  SensorChecks();

 

  WiFiClient client = server.available();

 

  if (client) {

    currentTime = millis();

    previousTime = currentTime;

    Serial.println("New Client.");

    String currentLine = "";

 

    while (client.connected() && currentTime - previousTime <= timeoutTime) {  

      currentTime = millis();

      if (client.available()) {

        char c = client.read();

        Serial.write(c);

        header += c;

 

        if (c == '\n') {

          if (currentLine.length() == 0) {

            client.println("HTTP/1.1 200 OK");

            client.println("Content-type:text/html");

            client.println("Connection: close");

            client.println();

           

            if (header.indexOf("GET /daymode") >= 0) {

              dayModeCheck = 0;

              dayMode = "Day Mode Activated.";

            } else if (header.indexOf("GET /nightmode") >= 0) {

              dayModeCheck = 1;

              dayMode = "Night Mode Activated.";

            }

           

            // Display the HTML web page

            client.println("<!DOCTYPE html><html>");

            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");

            client.println("<meta http-equiv=\"refresh\" content=\"2\" >");

            client.println("<link rel=\"icon\" href=\"data:,\">");

            client.println("<title>Bank Security System</title>");

            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");

            client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");

            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");

            client.println(".button2 {background-color: #555555;}");

            client.println("#sensors {font-family: Arial, Helvetica, sans-serif;border-collapse: collapse;width: 100%;}");

            client.println("#sensors td, #sensors th {  border: 1px solid #ddd;  padding: 8px;}");

            client.println("#sensors tr:nth-child(even){background-color: #f2f2f2;}");

            client.println("#sensors tr:nth-child(odd){background-color: white;}");

            client.println("#sensors tr:hover {background-color: #ddd;}");

            client.println("#sensors th {padding-top: 12px;padding-bottom: 12px;text-align: left; background-color: #04AA6D;color: white;}");

            client.println("#sensorsName {padding-top: 12px;padding-bottom: 12px;text-align: left; background-color: #04AA6D;color: white;}");

            client.println("</style></head>");

           

            // Web Page Heading

            client.println("<body style=\"background: antiquewhite;\"><h1 style=\"font-size: 50px;\">Bank Security System</h1>");

            client.println("<center><table><tr><td>");

                 

            if (dayModeCheck == 0) {

              client.println("<p><a href=\"/nightmode\"><button class=\"button\">Day Mode</button></a></p>");

            } else {

              client.println("<p><a href=\"/daymode\"><button class=\"button button2\">Day Mode</button></a></p>");

            }

 

            client.println("</td><td>");    

            if (dayModeCheck == 1) {

              client.println("<p><a href=\"/daymode\"><button class=\"button\">Night Mode</button></a></p>");

            } else {

              client.println("<p><a href=\"/nightmode\"><button class=\"button button2\">Night Mode</button></a></p>");

            }

            client.println("</td></tr></table></center>");  

               

            if (dayModeCheck == 0) {

              client.println("<span style =\"font-size: 30px; color: green;\">" + dayMode + "</span>");

            }else{

              client.println("<span style =\"font-size: 30px; color: red;\">" + dayMode + "</span>");

            }

 

            client.println("<p style =\"font-size: 30px;font-weight: bold;\">Vault Status: ");            

            if (checkPinCode == 0) {

              client.println("<span style =\"color: green;\">" + vaultStatus + "</span>");

            }else if(checkPinCode == 1){

              client.println("<span style =\"color: darkorchid;\">" + vaultStatus + "</span>");

            }else if(checkPinCode == 2){

              client.println("<span style =\"color: red;\">" + vaultStatus + "</span>");

            }

            client.println("</p>");

           

            if (fireSensorValue == LOW) {

            client.println("<p style =\"font-size: 30px;font-weight: bold; color: red;\">Fire Detected. </p>");

            }

                       

            if (smokeSensorValue == LOW) {

            client.println("<p style =\"font-size: 30px;font-weight: bold; color: red;\">Smoke Detected. </p>");

            }

 

            client.println("<table id=\"sensors\"><tr><th>Sensors</th>");

            client.println("<th>Vault Section</th>");

            client.println("<th>Employees Section</th>");

            client.println("<th>Counters Section</th></tr>");

            client.println("<tr><td id=\"sensorsName\">IR Sensors</td>");

            client.println("<td>" + vaultIRText + "</td>");

            client.println("<td>" + employeeIRText + "</td>");

            client.println("<td>" + counterIRText + "</td></tr>");

            client.println("<tr><td id=\"sensorsName\">Fire Sensor</td>");

            client.println("<td colspan=\"3\" >" + fireSensorText + "</td></tr>");

            client.println("<tr><td id=\"sensorsName\">Smoke Sensor</td>");

            client.println("<td colspan=\"3\" >" + smokeSensorText + "</td></tr>");

            client.println("</table>");

            client.println("</body></html>");

           

            // The HTTP response ends with another blank line

            client.println();

            // Break out of the while loop

            break;

          } else { // if you got a newline, then clear currentLine

            currentLine = "";

          }

        } else if (c != '\r') {  // if you got anything else but a carriage return character,

          currentLine += c;      // add it to the end of the currentLine

        }

      }

    }

   

    header = "";

    client.stop();

    Serial.println("Client disconnected.");

    Serial.println("");

  }

}

void keypadEvent(KeypadEvent key){

    switch (customKeypad.getState()){

    case PRESSED:    

        if(checkPinCode == 1) {

          lcd.print("*");

        }

        if (key == '#') {

          lcd.clear();

          lcd.setCursor(0,0);

          lcd.print("Enter PinCode:");

          lcd.setCursor(6,1);

          vaultStatus = "Authentication in Process";

          position = 0;

          checkPinCode = 1;

        }

        if (key == vaultPinCode[position]) {

          position++;

        }

        if (key == '*') {

          if (position == 4) {

            lcd.clear();

            lcd.setCursor(0,0);

            lcd.print("Authentication");

            lcd.setCursor(0,1);

            lcd.print("Successful.");

            vaultStatus = "Authentication Successful.";

            delay(3000);

 

            lcd.clear();

            lcd.setCursor(0,0);

            lcd.print("Vault Unlocked.");

            vaultStatus = "Vault Unlocked.";

            position = 0;

            checkPinCode = 0;

          }else{            

            lcd.clear();

            lcd.setCursor(0,0);

            lcd.print("Authentication");

            lcd.setCursor(0,1);

            lcd.print("Failed.");

            vaultStatus = "Incorrect PinCode.";

            delay(3000);

 

            lcd.clear();

            lcd.setCursor(0,0);

            lcd.print("Vault Locked.");

            vaultStatus = "Vault Locked.";

            checkPinCode = 2;

          }

        }

        break;

 

    case RELEASED:

        if (key == '*') {

        }

        break;

 

    case HOLD:

        if (key == '*') {

        }

        break;

    }

}

void SensorChecks(){

 

  smokeSensorValue = digitalRead(smokeSensor);

  if(smokeSensorValue == LOW) {

    smokeSensorText = "Smoke Detected";

  }else{

    smokeSensorText = "Secure";

  }

 

  fireSensorValue = digitalRead(fireSensor);

  if(fireSensorValue == LOW) {

    fireSensorText = "Fire Detected";

  }else{

    fireSensorText = "Secure";

  }

 

  vaultIRValue = digitalRead(vaultIR);

  if(vaultIRValue == HIGH) {

    vaultIRText = "Breach";

  }else{

    vaultIRText = "Secure";

  }

 

  if(dayModeCheck == 1){

    employeeIRValue = digitalRead(employeeIR);

    if(employeeIRValue == HIGH) {

      employeeIRText = "Breach";

    }else{

      employeeIRText = "Secure";

    }

 

    counterIRValue = digitalRead(counterIR);

    if(counterIRValue == HIGH) {

      counterIRText = "Breach";

    }else{

      counterIRText = "Secure";

    }

  }else{    

      employeeIRText = "Deactivated";

      counterIRText = "Deactivated";

  }

}

Now, let’s understand the code step by step:

Understanding ESP32 Code

First of all, we need to add the required libraries:

  1. WiFi.h Library is used to create the WebServer in ESP32.
  2. LiquidCrystal_I2C.h Library is used to control the I2C LCD.
  3. Keypad.h Library is used to control the Keypad 4x4.

Keypad Initialization

Next, we need to initialize our variables, so let’s first map the Keypad pins and create its object, as shown in the below figure:

LCD Initialization

Next, we need to create the LCD object, as shown in the below figure:

Change Network Credentials

The below code is creating the WebServer object. Here we need to provide the network credentials:

In the above code, we need to change the SSID and Password.

  • SSID: Change it to your network’s SSID.
  • Password: Change it to your network’s Password.

Variables Initialization

In the below code, I have initialized the variables with default values, we will use them later in the code:

Sensors Initialization

In the below code, I have assigned the sensors to respective ESP32 pins:

The variables with “Text” at the end are used to display the sensors’ states on the WebServer.

Setup() Function

The below code shows the Setup() Function, here we need to add the initial settings for our modules:

Let’s quickly go through the Setup() code:

  •  First of all, we begin the Serial Port at baud rate 115200. It’s used to debug the code in the Serial Monitor.
  • Next, we changed the pinMode of sensor pins to INPUT_PULLUP. Pullup is preferred as it ignores the noise.
  • After that, we initialized the LCD, turned on its backlight and printed the first message “Connecting to {ssid}”.
  • Next, we used the while loop to wait for the ESP32 to get connected with our Network. Once the WiFi get connected, the compiler will get out of the while() loop.
  • Next, we printed the ESP32 IP Address on the LCD screen and begin the WeServer.
  • Finally, we added the EventListener to the keypad, it will detect the buttons pressed on the keypad. We have assigned keypadEvent() Function to this Event Listener. Let’s have a look at this function:

keypadEvent() Function

The keypadEvent() Function will detect the keys pressed on the keypad, we have added this function in the AddEventListener, so its interrupt based.

Here, we have first detected the “#” key, once this key is pressed, the code will wait for the user to enter the 4 Pin Authentication Code. When the “*” key is pressed, the code will compare the PinCode with the Admin PinCode and will display the result on the LCD.

The KeypadEvent() Function is shown in the below figure:

SensorChecks() Function

In the SensorChecks() Function, we are checking the state of each sensor and updating the server strings i.e. if smoke sensor is LOW, we are printing “Smoke Detected” on the WebServer and if its HIGH, we are printing “Secure”. Moreover, if the dayModeCheck variable is 1, it means the system is operating in the “Night Mode”. So, Employee & Counter IR Sensors will only work when dayModeCheck == 1(Night Mode).

Loop() Function

In th Loop() Function, we are first detecting the pressed keypad button and after that calling the SensorChecks() Function.

Next, we are checking the webServer, so if someone entered the ESP32 IP address in the web browser, the WiFiClient will get HIGH and the ESP32 will send a webPage(front-end) to the user, that will be displayed in the browser. Here’s he head section of the webPage:

The body section of the webPage is shown in the below figure:

As you can see in the above code, we have placed several checks i.e. if the dayCheck is LOW, display the Green Button for Day Mode and grey button for the Night Mode. Moreover, we are displaying the Vault Statues i.e. Vault Locked, Vault Unlocked or Authentication in Process. Lastly, we displayed the sensor’s values in a tabular Form.

Now let’s upload the code in the ESP32 Microcontrolelr Board and check the results:

Smart Bank Security System - Results

After uploading the code in the ESP32 board, if everything goes fine, you will get the below results:

Let’s focus on the LCD screen, you will get the below display on the LCD screen:

Meanwhile, the ESP32 is trying to connect to the WiFi network, provided in the programming code. Once it gets connected to the network, the below message will be displayed on the LCD:

Finally, it will display the ESP32 IP Address on the LCD screen:

Now, enter this IP Address in the web browser and the below webPage will open up:

As you can see in the above figure, the IR Sensors of Employee Section & Client Section are Deactivated because we are operating in the Day Mode. So, let’s click on the Night Mode Button and we will get the below results:

As you can see in the above figure, the Night Mode is Activated and now the system is monitoring all the sensors. Currently, the Vault status is displayed “Secure”. Now ,let’s enter the PinCode to open the Vault. So, press the “#” key on the keypad and the Vault Status will be displayed as follow:

The LCD display will also ask to Enter PinCode, as shown below:

The Admin PinCode added in the programming code is “1234”, so let’s enter this PinCode:

If the user entered the corred PinCode, the Vault will get Unlocked and the below status will be updated on the WebServer:

The LCD screen will also display the Authentication Succesful message, as shown in the below figure:

After that, will display the Vault Unlocked message, as shown in the below figure:

If the user entered the wrong PinCode, the Vault will get Locked and the below status will be updated on the WebServer:

LCD screen will also print the Authentification Failed message:

Vault Locked message will also be displayed on the LCD screen:

Now let’s test the sensors, I have placed a burning matchstick infront of the Fire sensor and the below result is updated on the WebServer:

Next, let’s test the Smoke Sensor, so I have placed a lighten up cigarette infront of the Smoke Sensor.(We are just using cigarette for testing. Warning: Smoking is injurious to health.)

Next, we need to test the IR Sensors, so place a hand infront of the Vault IR Sensor and the status of Vault IR Sensor will be changed to “Breach”.

If the IR Sensor of Employee Section will detect any hurdle, its status will be updated as follows:

Lastly, palace something infront of the Counter Section IR Sensor, and its status will be updated as follows:

So, that was all for today. I hope you have enjoyed reading this tutorial and will be able to design it on your own. If you have any quarries, regarding this project, please ask in the comments. Thanks for reading. Have a good day.

Get Instant Online Quote


Like

Comment