#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
const char *ssid = "";
const char *password = "";
String button = "<html><body id='bdy_1' style='height: 100px; width: 100px;'> <div id='bdy_2' style='height: 400px; width: 600px; margin-top: 50px; margin-left: 500px;'> <div style='margin-top: 100px; margin-left: 100px;'> <h2 style='color:white;'>ESP32 Webserver</h2> <p style='color:white;'>Click on the respected button to switch the Appliances:</p><div class='row'><table> <tr> <th><h3 style='color:white;'>Room Light</h3><input id= 'b1' type=button onClick=\"parent.location='/roomLight/on'\" value='On'> <input id= 'b2' type=button onClick=\"parent.location='/roomLight/off'\" value='Off'></th><th><h3 style='color:white;'>Room Bell</h3> <input id= 'b1' type=button onClick=\"parent.location='/roomBell/on'\" value='On'> <input id= 'b2' type=button onClick=\"parent.location='/roomBell/off'\" value='Off'></th></table></div></body></div></div><style>#bdy_1{background-image: url('https://i.postimg.cc/bvw7DS9S/Technological-background.jpg');background-repeat: no-repeat;background-size: 1920px 900px;font-family: 'helvetica neue';font-weight: 200;font-size: 20px;}#bdy_2{font-family: 'helvetica neue';font-weight: 200;font-size: 20px;}#b1{border: none;color: white;padding: 15px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;cursor: pointer;}#b2{border: none;color: white;padding: 15px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;cursor: pointer;}#b1{background-color: #4CAF50;}#b2{background-color: red;}</style></html>";
WebServer server(80);
void handle_root(){
server.send(200 , "text/html" , button);
}
void handle_not_found(){
String message = "file not found";
message += "uri: ";
message += server.uri();
message += "\n method: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\n arguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i< server.args() ; i++){
message +=" "+server.argName(i) +":"+server.arg(i) + "\n";
}
server.send(404 , "text/plain" , message);
}
const int led_1 = 2;
const int buzzer = 15;
void setup() {
pinMode(led_1 , OUTPUT);
pinMode(buzzer , OUTPUT);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
//WiFi.begin(ssid , password);
WiFi.begin("Wokwi-GUEST", "", 6);
Serial.println(" ");
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println(" ");
Serial.print("Connected to: ");
Serial.println(ssid);
Serial.print("IP ADDRESS: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp32")){
Serial.println(" MDNS RESPONDER STARTED");
}
server.on("/" , handle_root);
server.on("/roomlight/on" , [](){
server.send(200 , "text/html" , button);
digitalWrite(led_1 , HIGH);
});
server.on("/roomlight/off" , [](){
server.send(200 , "text/html" , button);
digitalWrite(led_1 , LOW);
});
server.on("/roombell/on" , [](){
server.send(200 , "text/html" , button);
digitalWrite(buzzer , HIGH);
});
server.on("/roombell/off" , [](){
server.send(200 , "text/html" , button);
digitalWrite(buzzer , LOW);
});
server.onNotFound(handle_not_found);
server.begin();
Serial.println("http server started");
digitalWrite(led_1 , LOW);
digitalWrite(buzzer , LOW);
}
void loop() {
server.handleClient();
}