/**************************************************
Übung: ESP32 (Teil 2) - Aufgabe 3 (Vorlage)
***************************************************/
//-----------------------------------------------------------
#include <WiFi.h>
#include <WebServer.h>
//-----------------------------------------------------------
// BBWAS1:
const int PIN_LED_AMPEL_RECHTS_ROT = 18;
// Statusvariablen
bool status_led_rot = false;
//-----------------------------------------------------------
// WiFi:
const char WIFI_SSID[] = "m126";
const char WIFI_PASSWORD[] = "M126M126";
// Webserver Objekt (Port 80)
WebServer webserver(80);
//-----------------------------------------------------------
void setup(void) {
//---
// UART starten (115200 Baud)
Serial.begin(115200);
//---
// BBWAS1
pinMode(PIN_LED_AMPEL_RECHTS_ROT, OUTPUT);
//---
// WiFi verbinden
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Verbinde mit ");
Serial.print(WIFI_SSID);
// Warten bis mit WiFi verbunden...
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println(" Verbunden!");
// Zugewiesene IP-Adresse ausgeben
Serial.print("IP-Adresse: ");
Serial.println(WiFi.localIP());
//---
// Webserver URL konfigurieren
webserver.on("/", handle_page_index);
// Webserver starten
webserver.begin();
Serial.println("Webserver gestartet!");
}
//-----------------------------------------------------------
void loop(void) {
// Webserver bedienen
webserver.handleClient();
delay(10);
}
//-----------------------------------------------------------
void handle_page_index() {
//---
// Query-Parameter "rot" behandeln
if (webserver.hasArg("rot") == true) {
String p = webserver.arg("rot");
//
Serial.print("rot=");
Serial.println(p);
//
status_led_rot = p.toInt();
digitalWrite(PIN_LED_AMPEL_RECHTS_ROT, status_led_rot);
}
//---
// HTML-Template
String html_template = R"(
<!DOCTYPE html>
<html>
<head>
<title>ESP32 Webserver</title>
</head>
<body>
<h1>ESP32 Webserver</h1>
<div>
<h2>AMPEL RECHTS ROT</h2>
<p>Aktueller Status: *AMPEL_RECHTS_ROT_TEXT*</p>
<form method="get" action="/">
<button type="submit" name="rot" value="0"> OFF </button>
<button type="submit" name="rot" value="1"> ON </button>
</form>
</div>
</body>
</html>
)";
//---
// HTML-Template updaten
if (status_led_rot == true) {
html_template.replace("*AMPEL_RECHTS_ROT_TEXT*", "ON");
} else {
html_template.replace("*AMPEL_RECHTS_ROT_TEXT*", "OFF");
}
//---
// HTML an Client senden
webserver.send(200, "text/html", html_template);
}