Wemos D1 Mini Frimware zur Steuerung einer RGBW-LED-Lampe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LEDLamp.ino 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #include "Arduino.h"
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266WebServer.h>
  4. #include <EEPROM.h>
  5. #include <DNSServer.h>
  6. const int ledPin = LED_BUILTIN; // The built-in LED on the Wemos D1 Mini is on GPIO2 (D4)
  7. ESP8266WebServer webServer(80);
  8. WiFiServer server(1234);
  9. DNSServer dnsServer;
  10. const byte DNS_PORT = 53;
  11. // AP Daten
  12. const char *ap_ssid = "Wemos_Setup";
  13. const char *ap_pass = "admin123";
  14. // WLAN Config
  15. struct Config {
  16. char ssid[32];
  17. char pass[64];
  18. bool valid;
  19. };
  20. Config config;
  21. // ---------- EEPROM ----------
  22. void saveConfig() {
  23. EEPROM.begin(sizeof(Config));
  24. EEPROM.put(0, config);
  25. EEPROM.commit();
  26. }
  27. void loadConfig() {
  28. EEPROM.begin(sizeof(Config));
  29. EEPROM.get(0, config);
  30. if (config.valid != true) {
  31. config.valid = false;
  32. }
  33. }
  34. // ---------- CAPTIVE PORTAL PAGE ----------
  35. void handleRoot() {
  36. String mac = WiFi.macAddress();
  37. String html =
  38. R"rawliteral(
  39. <!DOCTYPE html>
  40. <html>
  41. <script>
  42. function togglePassword() {
  43. const pass = document.getElementById("pass");
  44. const eye = document.querySelector(".toggle-eye");
  45. if (pass.type === "password") {
  46. pass.type = "text";
  47. eye.textContent = "❌";
  48. } else {
  49. pass.type = "password";
  50. eye.textContent = "👁";
  51. }
  52. }
  53. </script>
  54. <head>
  55. <meta name="viewport" content="width=device-width, initial-scale=1">
  56. <title>Wemos Setup</title>
  57. <style>
  58. body {
  59. margin: 0;
  60. font-family: Arial, sans-serif;
  61. background: linear-gradient(135deg, #1e1e2f, #2b5876);
  62. color: white;
  63. display: flex;
  64. justify-content: center;
  65. align-items: center;
  66. height: 100vh;
  67. }
  68. * {
  69. box-sizing: border-box;
  70. }
  71. .card {
  72. background: rgba(255,255,255,0.08);
  73. backdrop-filter: blur(10px);
  74. padding: 25px;
  75. border-radius: 16px;
  76. width: 90%;
  77. max-width: 360px;
  78. box-shadow: 0 8px 20px rgba(0,0,0,0.3);
  79. animation: fadeIn 0.8s ease;
  80. }
  81. h2 {
  82. margin-top: 0;
  83. text-align: center;
  84. }
  85. .info {
  86. font-size: 12px;
  87. opacity: 0.8;
  88. margin-bottom: 15px;
  89. text-align: center;
  90. }
  91. input {
  92. width: 100%;
  93. padding: 12px;
  94. margin: 8px 0;
  95. border-radius: 10px;
  96. border: none;
  97. outline: none;
  98. font-size: 14px;
  99. transition: 0.2s;
  100. }
  101. input:focus {
  102. transform: scale(1.02);
  103. }
  104. button {
  105. width: 100%;
  106. padding: 12px;
  107. margin-top: 10px;
  108. border: none;
  109. border-radius: 10px;
  110. background: #00c6ff;
  111. color: white;
  112. font-size: 16px;
  113. cursor: pointer;
  114. transition: 0.3s;
  115. }
  116. button:hover {
  117. background: #0072ff;
  118. transform: translateY(-2px);
  119. }
  120. .mac {
  121. font-size: 11px;
  122. text-align: center;
  123. margin-bottom: 15px;
  124. opacity: 0.7;
  125. word-break: break-all;
  126. }
  127. .pw-wrapper {
  128. position: relative;
  129. width: 100%;
  130. margin: 8px 0;
  131. }
  132. .pw-wrapper input {
  133. width: 100%;
  134. padding: 12px 40px 12px 12px; /* Platz für Icon rechts */
  135. border-radius: 10px;
  136. border: none;
  137. outline: none;
  138. font-size: 14px;
  139. box-sizing: border-box;
  140. }
  141. .toggle-eye {
  142. position: absolute;
  143. right: 12px;
  144. top: 50%;
  145. transform: translateY(-50%);
  146. cursor: pointer;
  147. font-size: 16px;
  148. opacity: 0.6;
  149. user-select: none;
  150. transition: 0.2s;
  151. }
  152. .toggle-eye:hover {
  153. opacity: 1;
  154. }
  155. @keyframes fadeIn {
  156. from {opacity: 0; transform: translateY(10px);}
  157. to {opacity: 1; transform: translateY(0);}
  158. }
  159. </style>
  160. </head>
  161. <body>
  162. <div class="card">
  163. <h2>Wemos Setup</h2>
  164. <div class="mac">Device: )rawliteral"
  165. + mac
  166. + R"rawliteral(</div>
  167. <form action="/save" method="POST">
  168. <input name="ssid" placeholder="WLAN Name (SSID)">
  169. <div class="pw-wrapper">
  170. <input id="pass" name="pass" type="password" placeholder="Passwort">
  171. <span class="toggle-eye" onclick="togglePassword()">👁</span>
  172. </div>
  173. <button type="submit">Verbinden</button>
  174. </form>
  175. <div class="info">
  176. Verbinde deinen Wemos mit einem WLAN Netzwerk
  177. </div>
  178. </div>
  179. </body>
  180. </html>
  181. )rawliteral";
  182. webServer.send(200, "text/html", html);
  183. }
  184. // ---------- SAVE ----------
  185. void handleSave() {
  186. String ssid = webServer.arg("ssid");
  187. String pass = webServer.arg("pass");
  188. ssid.toCharArray(config.ssid, 32);
  189. pass.toCharArray(config.pass, 64);
  190. config.valid = true;
  191. saveConfig();
  192. webServer.send(200, "text/html", "<h2>Gespeichert!</h2><p>Wemos startet neu...</p>");
  193. delay(1500);
  194. ESP.restart();
  195. }
  196. // ---------- NOT FOUND (CAPTIVE REDIRECT) ----------
  197. void handleNotFound() {
  198. webServer.sendHeader("Location", "http://192.168.4.1/", true);
  199. webServer.send(302, "text/plain", "");
  200. }
  201. // ---------- START ACCESS POINT ----------
  202. void startAP() {
  203. WiFi.mode(WIFI_AP);
  204. WiFi.softAP(ap_ssid, ap_pass);
  205. delay(500);
  206. IPAddress apIP = WiFi.softAPIP();
  207. Serial.print("AP IP: ");
  208. Serial.println(apIP);
  209. // DNS: ALLE Domains auf ESP IP umleiten
  210. dnsServer.start(DNS_PORT, "*", apIP);
  211. // Webserver Routes
  212. webServer.on("/", handleRoot);
  213. webServer.on("/save", HTTP_POST, handleSave);
  214. webServer.onNotFound(handleNotFound);
  215. webServer.begin();
  216. Serial.println("Captive Portal gestartet");
  217. }
  218. // ---------- CONNECT STA ----------
  219. bool connectSTA() {
  220. WiFi.mode(WIFI_STA);
  221. WiFi.begin(config.ssid, config.pass);
  222. Serial.println();
  223. Serial.print("Verbinde mit WLAN: ");
  224. Serial.println(config.ssid);
  225. int tries = 0;
  226. while (WiFi.status() != WL_CONNECTED && tries < 20) {
  227. delay(500);
  228. Serial.print(".");
  229. tries++;
  230. }
  231. if (WiFi.status() == WL_CONNECTED) {
  232. Serial.println();
  233. Serial.println("\nVerbunden!");
  234. Serial.print("IP: ");
  235. Serial.println(WiFi.localIP());
  236. server.begin();
  237. Serial.println("TCP Server gestartet auf Port 1234");
  238. return true;
  239. }
  240. return false;
  241. }
  242. // ---------- SETUP ----------
  243. void setup() {
  244. Serial.begin(115200);
  245. loadConfig();
  246. if (config.valid && connectSTA()) {
  247. Serial.println("STA Mode aktiv");
  248. } else {
  249. Serial.println();
  250. Serial.println();
  251. Serial.println("Starte Captive Portal");
  252. startAP();
  253. }
  254. }
  255. // ---------- LOOP ----------
  256. void loop() {
  257. if (WiFi.getMode() == WIFI_AP) {
  258. dnsServer.processNextRequest();
  259. webServer.handleClient();
  260. return;
  261. }
  262. webServer.handleClient(); // optional auch im STA Mode
  263. WiFiClient client = server.available();
  264. if (!client) {
  265. return;
  266. }
  267. Serial.println("Client verbunden");
  268. client.println("Connected");
  269. while (client.connected()) {
  270. if (!client.available()) {
  271. delay(1);
  272. continue;
  273. }
  274. String msg = client.readStringUntil('\n');
  275. msg.trim();
  276. Serial.print("Empfangen: ");
  277. Serial.println(msg);
  278. client.println("ACK: " + msg);
  279. if (msg.equalsIgnoreCase("/ledOn")) {
  280. digitalWrite(ledPin, LOW);
  281. }
  282. if (msg.equalsIgnoreCase("/ledOff")) {
  283. digitalWrite(ledPin, HIGH);
  284. }
  285. if (msg.equalsIgnoreCase("quit")) {
  286. break;
  287. }
  288. }
  289. client.stop();
  290. Serial.println("Client getrennt");
  291. }