// Load Wi-Fi library
#include <WiFi.h>
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = ""; // No password required
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliary variables to store the current output state
String output12State = "off";
String output14State = "off";
String output27State = "off";
String output26State = "off";
String buzzerState = "off";
// Assign output variables to GPIO pins
const int output12 = 12; // LED 1
const int output14 = 14; // LED 2
const int output27 = 27; // LED 3
const int output26 = 26; // LED 4
const int buzzerPin = 13; // Buzzer
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output12, OUTPUT);
pinMode(output14, OUTPUT);
pinMode(output27, OUTPUT);
pinMode(output26, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Set outputs to LOW
digitalWrite(output12, LOW);
digitalWrite(output14, LOW);
digitalWrite(output27, LOW);
digitalWrite(output26, LOW);
digitalWrite(buzzerPin, LOW);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // Print a message out in the serial port
String currentLine = ""; // Make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { // Loop while the client's connected
currentTime = millis();
if (client.available()) { // If there's bytes to read from the client,
char c = client.read(); // Read a byte, then
Serial.write(c); // Print it out the serial monitor
header += c;
if (c == '\n') { // If the byte is a newline character
if (currentLine.length() == 0) {
// HTTP headers
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// Turn GPIOs on and off based on URL requests
if (header.indexOf("GET /12/on") >= 0) {
Serial.println("GPIO 12 on");
output12State = "on";
digitalWrite(output12, HIGH);
} else if (header.indexOf("GET /12/off") >= 0) {
Serial.println("GPIO 12 off");
output12State = "off";
digitalWrite(output12, LOW);
} else if (header.indexOf("GET /14/on") >= 0) {
Serial.println("GPIO 14 on");
output14State = "on";
digitalWrite(output14, HIGH);
} else if (header.indexOf("GET /14/off") >= 0) {
Serial.println("GPIO 14 off");
output14State = "off";
digitalWrite(output14, 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);
} else 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 /buzzer/on") >= 0) {
Serial.println("Buzzer on");
buzzerState = "on";
digitalWrite(buzzerPin, HIGH);
} else if (header.indexOf("GET /buzzer/off") >= 0) {
Serial.println("Buzzer off");
buzzerState = "off";
digitalWrite(buzzerPin, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;} .button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;} .button2 {background-color: #555555;}</style></head>");
client.println("<body><h1>ESP32 Web Server</h1>");
// Display buttons for each GPIO
client.println("<p>GPIO 12 - State " + output12State + "</p>");
client.println(output12State == "off" ? "<p><a href=\"/12/on\"><button class=\"button\">ON</button></a></p>" : "<p><a href=\"/12/off\"><button class=\"button button2\">OFF</button></a></p>");
client.println("<p>GPIO 14 - State " + output14State + "</p>");
client.println(output14State == "off" ? "<p><a href=\"/14/on\"><button class=\"button\">ON</button></a></p>" : "<p><a href=\"/14/off\"><button class=\"button button2\">OFF</button></a></p>");
client.println("<p>GPIO 27 - State " + output27State + "</p>");
client.println(output27State == "off" ? "<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>" : "<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");
client.println("<p>GPIO 26 - State " + output26State + "</p>");
client.println(output26State == "off" ? "<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>" : "<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");
client.println("<p>Buzzer - State " + buzzerState + "</p>");
client.println(buzzerState == "off" ? "<p><a href=\"/buzzer/on\"><button class=\"button\">ON</button></a></p>" : "<p><a href=\"/buzzer/off\"><button class=\"button button2\">OFF</button></a></p>");
client.println("</body></html>");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
header = "";
client.stop();