Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <ESPAsyncWebServer.h>
- #include <HTTPClient.h>
- const char* ssid = "Your_SSID"; // Replace with your WiFi SSID
- const char* password = "Your_PASSWORD"; // Replace with your WiFi Password
- const char* otp = "123456"; // Temporary hardcoded OTP, replace with generated OTP
- AsyncWebServer server(80); // Start a web server on port 80
- const int relayPin = 5; // Relay connected to GPIO 5
- bool doorUnlocked = false;
- void setup() {
- Serial.begin(115200);
- pinMode(relayPin, OUTPUT);
- digitalWrite(relayPin, LOW); // Ensure door is locked by default
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- // Web server to enter OTP
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- String html = "<html><body><h2>Enter OTP to Unlock Door</h2><form action='/verify' method='POST'><input type='text' name='otp' placeholder='Enter OTP'><input type='submit' value='Submit'></form></body></html>";
- request->send(200, "text/html", html);
- });
- // OTP verification and door control
- server.on("/verify", HTTP_POST, [](AsyncWebServerRequest *request){
- if (request->hasParam("otp", true)) {
- String inputOTP = request->getParam("otp", true)->value();
- if (inputOTP == otp) {
- digitalWrite(relayPin, HIGH); // Unlock door
- delay(5000); // Keep unlocked for 5 seconds
- digitalWrite(relayPin, LOW); // Lock door again
- request->send(200, "text/html", "Door Unlocked! Welcome.");
- doorUnlocked = true;
- } else {
- request->send(200, "text/html", "Invalid OTP. Try again.");
- }
- }
- });
- server.begin();
- }
- void loop() {
- // The main loop remains empty as the web server handles requests
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement