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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #include "Arduino.h"
  2. #include <ESP8266WebServer.h>
  3. #include <WebSocketsServer.h>
  4. #include <EEPROM.h>
  5. #include <DNSServer.h>
  6. #include "States.h"
  7. AppState state = STATE_BOOT;
  8. unsigned long connectStart = 0;
  9. const int ledPin = LED_BUILTIN; // The built-in LED on the Wemos D1 Mini is on GPIO2 (D4)
  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. ESP8266WebServer webServer(80);
  21. WebSocketsServer webSocket(81);
  22. DNSServer dnsServer;
  23. Config config;
  24. // ---------- EEPROM ----------
  25. void saveConfig() {
  26. EEPROM.begin(sizeof(Config));
  27. EEPROM.put(0, config);
  28. EEPROM.commit();
  29. }
  30. void loadConfig() {
  31. EEPROM.begin(sizeof(Config));
  32. EEPROM.get(0, config);
  33. if (config.valid != true) {
  34. config.valid = false;
  35. }
  36. }
  37. // ---------- CAPTIVE PORTAL PAGE ----------
  38. void handleRoot() {
  39. String mac = WiFi.macAddress();
  40. String html =
  41. R"rawliteral(
  42. <!DOCTYPE html>
  43. <html>
  44. <script>
  45. function togglePassword() {
  46. const pass = document.getElementById("pass");
  47. const eye = document.querySelector(".toggle-eye");
  48. if (pass.type === "password") {
  49. pass.type = "text";
  50. eye.textContent = "❌";
  51. } else {
  52. pass.type = "password";
  53. eye.textContent = "👁";
  54. }
  55. }
  56. </script>
  57. <head>
  58. <meta name="viewport" content="width=device-width, initial-scale=1">
  59. <title>Wemos Setup</title>
  60. <style>
  61. body {
  62. margin: 0;
  63. font-family: Arial, sans-serif;
  64. background: linear-gradient(135deg, #1e1e2f, #2b5876);
  65. color: white;
  66. display: flex;
  67. justify-content: center;
  68. align-items: center;
  69. height: 100vh;
  70. }
  71. * {
  72. box-sizing: border-box;
  73. }
  74. .card {
  75. background: rgba(255,255,255,0.08);
  76. backdrop-filter: blur(10px);
  77. padding: 25px;
  78. border-radius: 16px;
  79. width: 90%;
  80. max-width: 360px;
  81. box-shadow: 0 8px 20px rgba(0,0,0,0.3);
  82. animation: fadeIn 0.8s ease;
  83. }
  84. h2 {
  85. margin-top: 0;
  86. text-align: center;
  87. }
  88. .info {
  89. font-size: 12px;
  90. opacity: 0.8;
  91. margin-bottom: 15px;
  92. text-align: center;
  93. }
  94. input {
  95. width: 100%;
  96. padding: 12px;
  97. margin: 8px 0;
  98. border-radius: 10px;
  99. border: none;
  100. outline: none;
  101. font-size: 14px;
  102. transition: 0.2s;
  103. }
  104. input:focus {
  105. transform: scale(1.02);
  106. }
  107. button {
  108. width: 100%;
  109. padding: 12px;
  110. margin-top: 10px;
  111. border: none;
  112. border-radius: 10px;
  113. background: #00c6ff;
  114. color: white;
  115. font-size: 16px;
  116. cursor: pointer;
  117. transition: 0.3s;
  118. }
  119. button:hover {
  120. background: #0072ff;
  121. transform: translateY(-2px);
  122. }
  123. .mac {
  124. font-size: 11px;
  125. text-align: center;
  126. margin-bottom: 15px;
  127. opacity: 0.7;
  128. word-break: break-all;
  129. }
  130. .pw-wrapper {
  131. position: relative;
  132. width: 100%;
  133. margin: 8px 0;
  134. }
  135. .pw-wrapper input {
  136. width: 100%;
  137. padding: 12px 40px 12px 12px; /* Platz für Icon rechts */
  138. border-radius: 10px;
  139. border: none;
  140. outline: none;
  141. font-size: 14px;
  142. box-sizing: border-box;
  143. }
  144. .toggle-eye {
  145. position: absolute;
  146. right: 12px;
  147. top: 50%;
  148. transform: translateY(-50%);
  149. cursor: pointer;
  150. font-size: 16px;
  151. opacity: 0.6;
  152. user-select: none;
  153. transition: 0.2s;
  154. }
  155. .toggle-eye:hover {
  156. opacity: 1;
  157. }
  158. @keyframes fadeIn {
  159. from {opacity: 0; transform: translateY(10px);}
  160. to {opacity: 1; transform: translateY(0);}
  161. }
  162. </style>
  163. </head>
  164. <body>
  165. <div class="card">
  166. <h2>Wemos Setup</h2>
  167. <div class="mac">Device: )rawliteral"
  168. + mac
  169. + R"rawliteral(</div>
  170. <form action="/save" method="POST">
  171. <input name="ssid" placeholder="WLAN Name (SSID)">
  172. <div class="pw-wrapper">
  173. <input id="pass" name="pass" type="password" placeholder="Passwort">
  174. <span class="toggle-eye" onclick="togglePassword()">👁</span>
  175. </div>
  176. <button type="submit">Speichern & Verbinden</button>
  177. </form>
  178. <div class="btn-row">
  179. <button type="button" onclick="ledOn()">LED EIN</button>
  180. <button type="button" onclick="ledOff()">LED AUS</button>
  181. </div>
  182. </div>
  183. <script>
  184. let ws;
  185. function connectWS() {
  186. ws = new WebSocket("ws://" + location.hostname + ":81/");
  187. ws.onopen = () => {
  188. console.log("WebSocket connected");
  189. };
  190. ws.onmessage = (e) => {
  191. console.log("ESP:", e.data);
  192. };
  193. ws.onclose = () => {
  194. console.log("WS disconnected → reconnect");
  195. setTimeout(connectWS, 1000);
  196. };
  197. }
  198. connectWS();
  199. function ledOn() {
  200. if (ws && ws.readyState === 1) {
  201. ws.send("/ledOn");
  202. }
  203. }
  204. function ledOff() {
  205. if (ws && ws.readyState === 1) {
  206. ws.send("/ledOff");
  207. }
  208. }
  209. function togglePassword() {
  210. const pass = document.getElementById("pass");
  211. const eye = document.querySelector(".toggle-eye");
  212. if (pass.type === "password") {
  213. pass.type = "text";
  214. eye.textContent = "🚫";
  215. } else {
  216. pass.type = "password";
  217. eye.textContent = "👁";
  218. }
  219. }
  220. </script>
  221. </body>
  222. </html>
  223. )rawliteral";
  224. webServer.send(200, "text/html", html);
  225. }
  226. // ---------- SAVE ----------
  227. void handleSave() {
  228. String ssid = webServer.arg("ssid");
  229. String pass = webServer.arg("pass");
  230. ssid.toCharArray(config.ssid, 32);
  231. pass.toCharArray(config.pass, 64);
  232. config.valid = true;
  233. saveConfig();
  234. webServer.send(200, "text/html", "<h2>Gespeichert!</h2><p>Wemos startet neu...</p>");
  235. delay(1500);
  236. ESP.restart();
  237. }
  238. // ---------- NOT FOUND (CAPTIVE REDIRECT) ----------
  239. void handleNotFound() {
  240. webServer.sendHeader("Location", "http://192.168.4.1/", true);
  241. webServer.send(302, "text/plain", "");
  242. }
  243. // ---------- START ACCESS POINT ----------
  244. void startAP() {
  245. WiFi.mode(WIFI_AP);
  246. WiFi.softAP(ap_ssid, ap_pass);
  247. delay(500);
  248. IPAddress apIP = WiFi.softAPIP();
  249. Serial.print("AP IP: ");
  250. Serial.println(apIP);
  251. // DNS: ALLE Domains auf ESP IP umleiten
  252. dnsServer.start(DNS_PORT, "*", apIP);
  253. // Webserver Routes
  254. webServer.on("/", handleRoot);
  255. webServer.on("/save", HTTP_POST, handleSave);
  256. webServer.onNotFound(handleNotFound);
  257. webServer.begin();
  258. Serial.println("Captive Portal gestartet");
  259. }
  260. // ---------- CONNECT STA ----------
  261. bool connectSTA() {
  262. WiFi.mode(WIFI_STA);
  263. WiFi.begin(config.ssid, config.pass);
  264. Serial.println();
  265. Serial.print("Verbinde mit WLAN: ");
  266. Serial.println(config.ssid);
  267. int tries = 0;
  268. while (WiFi.status() != WL_CONNECTED && tries < 20) {
  269. delay(500);
  270. Serial.print(".");
  271. tries++;
  272. }
  273. if (WiFi.status() == WL_CONNECTED) {
  274. Serial.println();
  275. Serial.println("\nVerbunden!");
  276. Serial.print("IP: ");
  277. Serial.println(WiFi.localIP());
  278. webServer.on("/", handleRoot);
  279. webServer.on("/save", HTTP_POST, handleSave);
  280. webServer.begin();
  281. webSocket.begin();
  282. webSocket.onEvent(webSocketEvent);
  283. Serial.println("WebSocket gestartet auf Port 81");
  284. return true;
  285. }
  286. return false;
  287. }
  288. void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
  289. switch (type) {
  290. case WStype_CONNECTED: {
  291. IPAddress ip = webSocket.remoteIP(num);
  292. Serial.printf("WS Client %u verbunden von %d.%d.%d.%d\n", num, 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. void setState(AppState newState) {
  320. Serial.printf("STATE %s -> %s\n", stateToString(state), stateToString(newState));
  321. state = newState;
  322. }
  323. void handleBoot() {
  324. if (config.valid) {
  325. WiFi.mode(WIFI_STA);
  326. WiFi.begin(config.ssid, config.pass);
  327. setState(STATE_CONNECTING);
  328. }
  329. else {
  330. startAP();
  331. setState(STATE_AP_MODE);
  332. }
  333. }
  334. void handleConnecting() {
  335. if (WiFi.status() == WL_CONNECTED) {
  336. startSTAWebServer();
  337. setState(STATE_STA_MODE);
  338. return;
  339. }
  340. if (millis() - connectStart > 10000) {
  341. Serial.println("Connect Timeout");
  342. startAP();
  343. setState(STATE_AP_MODE);
  344. }
  345. }
  346. void handleAPMode() {
  347. dnsServer.processNextRequest();
  348. webServer.handleClient();
  349. }
  350. void handleSTAMode() {
  351. webServer.handleClient();
  352. webSocket.loop();
  353. if (WiFi.status() != WL_CONNECTED) {
  354. Serial.println("WLAN verloren");
  355. WiFi.begin(config.ssid, config.pass);
  356. connectStart = millis();
  357. setState(STATE_CONNECTING);
  358. }
  359. }
  360. void handleError() {
  361. digitalWrite(ledPin, LOW);
  362. }
  363. void startSTAWebServer()
  364. {
  365. Serial.println("Starte STA Services");
  366. // Webseite
  367. webServer.on("/", handleRoot);
  368. // optional
  369. webServer.on("/save", HTTP_POST, handleSave);
  370. webServer.begin();
  371. // WebSocket
  372. webSocket.begin();
  373. webSocket.onEvent(webSocketEvent);
  374. Serial.print("HTTP Server: http://");
  375. Serial.println(WiFi.localIP());
  376. Serial.print("WebSocket: ws://");
  377. Serial.print(WiFi.localIP());
  378. Serial.println(":81/");
  379. }
  380. // ---------- SETUP ----------
  381. void setup() {
  382. Serial.begin(115200);
  383. pinMode(ledPin, OUTPUT);
  384. digitalWrite(ledPin, HIGH); // LED zunächst AUS
  385. loadConfig();
  386. setState(STATE_BOOT);
  387. connectStart = millis();
  388. }
  389. // ---------- LOOP ----------
  390. void loop() {
  391. switch (state) {
  392. case STATE_BOOT:
  393. handleBoot();
  394. break;
  395. case STATE_AP_MODE:
  396. handleAPMode();
  397. break;
  398. case STATE_CONNECTING:
  399. handleConnecting();
  400. break;
  401. case STATE_STA_MODE:
  402. handleSTAMode();
  403. break;
  404. case STATE_ERROR:
  405. handleError();
  406. break;
  407. }
  408. }