diff --git a/BUTTON-WEB-RELAY/BUTTON-WEB-RELAY.ino b/BUTTON-WEB-RELAY/BUTTON-WEB-RELAY.ino new file mode 100644 index 0000000..958b166 --- /dev/null +++ b/BUTTON-WEB-RELAY/BUTTON-WEB-RELAY.ino @@ -0,0 +1,74 @@ +#include +#include +#include + +// --- Zugangsdaten --- +const char* ssid = "Wayne Manor"; +const char* password = "einfach10zeichen!"; +const char* hostname = "licht"; + +// --- Hardware --- +const int relayPin = 2; // Pin, an dem das Relais hängt +bool relayState = false; + +ESP8266WebServer server(80); + +// HTML Webseite +String getHTML() { + String html = ""; + html += "Licht Steuerung"; + html += ""; + html += "

Licht Steuerung

"; + + if (relayState) { + html += ""; + } else { + html += ""; + } + + html += ""; + return html; +} + +void handleRoot() { + server.send(200, "text/html", getHTML()); +} + +void handleToggle() { + relayState = !relayState; + digitalWrite(relayPin, relayState ? LOW : HIGH); // Relais sind oft Low-Level-Triggered + server.sendHeader("Location", "/"); + server.send(303); +} + +void setup() { + Serial.begin(9600); + pinMode(relayPin, OUTPUT); + digitalWrite(relayPin, HIGH); // Standardmäßig aus (bei High-Trigger umkehren) + + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + // mDNS (Hostname) starten + if (MDNS.begin(hostname)) { + Serial.println("mDNS Responder gestartet"); + } + + server.on("/", handleRoot); + server.on("/toggle", handleToggle); + + server.begin(); + Serial.print("Verbunden! IP-Adresse: "); + Serial.println(WiFi.localIP()); + Serial.printf("Hostname: http://%s.local\n", hostname); +} + +void loop() { + MDNS.update(); + server.handleClient(); +} \ No newline at end of file