#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <WiFi.h>
#include <ThingSpeak.h>
#define RELAY_PIN 19
#define ROW_NUM 4
#define COLUMN_NUM 4
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {12, 14, 27, 26};
byte pin_column[COLUMN_NUM] = {25, 33, 32, 17};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
const String password_1 = "1234";
const String password_2 = "4444";
const String password_3 = "ABCD";
String input_password;
LiquidCrystal_I2C lcd(0x27, 16, 2);
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
WiFiClient client;
long myChannelNumber = 2779057;
const char *myWriteAPIKey = "K978VB2YZR59PCY1";
int statusCode;
bool doorUnlocked = false;
unsigned long unlockStartTime = 0;
const unsigned long unlockDuration = 20000; // 20 seconds
void connectToWiFi() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected to WiFi.");
}
}
void updateThingSpeak(String doorStatus) {
int doorState = 0; // Default: Door Locked
if (doorStatus == "Door Unlocked") {
doorState = 1; // Represent Door Unlocked as 1
}
ThingSpeak.setField(1, doorState); // Use Field 1 for the graph
ThingSpeak.setField(2, doorStatus); // Optionally, log the textual status in Field 2
statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (statusCode == 200) {
Serial.println("Channel update successful.");
} else {
Serial.println("Problem writing data. HTTP error code: " + String(statusCode));
}
delay(20000);
}
void setup() {
Serial.begin(9600);
input_password.reserve(32);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH);
lcd.init();
lcd.backlight();
WiFi.mode(WIFI_STA);
connectToWiFi();
ThingSpeak.begin(client);
}
void loop() {
connectToWiFi();
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '*') {
input_password = "";
lcd.clear();
} else if (key == '#') {
lcd.clear();
if (input_password == password_1 || input_password == password_2 || input_password == password_3) {
Serial.println("Valid Password => Unlock the door");
lcd.setCursor(0, 0);
lcd.print("CORRECT!");
lcd.setCursor(0, 1);
lcd.print("DOOR UNLOCKED!");
digitalWrite(RELAY_PIN, LOW);
updateThingSpeak("Door Unlocked");
doorUnlocked = true;
unlockStartTime = millis();
} else {
Serial.println("Invalid Password => Try again");
lcd.setCursor(0, 0);
lcd.print("INCORRECT!");
lcd.setCursor(0, 1);
lcd.print("ACCESS DENIED!");
}
input_password = "";
} else {
if (input_password.length() == 0) {
lcd.clear();
}
input_password += key;
lcd.setCursor(input_password.length() - 1, 0);
lcd.print('*');
}
}
// Check if the door needs to be relocked
if (doorUnlocked && millis() - unlockStartTime >= unlockDuration) {
digitalWrite(RELAY_PIN, HIGH);
updateThingSpeak("Door Locked"); // Send "Door Locked" state
doorUnlocked = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("DOOR LOCKED");
}
}