#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 handleRoute(){
server.send(200,"text/html",button);
}
void handleNotFound(){
String message = "file not found\n";
message+="uri: ";
message+=server.uri();
message+="\nmethod: ";
message+=(server.method()==HTTP_GET)?"get":"post";
message+="\narguments: ";
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);
}
int led_1 = 2;
int buzzer = 15;
void setup() {
// put your setup code here, to run once:
pinMode(led_1, OUTPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED){
Serial.println("connecting...");
delay(1000);
}
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("/",handleRoute);
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(handleNotFound);
server.begin();
Serial.println("HTTPS server started");
digitalWrite(buzzer, LOW);
digitalWrite(led_1, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
}