#include <WiFi.h>

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

WiFiServer server(80);
String header;
String output26State = "off";
String output27State = "off";
const int output26 = 26;
const int output27 = 27;

unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000;

void setup() {
Serial.begin(115200);
pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT);
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}

void loop() {
WiFiClient client = server.available();
if (client) {
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client.");
String currentLine = "";
while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();
if (client.available()) {
char c = client.read();
Serial.write(c);
header += c;
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();

if (header.indexOf("GET /26/on") >= 0) {
Serial.println("GPIO 26 on");
output26State = "on";
digitalWrite(output26, HIGH);}
else if (header.indexOf("GET /26/off") >= 0) {
Serial.println("GPIO 26 off");
output26State = "off";
digitalWrite(output26, LOW);}
else if (header.indexOf("GET /27/on") >= 0) {
Serial.println("GPIO 27 on");
output27State = "on";
digitalWrite(output27, HIGH);}
else if (header.indexOf("GET /27/off") >= 0) {
Serial.println("GPIO 27 off");
output27State = "off";
digitalWrite(output27, LOW);}

client.println("<!DOCTYPE html>");
client.println("<html><head><style>");
client.println(".button1 {background-color:lime}");
client.println(".button2 {background-color:red}");
client.println("</style></head><body>");
client.println("<h1>ESP32 Web Server</h1>");
client.println("<p>GPIO 26</p>");
if (output26State=="off") {client.println("<p><a href=\"/26/on\"><button class=\"button1\">ON</button></a></p>");}
else {client.println("<p><a href=\"/26/off\"><button class=\"button2\">OFF</button></a></p>");}
client.println("<p>GPIO 27</p>");
if (output27State=="off") {client.println("<p><a href=\"/27/on\"><button class=\"button1\">ON</button></a></p>");}
else {client.println("<p><a href=\"/27/off\"><button class=\"button2\">OFF</button></a></p>");}
client.println("</body></html>");

client.println();
break;}
else {currentLine = "";}}
else if (c != '\r') {currentLine += c;}}}

header = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");}}