#include <Keypad.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* token = "u9kXU";
int Button=4;
int Button1=18;
int Lock = 23;
const byte numRows= 4;
const byte numCols= 4;
int buzzerPin = 15;
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
char keypressed;
char code[]= {'1','2','3','4'};
char check1[sizeof(code)];
char check2[sizeof(code)];
short a=0,i=0,s=0,j=0;
byte rowPins[numRows] = {13,12,14,27};
byte colPins[numCols]= {26,25,33,32};
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows,
numCols);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
lcd.init();
lcd.backlight();
lcd.print("CircuitSchools");
lcd.setCursor(0,1);
lcd.print("Password Lock");
delay(2000);
lcd.clear();
lcd.print("* to unlock");
lcd.setCursor(0,1);
lcd.print("B to lock");
delay(2000);
lcd.clear();
lcd.print("Press* to unlock");
pinMode(Button,INPUT);
pinMode(Button1,INPUT);
pinMode(Lock, OUTPUT);
for(i=0 ; i<sizeof(code);i++){
EEPROM.get(i, code[i]);
}
Serial.begin(9600);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
}
void loop()
{
keypressed = myKeypad.getKey();
if (keypressed == '*'){
playTone(1000, 100);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter code");
ReadCode();
if (a==sizeof(code))
OpenDoor();
else{
playTone(300, 100);
lcd.clear();
lcd.print("Wrong code");
lcd.setCursor(0,1);
lcd.print("Press * to Unlock");
}
delay(2000);
lcd.clear();
lcd.print("Standby");
}
if(keypressed == '#'){
playTone(1000, 100);
ChangeCode();
lcd.clear();
lcd.print("Standby");
lcd.setCursor(0,1);
lcd.print("Press * to Unlock");
}
if(digitalRead(Button)==HIGH){
digitalWrite(Lock, HIGH);
OpenDoor();
}
if(digitalRead(Button1)==HIGH){
lcd.clear();
digitalWrite(Lock, LOW);
lcd.print("Locked: Press * to ");
lcd.setCursor(0,1);
lcd.print("to unlock!!");
}
if(keypressed == 'B'){
lcd.clear();
digitalWrite(Lock, LOW);
lcd.print("Locked: Press * to ");
lcd.setCursor(0,1);
lcd.print("to unlock!!");
}
}
void ReadCode(){
i=0;
a=0;
j=0;
while(keypressed != 'A'){
keypressed = myKeypad.getKey();
playTone(1000, 100);
if(keypressed != NO_KEY && keypressed != 'A' ){
lcd.setCursor(j,1);
lcd.print("*");
playTone(1000, 100);
j++;
if(keypressed == code[i]&& i<sizeof(code)){
a++;
i++;
}
else
a--;
}
}
keypressed = NO_KEY;
}
void ChangeCode(){
lcd.clear();
lcd.print("Changing code");
delay(1000);
lcd.clear();
lcd.print("Enter old code");
ReadCode();
if(a==sizeof(code)){
lcd.clear();
lcd.print("Changing code");
GetNewCode1();
GetNewCode2();
s=0;
for(i=0 ; i<sizeof(code) ; i++){
if(check1[i]==check2[i])
s++;
}
if(s==sizeof(code)){
for(i=0 ; i<sizeof(code) ; i++){
code[i]=check2[i];
EEPROM.put(i, code[i]);
}
lcd.clear();
lcd.print("Code Changed");
playTone(1000, 1000);
delay(2000);
}
else{
lcd.clear();
playTone(300, 100);
lcd.print("Codes are not");
lcd.setCursor(0,1);
lcd.print("matching !!");
delay(2000);
}
}
else{
lcd.clear();
lcd.print("Wrong");
delay(2000);
}
}
void GetNewCode1(){
i=0;
j=0;
lcd.clear();
lcd.print("Enter new code");
lcd.setCursor(0,1);
lcd.print("and press A");
delay(2000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print("and press A");
while(keypressed != 'A'){
keypressed = myKeypad.getKey();
if(keypressed != NO_KEY && keypressed != 'A' ){
playTone(1000, 100);
lcd.setCursor(j,0);
lcd.print("*");
check1[i]=keypressed;
j++;
}
}
keypressed = NO_KEY;
}
void GetNewCode2(){
i=0;
j=0;
lcd.clear();
lcd.print("Confirm code");
lcd.setCursor(0,1);
lcd.print("and press A");
delay(3000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print("and press A");
while(keypressed != 'A'){
keypressed = myKeypad.getKey();
playTone(1000, 100);
if(keypressed != NO_KEY && keypressed != 'A' ){
lcd.setCursor(j,0);
lcd.print("*");
check2[i]=keypressed;
i++;
j++;
}
}
keypressed = NO_KEY;
}
void OpenDoor(){
lcd.clear();
lcd.print("Unlocked:Welcome");
playTone(1000, 1000);
digitalWrite(Lock,HIGH);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
Serial.println("WiFi connect");
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Authorization", "Bearer " + String(token));
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.POST("");
Serial.println("");
http.end();
}
}
void playTone(int frequency, int duration) {
tone(buzzerPin, frequency, duration);
delay(duration);
noTone(buzzerPin);
}