WebSchalter01

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

// Create a webserver object that listens for HTTP request on port 80
ESP8266WebServer server(80);    

const int led = D4;

void handleRoot(); 
void handleLED();
void handleNotFound();

void setup(void){
  Serial.begin(115200);
  delay(10);
  Serial.println('\n');

  pinMode(led, OUTPUT);
  digitalWrite(led, 1);

  WiFi.mode(WIFI_AP);           // access point modus
  Serial.println("Starte WLAN-Hotspot \"nodeMCU_28473\"");
  WiFi.softAP("nodeMCU_28473", "12345678");

  delay(1000);
  
  Serial.println('\n');
  Serial.print("IP address:\t");
  Serial.println(WiFi.softAPIP());  

  // Call the 'handleRoot' when a client requests URI "/"
  server.on("/", HTTP_GET, handleRoot);
  // Call the 'handleLED' when a POST request is made to URI "/LED"
  server.on("/LED", HTTP_POST, handleLED);
  // When a client requests an unknown URI, call "handleNotFound"
  server.onNotFound(handleNotFound);        

  server.begin();               // Actually start the server
  Serial.println("HTTP server started");
}

void loop(void){
  server.handleClient();            // Listen for HTTP requests from clients
}

// When URI "/"" is requested, send a web page with a button to toggle the LED
void handleRoot() {                 
  server.send(200, "text/html", "<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>");
}

// If a POST request is made to URI /LED
void handleLED() {                          
  digitalWrite(led,!digitalRead(led));  // Change the state of the LED
  // Add a header to respond with a new location for the browser to go to the home page again
  server.sendHeader("Location","/");    
  // Send it back to the browser with an HTTP status 303 (See Other) to redirect
  server.send(303);
}

void handleNotFound(){
  // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
  server.send(404, "text/plain", "404: Not found"); 
}