| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- #include "Arduino.h"
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <EEPROM.h>
- #include <DNSServer.h>
-
- const int ledPin = LED_BUILTIN; // The built-in LED on the Wemos D1 Mini is on GPIO2 (D4)
-
- ESP8266WebServer webServer(80);
- WiFiServer server(1234);
- DNSServer dnsServer;
-
- 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;
- };
-
- 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>
- <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;
- }
-
- @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)">
- <input name="pass" type="password" placeholder="Passwort">
-
- <button type="submit">Verbinden</button>
- </form>
-
- <div class="info">
- Verbinde deinen Wemos mit einem WLAN Netzwerk
- </div>
- </div>
-
- </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());
-
- server.begin();
- Serial.println("TCP Server gestartet auf Port 1234");
-
- return true;
- }
-
-
- return false;
- }
-
- // ---------- SETUP ----------
- void setup() {
- Serial.begin(115200);
-
- loadConfig();
-
- if (config.valid && connectSTA()) {
- Serial.println("STA Mode aktiv");
- } else {
- Serial.println();
- Serial.println();
- Serial.println("Starte Captive Portal");
- startAP();
- }
- }
-
- // ---------- LOOP ----------
- void loop() {
-
- if (WiFi.getMode() == WIFI_AP) {
- dnsServer.processNextRequest();
- webServer.handleClient();
- return;
- }
-
- webServer.handleClient(); // optional auch im STA Mode
-
- WiFiClient client = server.available();
-
- if (!client) {
- return;
- }
-
- Serial.println("Client verbunden");
- client.println("Connected");
-
- while (client.connected()) {
-
- if (!client.available()) {
- delay(1);
- continue;
- }
-
- String msg = client.readStringUntil('\n');
- msg.trim();
-
- Serial.print("Empfangen: ");
- Serial.println(msg);
-
- client.println("ACK: " + msg);
-
- if (msg.equalsIgnoreCase("/ledOn")) {
- digitalWrite(ledPin, LOW);
- }
-
- if (msg.equalsIgnoreCase("/ledOff")) {
- digitalWrite(ledPin, HIGH);
- }
-
- if (msg.equalsIgnoreCase("quit")) {
- break;
- }
- }
-
- client.stop();
- Serial.println("Client getrennt");
- }
|