#include <ESP32Servo.h> // Include ESP32-specific Servo library
#include <WiFi.h>
#include <LiquidCrystal.h> // For the 16x2 LCD
#include <Keypad.h> // For the keypad
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* wifiPassword = "";
// Define the lock password
String correctPassword = "1234";
String enteredPassword = "";
// Define LCD pins and initialize LCD
LiquidCrystal lcd(16, 17, 18, 19, 21, 22); // RS, E, D4, D5, D6, D7
// Setup LED and Servo
int ledPin = 5; // GPIO 5 for LED
Servo myServo; // Servo motor
// Setup Keypad
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {14, 27, 26, 25}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {33, 32, 13, 12}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Function to unlock the door
void unlockDoor() {
digitalWrite(ledPin, HIGH); // Turn on LED
myServo.write(90); // Unlock door (adjust angle based on setup)
lcd.clear();
lcd.print("Door Unlocked!");
delay(2000); // Keep unlocked for 2 seconds
digitalWrite(ledPin, LOW); // Turn off LED
myServo.write(0); // Lock door back
lcd.clear();
lcd.print("Door Locked");
}
/* Commented out: Function to get password from web server
void getPassword() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "https://cosmic-tiramisu-c4f697.netlify.app/get-password"; // Ensure this matches server URL and port
http.begin(url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println("Received Password: " + payload);
if (payload == correctPassword) {
Serial.println("Correct password from server!");
unlockDoor();
} else {
Serial.println("Incorrect password or no password set.");
}
} else {
Serial.println("Error on HTTP request: " + String(httpCode));
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
}
*/
void setup() {
Serial.begin(115200);
// Initialize LCD
lcd.begin(16, 2); // Set up the LCD's number of columns and rows
lcd.setCursor(0, 0);
lcd.print("Door Locked");
// Initialize LED and Servo
pinMode(ledPin, OUTPUT);
myServo.attach(15); // Attach servo to GPIO 15
myServo.write(0); // Start with door locked
// Setup Wi-Fi
WiFi.begin(ssid, wifiPassword);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Initialize Keypad prompt
lcd.clear();
lcd.print("Enter Password:");
}
void loop() {
// Handle keypad input
char key = keypad.getKey();
if (key) {
Serial.println(key); // Debugging: print the pressed key
if (key == '#') { // Submit password
lcd.clear();
if (enteredPassword == correctPassword) {
unlockDoor(); // Unlock the door
} else {
lcd.print("Wrong Password");
delay(2000); // Display wrong password message for 2 seconds
}
enteredPassword = ""; // Reset password after submission
lcd.clear();
lcd.print("Enter Password:");
} else if (key == '*') { // Clear entered password
enteredPassword = "";
lcd.clear();
lcd.print("Enter Password:");
} else {
enteredPassword += key; // Add the key to the entered password
lcd.setCursor(0, 1);
lcd.print(enteredPassword);
}
}
}
delay(1000); // Delay for next loop iteration
}