Wemos D1 Mini Frimware zur Steuerung einer RGBW-LED-Lampe
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

LEDLamp.ino 5.6KB

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