#include <WiFi.h>
#include <WebServer.h>

const char* ssid = "ESP32-Access-Point";
const char* password = "12345678";

WebServer server(80);
const String correctPassword = "1234";

void setup() {
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, 16, 17); // Initialize Serial2 to communicate with Arduino

  WiFi.softAP(ssid, password);
  Serial.println("Access Point Started");
  Serial.println(WiFi.softAPIP());

  server.on("/", HTTP_GET, []() {
    server.send(200, "text/html", "<h1>Enter Password</h1><form action=\"/submit\" method=\"POST\"><input type=\"password\" name=\"password\"><input type=\"submit\" value=\"Submit\"></form>");
  });

  server.on("/submit", HTTP_POST, []() {
    String passwordInput = server.arg("password");
    if (passwordInput == correctPassword) {
      Serial2.println("GRANTED");
      server.send(200, "text/html", "<h1>Access Granted</h1>");
    } else {
      Serial2.println("DENIED");
      server.send(200, "text/html", "<h1>Access Denied</h1>");
    }
  });

  server.begin();
}

void loop() {
  server.handleClient();
}