#include <WiFi.h>
#include <WebServer.h>
#include <Keypad.h>
#include "driver/ledc.h"
// WiFi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Buzzer and motion sensor
const int buzzerPin = 34; // Buzzer connected to GPIO34
const int buzzerChannel = LEDC_CHANNEL_0;
const int buzzerFrequency = 2000; // Frequency in Hz
const int buzzerResolution = LEDC_TIMER_8_BIT;
const ledc_timer_t buzzerTimer = LEDC_TIMER_0;
const int motionSensorPin = 35; // PIR motion sensor connected to GPIO35
bool lockerOpenedWithoutPasskey = false;
// Keypad setup
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] = {21, 19, 18, 5}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {17, 16, 4, 0}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Web server
WebServer server(80);
// Passkey for locker
String correctPasskey = "1234";
String enteredPasskey = "";
// Function to initialize the buzzer
void initBuzzer() {
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_HIGH_SPEED_MODE,
.duty_resolution = buzzerResolution,
.timer_num = buzzerTimer,
.freq_hz = buzzerFrequency,
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&ledc_timer);
ledc_channel_config_t ledc_channel = {
.gpio_num = buzzerPin,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.channel = buzzerChannel,
.timer_sel = buzzerTimer,
.duty = 0,
.hpoint = 0
};
ledc_channel_config(&ledc_channel);
}
// Function to sound the buzzer (alert sound)
void soundBuzzer() {
ledc_set_duty(LEDC_HIGH_SPEED_MODE, buzzerChannel, 127); // 50% duty cycle
ledc_update_duty(LEDC_HIGH_SPEED_MODE, buzzerChannel);
delay(1000); // Sound for 1 second
ledc_set_duty(LEDC_HIGH_SPEED_MODE, buzzerChannel, 0);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, buzzerChannel);
}
// Function to check if the motion sensor detects movement (i.e., door opened)
void checkMotionSensor() {
int motionDetected = digitalRead(motionSensorPin);
if (motionDetected == HIGH && !lockerOpenedWithoutPasskey) {
// If motion is detected without passkey entered, trigger alert
lockerOpenedWithoutPasskey = true;
soundBuzzer();
server.send(200, "text/plain", "ALERT! Locker opened without passkey.");
}
}
// Handle HTTP requests for status
void handleStatus() {
if (lockerOpenedWithoutPasskey) {
server.send(200, "text/plain", "Locker opened without passkey! ALERT!");
} else {
server.send(200, "text/plain", "Locker is secure.");
}
}
// Handle passkey entry via the web server
void handleEnterPasskey() {
if (server.hasArg("passkey")) {
enteredPasskey = server.arg("passkey");
if (enteredPasskey == correctPasskey) {
lockerOpenedWithoutPasskey = false; // Reset the alert flag
server.send(200, "text/plain", "Access granted. Locker opened.");
} else {
server.send(403, "text/plain", "Incorrect passkey. Access denied.");
}
} else {
server.send(400, "text/plain", "Passkey not provided.");
}
}
void setup() {
// Initialize serial monitor
Serial.begin(115200);
// Initialize WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize the web server
server.on("/status", handleStatus);
server.on("/enter-passkey", handleEnterPasskey);
server.begin();
Serial.println("Server started");
// Initialize buzzer and motion sensor
initBuzzer();
pinMode(motionSensorPin, INPUT);
}
void loop() {
// Check if the motion sensor detects the door opening
checkMotionSensor();
// Handle incoming HTTP requests
server.handleClient();
// Check keypad input
char key = keypad.getKey();
if (key) {
if (key == '#') {
// User pressed '#', check passkey
if (enteredPasskey == correctPasskey) {
lockerOpenedWithoutPasskey = false; // Access granted
Serial.println("Access granted. Locker opened.");
} else {
soundBuzzer(); // Incorrect passkey, trigger alert
Serial.println("Incorrect passkey. Access denied.");
}
enteredPasskey = ""; // Reset entered passkey
} else {
// Append key to entered passkey
enteredPasskey += key;
}
}
}