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 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include "Arduino.h"
  2. #include <ESP8266WebServer.h>
  3. #include <WebSocketsServer.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. WebSocketsServer webSocket(81);
  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">Speichern & Verbinden</button>
  174. </form>
  175. <div class="btn-row">
  176. <button type="button" onclick="ledOn()">LED EIN</button>
  177. <button type="button" onclick="ledOff()">LED AUS</button>
  178. </div>
  179. </div>
  180. <script>
  181. let ws;
  182. function connectWS() {
  183. ws = new WebSocket("ws://" + location.hostname + ":81/");
  184. ws.onopen = () => {
  185. console.log("WebSocket connected");
  186. };
  187. ws.onmessage = (e) => {
  188. console.log("ESP:", e.data);
  189. };
  190. ws.onclose = () => {
  191. console.log("WS disconnected → reconnect");
  192. setTimeout(connectWS, 1000);
  193. };
  194. }
  195. connectWS();
  196. function ledOn() {
  197. if (ws && ws.readyState === 1) {
  198. ws.send("/ledOn");
  199. }
  200. }
  201. function ledOff() {
  202. if (ws && ws.readyState === 1) {
  203. ws.send("/ledOff");
  204. }
  205. }
  206. function togglePassword() {
  207. const pass = document.getElementById("pass");
  208. const eye = document.querySelector(".toggle-eye");
  209. if (pass.type === "password") {
  210. pass.type = "text";
  211. eye.textContent = "🚫";
  212. } else {
  213. pass.type = "password";
  214. eye.textContent = "👁";
  215. }
  216. }
  217. </script>
  218. </body>
  219. </html>
  220. )rawliteral";
  221. webServer.send(200, "text/html", html);
  222. }
  223. // ---------- SAVE ----------
  224. void handleSave() {
  225. String ssid = webServer.arg("ssid");
  226. String pass = webServer.arg("pass");
  227. ssid.toCharArray(config.ssid, 32);
  228. pass.toCharArray(config.pass, 64);
  229. config.valid = true;
  230. saveConfig();
  231. webServer.send(200, "text/html", "<h2>Gespeichert!</h2><p>Wemos startet neu...</p>");
  232. delay(1500);
  233. ESP.restart();
  234. }
  235. // ---------- NOT FOUND (CAPTIVE REDIRECT) ----------
  236. void handleNotFound() {
  237. webServer.sendHeader("Location", "http://192.168.4.1/", true);
  238. webServer.send(302, "text/plain", "");
  239. }
  240. // ---------- START ACCESS POINT ----------
  241. void startAP() {
  242. WiFi.mode(WIFI_AP);
  243. WiFi.softAP(ap_ssid, ap_pass);
  244. delay(500);
  245. IPAddress apIP = WiFi.softAPIP();
  246. Serial.print("AP IP: ");
  247. Serial.println(apIP);
  248. // DNS: ALLE Domains auf ESP IP umleiten
  249. dnsServer.start(DNS_PORT, "*", apIP);
  250. // Webserver Routes
  251. webServer.on("/", handleRoot);
  252. webServer.on("/save", HTTP_POST, handleSave);
  253. webServer.onNotFound(handleNotFound);
  254. webServer.begin();
  255. Serial.println("Captive Portal gestartet");
  256. }
  257. // ---------- CONNECT STA ----------
  258. bool connectSTA() {
  259. WiFi.mode(WIFI_STA);
  260. WiFi.begin(config.ssid, config.pass);
  261. Serial.println();
  262. Serial.print("Verbinde mit WLAN: ");
  263. Serial.println(config.ssid);
  264. int tries = 0;
  265. while (WiFi.status() != WL_CONNECTED && tries < 20) {
  266. delay(500);
  267. Serial.print(".");
  268. tries++;
  269. }
  270. if (WiFi.status() == WL_CONNECTED) {
  271. Serial.println();
  272. Serial.println("\nVerbunden!");
  273. Serial.print("IP: ");
  274. Serial.println(WiFi.localIP());
  275. webServer.on("/", handleRoot);
  276. webServer.on("/save", HTTP_POST, handleSave);
  277. webServer.begin();
  278. webSocket.begin();
  279. webSocket.onEvent(webSocketEvent);
  280. Serial.println("WebSocket gestartet auf Port 81");
  281. return true;
  282. }
  283. return false;
  284. }
  285. void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
  286. switch (type) {
  287. case WStype_CONNECTED: {
  288. IPAddress ip = webSocket.remoteIP(num);
  289. Serial.printf(
  290. "WS Client %u verbunden von %d.%d.%d.%d\n",
  291. num,
  292. ip[0], ip[1], ip[2], ip[3]);
  293. webSocket.sendTXT(num, "Connected");
  294. break;
  295. }
  296. case WStype_DISCONNECTED:
  297. Serial.printf("WS Client %u getrennt\n", num);
  298. break;
  299. case WStype_TEXT: {
  300. String msg = String((char*) payload);
  301. Serial.print("WS empfangen: ");
  302. Serial.println(msg);
  303. if (msg == "/ledOn") {
  304. digitalWrite(ledPin, LOW);
  305. webSocket.sendTXT(num, "LED ON");
  306. } else if (msg == "/ledOff") {
  307. digitalWrite(ledPin, HIGH);
  308. webSocket.sendTXT(num, "LED OFF");
  309. } else {
  310. String s = "ACK: " + msg;
  311. webSocket.sendTXT(num, s);
  312. }
  313. break;
  314. }
  315. default:
  316. break;
  317. }
  318. }
  319. // ---------- SETUP ----------
  320. void setup() {
  321. Serial.begin(115200);
  322. pinMode(ledPin, OUTPUT);
  323. digitalWrite(ledPin, HIGH); // LED zunächst AUS
  324. loadConfig();
  325. if (config.valid && connectSTA()) {
  326. Serial.println("STA Mode aktiv");
  327. } else {
  328. Serial.println();
  329. Serial.println();
  330. Serial.println("Starte Captive Portal");
  331. startAP();
  332. }
  333. }
  334. // ---------- LOOP ----------
  335. void loop() {
  336. if (WiFi.getMode() == WIFI_AP) {
  337. dnsServer.processNextRequest();
  338. webServer.handleClient();
  339. return;
  340. }
  341. webServer.handleClient();
  342. webSocket.loop();
  343. }