| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- #include "Arduino.h"
- #include <ESP8266WebServer.h>
- #include <WebSocketsServer.h>
- #include <EEPROM.h>
- #include <DNSServer.h>
- #include "States.h"
-
- AppState state = STATE_BOOT;
- unsigned long connectStart = 0;
-
- const int ledPin = LED_BUILTIN; // The built-in LED on the Wemos D1 Mini is on GPIO2 (D4)
- const byte DNS_PORT = 53;
-
- // AP Daten
- const char *ap_ssid = "Wemos_Setup";
- const char *ap_pass = "admin123";
-
- // WLAN Config
- struct Config {
- char ssid[32];
- char pass[64];
- bool valid;
- };
-
- ESP8266WebServer webServer(80);
- WebSocketsServer webSocket(81);
- DNSServer dnsServer;
- Config config;
-
- // ---------- EEPROM ----------
- void saveConfig() {
- EEPROM.begin(sizeof(Config));
- EEPROM.put(0, config);
- EEPROM.commit();
- }
-
- void loadConfig() {
- EEPROM.begin(sizeof(Config));
- EEPROM.get(0, config);
-
- if (config.valid != true) {
- config.valid = false;
- }
- }
-
- // ---------- CAPTIVE PORTAL PAGE ----------
- void handleRoot() {
- String mac = WiFi.macAddress();
-
- String html =
- R"rawliteral(
- <!DOCTYPE html>
- <html>
-
- <script>
- function togglePassword() {
- const pass = document.getElementById("pass");
- const eye = document.querySelector(".toggle-eye");
-
- if (pass.type === "password") {
- pass.type = "text";
- eye.textContent = "❌";
- } else {
- pass.type = "password";
- eye.textContent = "👁";
- }
- }
- </script>
-
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Wemos Setup</title>
-
- <style>
- body {
- margin: 0;
- font-family: Arial, sans-serif;
- background: linear-gradient(135deg, #1e1e2f, #2b5876);
- color: white;
- display: flex;
- justify-content: center;
- align-items: center;
- height: 100vh;
- }
-
- * {
- box-sizing: border-box;
- }
-
- .card {
- background: rgba(255,255,255,0.08);
- backdrop-filter: blur(10px);
- padding: 25px;
- border-radius: 16px;
- width: 90%;
- max-width: 360px;
- box-shadow: 0 8px 20px rgba(0,0,0,0.3);
- animation: fadeIn 0.8s ease;
- }
-
- h2 {
- margin-top: 0;
- text-align: center;
- }
-
- .info {
- font-size: 12px;
- opacity: 0.8;
- margin-bottom: 15px;
- text-align: center;
- }
-
- input {
- width: 100%;
- padding: 12px;
- margin: 8px 0;
- border-radius: 10px;
- border: none;
- outline: none;
- font-size: 14px;
- transition: 0.2s;
- }
-
- input:focus {
- transform: scale(1.02);
- }
-
- button {
- width: 100%;
- padding: 12px;
- margin-top: 10px;
- border: none;
- border-radius: 10px;
- background: #00c6ff;
- color: white;
- font-size: 16px;
- cursor: pointer;
- transition: 0.3s;
- }
-
- button:hover {
- background: #0072ff;
- transform: translateY(-2px);
- }
-
- .mac {
- font-size: 11px;
- text-align: center;
- margin-bottom: 15px;
- opacity: 0.7;
- word-break: break-all;
- }
-
- .pw-wrapper {
- position: relative;
- width: 100%;
- margin: 8px 0;
- }
-
- .pw-wrapper input {
- width: 100%;
- padding: 12px 40px 12px 12px; /* Platz für Icon rechts */
- border-radius: 10px;
- border: none;
- outline: none;
- font-size: 14px;
- box-sizing: border-box;
- }
-
- .toggle-eye {
- position: absolute;
- right: 12px;
- top: 50%;
- transform: translateY(-50%);
- cursor: pointer;
- font-size: 16px;
- opacity: 0.6;
- user-select: none;
- transition: 0.2s;
- }
-
- .toggle-eye:hover {
- opacity: 1;
- }
-
- @keyframes fadeIn {
- from {opacity: 0; transform: translateY(10px);}
- to {opacity: 1; transform: translateY(0);}
- }
- </style>
- </head>
-
- <body>
-
- <div class="card">
-
- <h2>Wemos Setup</h2>
-
- <div class="mac">Device: )rawliteral"
- + mac
- + R"rawliteral(</div>
-
- <form action="/save" method="POST">
-
- <input name="ssid" placeholder="WLAN Name (SSID)">
-
- <div class="pw-wrapper">
- <input id="pass" name="pass" type="password" placeholder="Passwort">
- <span class="toggle-eye" onclick="togglePassword()">👁</span>
- </div>
-
- <button type="submit">Speichern & Verbinden</button>
- </form>
-
- <div class="btn-row">
- <button type="button" onclick="ledOn()">LED EIN</button>
- <button type="button" onclick="ledOff()">LED AUS</button>
- </div>
-
- </div>
-
- <script>
-
- let ws;
-
- function connectWS() {
- ws = new WebSocket("ws://" + location.hostname + ":81/");
-
- ws.onopen = () => {
- console.log("WebSocket connected");
- };
-
- ws.onmessage = (e) => {
- console.log("ESP:", e.data);
- };
-
- ws.onclose = () => {
- console.log("WS disconnected → reconnect");
- setTimeout(connectWS, 1000);
- };
- }
-
- connectWS();
-
- function ledOn() {
- if (ws && ws.readyState === 1) {
- ws.send("/ledOn");
- }
- }
-
- function ledOff() {
- if (ws && ws.readyState === 1) {
- ws.send("/ledOff");
- }
- }
-
- function togglePassword() {
- const pass = document.getElementById("pass");
- const eye = document.querySelector(".toggle-eye");
-
- if (pass.type === "password") {
- pass.type = "text";
- eye.textContent = "🚫";
- } else {
- pass.type = "password";
- eye.textContent = "👁";
- }
- }
-
- </script>
-
- </body>
- </html>
- )rawliteral";
-
- webServer.send(200, "text/html", html);
- }
-
- // ---------- SAVE ----------
- void handleSave() {
- String ssid = webServer.arg("ssid");
- String pass = webServer.arg("pass");
-
- ssid.toCharArray(config.ssid, 32);
- pass.toCharArray(config.pass, 64);
- config.valid = true;
-
- saveConfig();
-
- webServer.send(200, "text/html", "<h2>Gespeichert!</h2><p>Wemos startet neu...</p>");
-
- delay(1500);
- ESP.restart();
- }
-
- // ---------- NOT FOUND (CAPTIVE REDIRECT) ----------
- void handleNotFound() {
- webServer.sendHeader("Location", "http://192.168.4.1/", true);
- webServer.send(302, "text/plain", "");
- }
-
- // ---------- START ACCESS POINT ----------
- void startAP() {
- WiFi.mode(WIFI_AP);
-
- WiFi.softAP(ap_ssid, ap_pass);
- delay(500);
-
- IPAddress apIP = WiFi.softAPIP();
- Serial.print("AP IP: ");
- Serial.println(apIP);
-
- // DNS: ALLE Domains auf ESP IP umleiten
- dnsServer.start(DNS_PORT, "*", apIP);
-
- // Webserver Routes
- webServer.on("/", handleRoot);
- webServer.on("/save", HTTP_POST, handleSave);
- webServer.onNotFound(handleNotFound);
-
- webServer.begin();
-
- Serial.println("Captive Portal gestartet");
- }
-
- // ---------- CONNECT STA ----------
- bool connectSTA() {
- WiFi.mode(WIFI_STA);
- WiFi.begin(config.ssid, config.pass);
-
- Serial.println();
- Serial.print("Verbinde mit WLAN: ");
- Serial.println(config.ssid);
-
- int tries = 0;
- while (WiFi.status() != WL_CONNECTED && tries < 20) {
- delay(500);
- Serial.print(".");
- tries++;
- }
-
- if (WiFi.status() == WL_CONNECTED) {
- Serial.println();
- Serial.println("\nVerbunden!");
- Serial.print("IP: ");
- Serial.println(WiFi.localIP());
-
- webServer.on("/", handleRoot);
- webServer.on("/save", HTTP_POST, handleSave);
- webServer.begin();
-
- webSocket.begin();
- webSocket.onEvent(webSocketEvent);
- Serial.println("WebSocket gestartet auf Port 81");
-
- return true;
- }
-
- return false;
- }
-
- void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
-
- switch (type) {
- case WStype_CONNECTED: {
- IPAddress ip = webSocket.remoteIP(num);
- Serial.printf("WS Client %u verbunden von %d.%d.%d.%d\n", num, ip[0], ip[1], ip[2], ip[3]);
- webSocket.sendTXT(num, "Connected");
- break;
- }
-
- case WStype_DISCONNECTED:
- Serial.printf("WS Client %u getrennt\n", num);
- break;
-
- case WStype_TEXT: {
- String msg = String((char*) payload);
- Serial.print("WS empfangen: ");
- Serial.println(msg);
-
- if (msg == "/ledOn") {
- digitalWrite(ledPin, LOW);
- webSocket.sendTXT(num, "LED ON");
- } else if (msg == "/ledOff") {
- digitalWrite(ledPin, HIGH);
- webSocket.sendTXT(num, "LED OFF");
- } else {
- String s = "ACK: " + msg;
- webSocket.sendTXT(num, s);
- }
-
- break;
- }
-
- default:
- break;
- }
- }
-
- void setState(AppState newState) {
- Serial.printf("STATE %s -> %s\n", stateToString(state), stateToString(newState));
- state = newState;
- }
-
- void handleBoot() {
-
- if (config.valid) {
- WiFi.mode(WIFI_STA);
- WiFi.begin(config.ssid, config.pass);
- setState(STATE_CONNECTING);
- }
- else {
- startAP();
- setState(STATE_AP_MODE);
- }
- }
-
- void handleConnecting() {
-
- if (WiFi.status() == WL_CONNECTED) {
- startSTAWebServer();
- setState(STATE_STA_MODE);
- return;
- }
-
- if (millis() - connectStart > 10000) {
- Serial.println("Connect Timeout");
- startAP();
- setState(STATE_AP_MODE);
- }
- }
-
- void handleAPMode() {
- dnsServer.processNextRequest();
- webServer.handleClient();
- }
-
- void handleSTAMode() {
- webServer.handleClient();
- webSocket.loop();
-
- if (WiFi.status() != WL_CONNECTED) {
- Serial.println("WLAN verloren");
- WiFi.begin(config.ssid, config.pass);
- connectStart = millis();
- setState(STATE_CONNECTING);
- }
- }
-
- void handleError() {
- digitalWrite(ledPin, LOW);
- }
-
- void startSTAWebServer()
- {
- Serial.println("Starte STA Services");
-
- // Webseite
- webServer.on("/", handleRoot);
-
- // optional
- webServer.on("/save", HTTP_POST, handleSave);
- webServer.begin();
-
- // WebSocket
- webSocket.begin();
- webSocket.onEvent(webSocketEvent);
-
- Serial.print("HTTP Server: http://");
- Serial.println(WiFi.localIP());
-
- Serial.print("WebSocket: ws://");
- Serial.print(WiFi.localIP());
- Serial.println(":81/");
- }
-
- // ---------- SETUP ----------
- void setup() {
- Serial.begin(115200);
- pinMode(ledPin, OUTPUT);
- digitalWrite(ledPin, HIGH); // LED zunächst AUS
-
- loadConfig();
-
- setState(STATE_BOOT);
- connectStart = millis();
- }
-
- // ---------- LOOP ----------
- void loop() {
-
- switch (state) {
- case STATE_BOOT:
- handleBoot();
- break;
-
- case STATE_AP_MODE:
- handleAPMode();
- break;
-
- case STATE_CONNECTING:
- handleConnecting();
- break;
-
- case STATE_STA_MODE:
- handleSTAMode();
- break;
-
- case STATE_ERROR:
- handleError();
- break;
- }
- }
|