#include <WiFi.h>

const char WiFiPassword[] = "123456789";
const char AP_NameChar[] = "My-ESP32-AP";

WiFiServer server(80);

String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";

String html_1 = "<!DOCTYPE html><html><head><title>Relay Control</title></head><body><h2>Relay Control</h2>";
String html_2 = "<form action='/LEDON'><input type='submit' value='ON'></form><br>";
String html_3 = "<form action='/LEDOFF'><input type='submit' value='OFF'></form><br>";
String html_4 = "</body></html>";

int relayPin = 21;

void setup() {
  Serial.begin(115200);

  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);

  WiFi.softAP(AP_NameChar, WiFiPassword);

  Serial.println(WiFi.softAPIP());

  server.begin();
}

void loop() {
  WiFiClient client = server.available();
  if (!client) return;

  String request = client.readStringUntil('\r');
  Serial.println(request);

  if (request.indexOf("/LEDON") != -1) {
    digitalWrite(relayPin, HIGH);
  }
  else if (request.indexOf("/LEDOFF") != -1) {
    digitalWrite(relayPin, LOW);
  }

  client.flush();

  client.print(header);
  client.print(html_1);
  client.print(html_2);
  client.print(html_3);
  client.print(html_4);

  delay(5);
}