0% found this document useful (0 votes)
10 views3 pages

Notes 250730 203032

Uploaded by

Aung Thu Ya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Notes 250730 203032

Uploaded by

Aung Thu Ya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <WiFi.

h>
#include <WebServer.h>

const char* ssid = "YOUR_WIFI_SSID"; //


Replace with your Wi-Fi SSID
const char* password =
"YOUR_WIFI_PASSWORD"; // Replace with
your Wi-Fi password

WebServer server(80); // Create a web server


on port 80

void handleRoot() {
server.send(200, "text/plain", "ESP32 Web
Server");
}

void handleToggle() {
String message = "";
if (server.hasArg("state")) { // Check if "state"
argument exists in the URL
String state = server.arg("state");
if (state == "on") {
// Perform actions when toggle is ON (e.g.,
turn on an LED)
message = "Toggle is ON";
Serial.println("Toggle ON");
} else if (state == "off") {
// Perform actions when toggle is OFF
(e.g., turn off an LED)
message = "Toggle is OFF";
Serial.println("Toggle OFF");
}
}
server.send(200, "text/plain", message);
}

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.localIP());

server.on("/", handleRoot);
server.on("/toggle", handleToggle); // Handle
requests to /toggle
server.begin();
Serial.println("HTTP server started");
}

void loop() {
server.handleClient(); // Handle incoming
client requests
}

You might also like