Wemos D1 Mini Frimware zur Steuerung einer RGBW-LED-Lampe
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

LEDLamp.ino 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #include "HtmlPages.h"
  8. AppState state = STATE_BOOT;
  9. unsigned long connectStart = 0;
  10. const int ledPin = LED_BUILTIN; // The built-in LED on the Wemos D1 Mini is on GPIO2 (D4)
  11. const byte DNS_PORT = 53;
  12. // AP Daten
  13. const char *ap_ssid = "Wemos_Setup";
  14. const char *ap_pass = "admin123";
  15. // WLAN Config
  16. struct Config {
  17. char ssid[32];
  18. char pass[64];
  19. bool valid;
  20. };
  21. ESP8266WebServer webServer(80);
  22. WebSocketsServer webSocket(81);
  23. DNSServer dnsServer;
  24. Config config;
  25. // ---------- EEPROM ----------
  26. void saveConfig() {
  27. EEPROM.begin(sizeof(Config));
  28. EEPROM.put(0, config);
  29. EEPROM.commit();
  30. }
  31. void loadConfig() {
  32. EEPROM.begin(sizeof(Config));
  33. EEPROM.get(0, config);
  34. if (config.valid != true) {
  35. config.valid = false;
  36. }
  37. }
  38. // ---------- CAPTIVE PORTAL PAGE ----------
  39. void handleSetupPage() {
  40. String mac = WiFi.macAddress();
  41. String html = getSetupPage(mac);
  42. webServer.send(200, "text/html", html);
  43. }
  44. // ---------- Wemos Control PAGE ----------
  45. void handleSTAControlPage() {
  46. String mac = WiFi.macAddress();
  47. String html = getSTAControlPage(mac);
  48. webServer.send(200, "text/html", html);
  49. }
  50. // ---------- SAVE ----------
  51. void handleSave() {
  52. String ssid = webServer.arg("ssid");
  53. String pass = webServer.arg("pass");
  54. ssid.toCharArray(config.ssid, 32);
  55. pass.toCharArray(config.pass, 64);
  56. config.valid = true;
  57. saveConfig();
  58. webServer.send(200, "text/html", "<h2>Gespeichert!</h2><p>Wemos startet neu...</p>");
  59. delay(1500);
  60. ESP.restart();
  61. }
  62. // ---------- NOT FOUND (CAPTIVE REDIRECT) ----------
  63. void handleNotFound() {
  64. webServer.sendHeader("Location", "http://192.168.4.1/", true);
  65. webServer.send(302, "text/plain", "");
  66. }
  67. // ---------- START ACCESS POINT ----------
  68. void startAP() {
  69. WiFi.mode(WIFI_AP);
  70. WiFi.softAP(ap_ssid, ap_pass);
  71. delay(500);
  72. IPAddress apIP = WiFi.softAPIP();
  73. Serial.print("AP IP: ");
  74. Serial.println(apIP);
  75. // DNS: ALLE Domains auf ESP IP umleiten
  76. dnsServer.start(DNS_PORT, "*", apIP);
  77. // Webserver Routes
  78. webServer.on("/", handleSetupPage);
  79. webServer.on("/save", HTTP_POST, handleSave);
  80. webServer.onNotFound(handleNotFound);
  81. webServer.begin();
  82. Serial.println("Captive Portal gestartet");
  83. }
  84. void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
  85. switch (type) {
  86. case WStype_CONNECTED: {
  87. IPAddress ip = webSocket.remoteIP(num);
  88. Serial.printf("WS Client %u verbunden von %d.%d.%d.%d\n", num, ip[0], ip[1], ip[2], ip[3]);
  89. webSocket.sendTXT(num, "Connected");
  90. break;
  91. }
  92. case WStype_DISCONNECTED:
  93. Serial.printf("WS Client %u getrennt\n", num);
  94. break;
  95. case WStype_TEXT: {
  96. String msg = String((char*) payload);
  97. Serial.print("WS empfangen: ");
  98. Serial.println(msg);
  99. if (msg == "/ledOn") {
  100. digitalWrite(ledPin, LOW);
  101. delay(10);
  102. if (digitalRead(ledPin) == LOW) {
  103. webSocket.sendTXT(num, "LED ON");
  104. }
  105. } else if (msg == "/ledOff") {
  106. digitalWrite(ledPin, HIGH);
  107. delay(10);
  108. if (digitalRead(ledPin) == HIGH) {
  109. webSocket.sendTXT(num, "LED OFF");
  110. }
  111. } else if (msg == "/resetWifiCfg") {
  112. resetWifiConfiguration(num);
  113. } else {
  114. String s = "ACK: " + msg;
  115. webSocket.sendTXT(num, s);
  116. }
  117. break;
  118. }
  119. default:
  120. break;
  121. }
  122. }
  123. void resetWifiConfiguration(uint8_t num) {
  124. EEPROM.begin(sizeof(Config));
  125. config.valid = false;
  126. EEPROM.put(0, config);
  127. webSocket.sendTXT(num, "Wifi_Reset_Success");
  128. delay(200);
  129. EEPROM.commit();
  130. ESP.restart();
  131. }
  132. void setState(AppState newState) {
  133. Serial.printf("STATE %s -> %s\n", stateToString(state), stateToString(newState));
  134. state = newState;
  135. }
  136. void handleBoot() {
  137. if (config.valid) {
  138. WiFi.mode(WIFI_STA);
  139. WiFi.begin(config.ssid, config.pass);
  140. setState(STATE_CONNECTING);
  141. }
  142. else {
  143. startAP();
  144. setState(STATE_AP_MODE);
  145. }
  146. }
  147. void handleConnecting() {
  148. if (WiFi.status() == WL_CONNECTED) {
  149. startSTAWebServer();
  150. setState(STATE_STA_MODE);
  151. return;
  152. }
  153. if (millis() - connectStart > 10000) {
  154. Serial.println("Connect Timeout");
  155. startAP();
  156. setState(STATE_AP_MODE);
  157. }
  158. }
  159. void handleAPMode() {
  160. dnsServer.processNextRequest();
  161. webServer.handleClient();
  162. }
  163. void handleSTAMode() {
  164. webServer.handleClient();
  165. webSocket.loop();
  166. if (WiFi.status() != WL_CONNECTED) {
  167. Serial.println("WLAN verloren");
  168. WiFi.begin(config.ssid, config.pass);
  169. connectStart = millis();
  170. setState(STATE_CONNECTING);
  171. }
  172. }
  173. void handleError() {
  174. digitalWrite(ledPin, LOW);
  175. }
  176. void startSTAWebServer()
  177. {
  178. Serial.println("Starte STA Services");
  179. // Webseite
  180. webServer.on("/", handleSTAControlPage);
  181. // optional
  182. webServer.on("/save", HTTP_POST, handleSave);
  183. webServer.begin();
  184. // WebSocket
  185. webSocket.begin();
  186. webSocket.onEvent(webSocketEvent);
  187. Serial.print("HTTP Server: http://");
  188. Serial.println(WiFi.localIP());
  189. Serial.print("WebSocket: ws://");
  190. Serial.print(WiFi.localIP());
  191. Serial.println(":81/");
  192. }
  193. // ---------- SETUP ----------
  194. void setup() {
  195. Serial.begin(115200);
  196. pinMode(ledPin, OUTPUT);
  197. digitalWrite(ledPin, HIGH); // LED zunächst AUS
  198. loadConfig();
  199. setState(STATE_BOOT);
  200. connectStart = millis();
  201. }
  202. // ---------- LOOP ----------
  203. void loop() {
  204. switch (state) {
  205. case STATE_BOOT:
  206. handleBoot();
  207. break;
  208. case STATE_AP_MODE:
  209. handleAPMode();
  210. break;
  211. case STATE_CONNECTING:
  212. handleConnecting();
  213. break;
  214. case STATE_STA_MODE:
  215. handleSTAMode();
  216. break;
  217. case STATE_ERROR:
  218. handleError();
  219. break;
  220. }
  221. }