5 Digit Wireless lock made with Arduino Mega and ESP8266

/*
 ************************************************************************************************************
 Author: Siddhant Sangai
 Name of the Project: 5 digit Wireless lock.
 Hardware Used: Arduino Mega, ESP8266, Logic level shifter(bidirectional), External Power supply for ESP8266
 Software Used: Blynk
 Email: siddhantsangai787@gmail.com
 ************************************************************************************************************
 */

//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#define EspSerial Serial1     // Set ESP8266 Serial object


#include <ESP8266_HardSer.h>
#include <BlynkSimpleShieldEsp8266_HardSer.h>
#include <Servo.h>


ESP8266 wifi(EspSerial);
WidgetLCD lcd(1);
WidgetLED led(2);

int masterKey[5];
int key[5];
int flag = 0;
int  statusOfBlynkInput;


Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0;    // variable to store the servo position

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";




void setup()
{


  myservo.attach(48); //attach the servo at pin no 48 of the Arduinio mega.
  pinMode(44, OUTPUT); //Enable for the Servo.
  digitalWrite(44, LOW);
  pinMode(42, OUTPUT); // On Blynk Dashboard for Relocking the Door
  pinMode(21, INPUT); // Connected to pin 42
  for (int m = 0; m < 10; m++)
  {
    pinMode(m + 24, INPUT);
    pinMode(m + 2, OUTPUT);
  }
  // Set console baud rate
  Serial.begin(9600);
  delay(10);
  // Set ESP8266 baud rate
  EspSerial.begin(115200);
  delay(10);

  Blynk.begin(auth, wifi, "SSID", "Password");

  while (Blynk.connect() == false)
  {
    // Wait until connected
  }

  lcd.clear();
  lcd.print(0, 0, "Enter The Master");
  lcd.print(0 , 1, "      Key      ");
  delay(1000);
  lcd.clear();


  for (int count = 0; count < 5; count++)// Loop for a 5 digit Key
  {
    Blynk.run();
    masterKey[count] = keypadBlynk(count);

  }
  lcd.clear();
}




void loop()
{

  Blynk.run();
  if (flag == 0)
  {
    digitalWrite(44, HIGH);
    lcd.print(0, 0, "Enter the Key");
    for (int count = 0; count < 5; count++)
    {
      Blynk.run();
      key[count] = keypadBlynk(count);
    }

    if (key[0] == masterKey[0] && key[1] == masterKey[1] && key[2] == masterKey[2] && key[3] == masterKey[3] && key[4] == masterKey[4])
    {
      flag = 1;
      for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
      {
        myservo.write(pos); // tell servo to go to position in variable 'pos'
        delay(15);
      }
      lcd.clear();
      lcd.print(0, 0, "Lock Open");
      led.on();
      digitalWrite(44, LOW);
    }
    else
    {
      flag = 0;
      lcd.clear();
      lcd.print(0, 0, "Incorrect Key");
      lcd.print(0, 1, "Please try again");
      delay(1000);
      lcd.clear();
    }

  }
  if (digitalRead(21) == HIGH) // condition to relock the system.
    lock();
}



/*********************************************************************************************
                              Blynk Keypad Initialization 
 *********************************************************************************************/

 
int keypadBlynk(int count)
{

  Blynk.run();
  int keyBlynk = 0;
  int statusOfPin = LOW;
  while (statusOfPin != HIGH)
  {
    //lcd.print(0, 0, "Enter the Key");
    Blynk.run();
    for (int num = 24; num < 34; num++)
    {
      statusOfPin = digitalRead(num);

      if (statusOfPin == HIGH)
      {
        keyBlynk = num - 24;
        num = 34;
      }

    }

  }

  lcd.print(count, 1, keyBlynk);
  return keyBlynk;
}



/***********************************************************************************************
                        Initialization of the Locking module 
 ***********************************************************************************************/

 
void lock()
{
  digitalWrite(44, HIGH);
  Blynk.run();
  flag = 0;
  led.off();

  for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
  {
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);

  }
  digitalWrite(44, LOW);
}


6 Likes

Great project! :clap: Thanks for sharing!

As an idea: maybe it’s good not to show the code and put ***** instead?

Pretty cool project, what is the lock’s application? I can think of several great uses for this, just curious how you applied this…

brian

Thanks Brian! Sorry for my late reply.
I was thinking of trying this out at my own place, but you see there are a few practical problems.
The ESP8266 has a history, if by any chance it disconnects form the internet it does not connect itself also it is not that stable and that would cause some trouble.
I have not worked on this idea lately, once i find the solution to these problems i will share some real life applications.

Siddhant

Sure Sir. Will work on it soon.