Skip to content

Instantly share code, notes, and snippets.

@Ajak58a
Created May 23, 2024 16:33
Show Gist options
  • Save Ajak58a/40a847b599430304b499a03d3441680b to your computer and use it in GitHub Desktop.
Save Ajak58a/40a847b599430304b499a03d3441680b to your computer and use it in GitHub Desktop.
OTP Door Lock
#include <Keypad.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {22, 24, 26, 28}; //connect to the Rows of the keypad pin 8, 7, 6, 5 respectively
byte colPins[COLS] = {30, 32, 34, 36}; //connect to the Columns of the keypad pin 4, 3, 2, 1 respectively
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
int irsensor = 8;
#define relay 50
int otp;
String otpstring = "";
int i = 0;
void sendATCommand(String command) {
Serial1.println(command);
delay(1000);
while (Serial1.available()) {
char c = Serial1.read();
Serial.print(c);
}
Serial.println();
}
void printText(int x, int y, String txt, float size){
display.setTextSize(size);
display.setTextColor(SSD1306_WHITE);
display.setCursor(x, y);
display.println(txt);
}
void SendSMS(){
Serial.println("Sending SMS...");
sendATCommand("AT+CMGF=1");
delay(100);
sendATCommand("AT+CSMP=17,167,0,0");
delay(500);
sendATCommand("AT+CMGS=\"XXXXXXXXXX\""); //REPLACE WITH YOUR OWN MOBILE NUMBER AT WHICH YOU WANT TO RECEIVE OTP
delay(500);
Serial1.print("Your OTP is " + otpstring + " Just Type OTP And Unlock The Door");
delay(500);
Serial1.print((char)26);
delay(500);
Serial1.println();
Serial.println("Text Message Sent.");
delay(500);
}
void getOTP(){
String pass = "";
int a = pass.length();
while (a < 4) {
char customKey = customKeypad.getKey();
if (customKey) {
pass = pass + customKey;
printText(10, 30, pass, 1.5);
display.display();
a = pass.length();
}
}
Serial.print("Entered OTP is ");
Serial.println(pass);
if (otpstring == pass) {
display.clearDisplay();
printText(10, 0, "Access Granted", 1.5);
display.display();
delay(2000);
digitalWrite(relay, HIGH);
delay(10000);
digitalWrite(relay, LOW);
} else {
display.clearDisplay();
printText(10, 0, "Access Denied", 1.5);
printText(10, 20, "Try Again", 1.5);
display.display();
delay(3000);
}
}
void setup(){
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
pinMode(irsensor, INPUT_PULLUP);
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
delay(500);
Serial1.begin(9600); //SIM900
Serial.print("Initializing Modem");
sendATCommand("AT");
sendATCommand("AT+CSQ");
delay(1000);
}
void loop(){
display.clearDisplay();
printText(10, 0, "OTP Based", 2);
printText(10, 30, "Door Lock", 2);
display.display();
delay(2000);
if (digitalRead(irsensor) == LOW) {
otp = random(1000, 9999);
otpstring = String(otp);
Serial.print("Current OTP: ");
Serial.println(otpstring);
while (digitalRead(irsensor) == LOW) {}
display.clearDisplay();
printText(10, 0, "OTP is sent", 1.5);
printText(10, 20, "to your mobile", 1.5);
display.display();
delay(2000);
SendSMS();
display.clearDisplay();
printText(10, 0, "Enter OTP", 1.5);
display.display();
delay(2000);
getOTP();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment