/*se conecteaza la server local
 * led intern pe GPIO 2
 * generaza pag web simpla
 * 
 * https://microcontrollerslab.com/esp32-web-server-arduino-led/
*/

#define LED1 2

#include <WiFi.h>
const char* ssid= "UPCF4821BC"; 
const char* password = "Gherla1956"; 
WiFiServer server(80);

String html ="<!DOCTYPE html> \
<html> \
<body> \
<center><h1 >ESP32 Station Mode</h1></center> \
<form> \
<center><button name=\"LED1\" button style=\"font-size:400%\"color:green\" value=\"ON\" type=\"submit\">LED1 ON</button> \
<center><button name=\"LED1\" button style=\"font-size:400%\"color:red\" value=\"OFF\" type=\"submit\">LED1 OFF</button> \
</form> \
</body> \
</html>";


void Connect_WiFi()
{

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
}
}

void setup()
{
Serial.begin(115200);

pinMode(LED1, OUTPUT);

digitalWrite(LED1, LOW);

Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print("Trying to connect to Wifi Network");
}
Serial.println("");
Serial.println("Successfully connected to WiFi network");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}


void loop()
{
WiFiClient client=server.available();
if(client)
{
String request = client.readStringUntil('\r');

if(request.indexOf("LED1=ON") != -1)digitalWrite(LED1, HIGH);
if(request.indexOf("LED1=OFF") != -1)digitalWrite(LED1, LOW);
client.print(html);
request="";
}
}
