#include <WiFi.h>
#include <WebServer.h>
#define ssid "3SPEZ"
#define password "12345678"
#define blueLEDpin 21
//IPAddress local_ip(192,168,1,1);
//IPAddress gateway(192,168,1,1);
//IPAddress subnet(255,255,255,0);
WebServer server(80);
bool LEDStatus = LOW;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(blueLEDpin, OUTPUT);
// WiFi.softAP(ssid, password);
// WiFi.softAPConfig(local_ip,gateway,gateway);
// delay(1000);
//added
Serial.println();
Serial.println("Configuring access point...");
// You can remove the password parameter if you want the AP to be open.
// a valid password must have more than 7 characters
if (!WiFi.softAP(ssid, password)) {
log_e("Soft AP creation failed.");
while(1);
}
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
// server.begin();
// Serial.println("Server started");
//added
server.on("/", handle_OnConnect);
server.on("/ledon", handle_ledON);
server.on("/ledoff", handle_ledOFF);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP Server Started");
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
if(LEDStatus){
digitalWrite(blueLEDpin, HIGH);
}
else{
digitalWrite(blueLEDpin, LOW);
}
}
String getHTML(){
String htmlcode = "<!DOCTYPE html> <html>\n";
htmlcode +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
htmlcode +="<title>LED Control</title>\n";
htmlcode +="<style>html { font-family: Open Sans; display: inline-block; margin: 0px auto; text-align: center;}\n";
htmlcode +="body{margin-top: 100px;} h1 {color: #154c79; margin: 30px auto 30px;} h3 {color: #1e81b0;margin-bottom: 50px;}\n";
// htmlcode +=".botton {display: block;width: 150px;background-color: #3498db;border: none;color: white;padding:13px 30px;text-decoration: none}\n";
htmlcode +=".button {background-color: #3498db;border: none;color: white;padding: 15px 32px;text-align: center;text-decoration: none;display: block;font-size: 16px;margin: auto;cursor: pointer;width: 120px;border-radius: 8px}\n";
htmlcode +=".button-on {background-color: #1e81b0;}\n";
htmlcode +=".button-on:active {background-color:#15767b;}\n";
// htmlcode +=".button-off {background-color: #5e0f1a;}\n";
// htmlcode +=".button-off:active {background-color: #4b0c15;}\n";
htmlcode +=".button-off {background-color: #15767b;}\n";
htmlcode +=".button-off:active {background-color: #1e81b0;}\n";
htmlcode +="p {font-size: 18px;color: #888;margin-bottom: 10px;}\n";
htmlcode +="</style>\n";
htmlcode +="</head>\n";
htmlcode +="</body>\n";
htmlcode +="<h1>ESP32 Web Server</h1>\n";
htmlcode +="<h3>A simple demo using Access Point (AP) Mode</h3>\n";
if (LEDStatus){
// htmlcode +="<p>Blue LED Status:ON</p><a href=\"/ledoff\">Turn it OFF</a>\n";
htmlcode +="<p>Blue LED Status:ON</p><a class=\"button button-off\" href=\"/ledoff\">Turn it OFF</a>\n";
}
else{
// htmlcode +="<p>Blue LED Status:OFF</p><a href=\"/ledon\">Turn it ON</a>\n";
htmlcode +="<p>Blue LED Status:OFF</p><a class=\"button button-on\" href=\"/ledon\">Turn it ON</a>\n";
}
htmlcode +="</body>\n";
htmlcode +="</html>\n";
return htmlcode;
}
void handle_OnConnect(){
LEDStatus = LOW;
Serial.println("LED Status: OFF");
server.send(200,"text/html", getHTML());
}
void handle_ledON(){
LEDStatus = HIGH;
Serial.println("LED Status: ON");
server.send(200,"text/html", getHTML());
}
void handle_ledOFF(){
LEDStatus = LOW;
Serial.println("LED Status: OFF");
server.send(200,"text/html", getHTML());
}
void handle_NotFound(){
server.send(404,"text/plain", "Not Found");
}