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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include "Arduino.h"
  2. #include <ESP8266WebServer.h>
  3. #include <WebSocketsServer.h>
  4. #include <EEPROM.h>
  5. #include <DNSServer.h>
  6. #include <ArduinoJson.h>
  7. #include <Adafruit_Neopixel.h>
  8. #include "States.h"
  9. #include "Commands.h"
  10. #include "ConfigManager.h"
  11. #include "HtmlPages.h"
  12. #include "LEDStripManager.h"
  13. AppState state = STATE_BOOT;
  14. #define LED_COUNT 12 // Nbr of LEDs on stripe TODO: move to eeprom, configurable over wifi
  15. unsigned long connectStart = 0;
  16. const byte DNS_PORT = 53;
  17. // WLAN Config
  18. //struct Config {
  19. // char ssid[32];
  20. // char pass[64];
  21. // bool valid;
  22. //};
  23. // AP Daten
  24. const char *ap_ssid = "Wemos_Setup";
  25. const char *ap_pass = "admin123";
  26. ESP8266WebServer webServer(80);
  27. WebSocketsServer webSocket(81);
  28. DNSServer dnsServer;
  29. LEDStripManager ledMngr(LED_COUNT, LED_STRIP_DATA_PIN);
  30. ConfigManager configManager;
  31. // ---------- CAPTIVE PORTAL PAGE ----------
  32. void handleSetupPage() {
  33. String mac = WiFi.macAddress();
  34. String html = getSetupPage(mac);
  35. webServer.send(200, "text/html", html);
  36. }
  37. // ---------- Wemos Control PAGE ----------
  38. void handleSTAControlPage() {
  39. String mac = WiFi.macAddress();
  40. String html = getSTAControlPage(mac);
  41. webServer.send(200, "text/html", html);
  42. }
  43. // ---------- SAVE ----------
  44. void handleSave() {
  45. // String ssid = webServer.arg("ssid");
  46. // String pass = webServer.arg("pass");
  47. //
  48. // ssid.toCharArray(config.ssid, 32);
  49. // pass.toCharArray(config.pass, 64);
  50. // config.valid = true;
  51. //
  52. // saveConfig();
  53. Config cfg;
  54. String ssid = webServer.arg("ssid");
  55. String pass = webServer.arg("pass");
  56. ssid.toCharArray(cfg.ssid, sizeof(cfg.ssid));
  57. pass.toCharArray(cfg.pass, sizeof(cfg.pass));
  58. cfg.wifiValid = true;
  59. configManager.saveConfig(cfg);
  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. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  93. webSocket.sendTXT(num, "LED ON");
  94. } else {
  95. webSocket.sendTXT(num, "LED OFF");
  96. }
  97. break;
  98. }
  99. case WStype_DISCONNECTED:
  100. Serial.printf("WS Client %u getrennt\n", num);
  101. break;
  102. case WStype_TEXT: {
  103. String msg = String((char*) payload);
  104. Serial.print("WS empfangen: ");
  105. Serial.println(msg);
  106. JsonDocument doc;
  107. deserializeJson(doc, msg);
  108. const char *cmd = doc["cmd"];
  109. if (strToCommand(cmd) == CMD_SWITCH_LED) {
  110. const char *value = doc["val"];
  111. if (strcmp(value, "on") == 0) {
  112. Serial.println("LED ON");
  113. ledMngr.on();
  114. delay(20);
  115. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  116. webSocket.sendTXT(num, "LED ON");
  117. }
  118. } else if (strcmp(value, "off") == 0) {
  119. Serial.println("LED OFF");
  120. ledMngr.off();
  121. delay(20);
  122. if (digitalRead(LED_STRIP_POWER_PIN) == LOW) {
  123. webSocket.sendTXT(num, "LED OFF");
  124. }
  125. }
  126. } else if (strToCommand(cmd) == CMD_RESET_WIFI_CFG) {
  127. resetWifiConfiguration(num);
  128. } else {
  129. String s = "ACK UNKNOWN: " + msg;
  130. webSocket.sendTXT(num, s);
  131. }
  132. break;
  133. }
  134. default:
  135. break;
  136. }
  137. }
  138. void resetWifiConfiguration(uint8_t num) {
  139. // EEPROM.begin(sizeof(Config));
  140. // config.valid = false;
  141. // EEPROM.put(0, config);
  142. Config cfg = configManager.getConfig();
  143. String ssid = "";
  144. String pass = "";
  145. ssid.toCharArray(cfg.ssid, sizeof(cfg.ssid));
  146. pass.toCharArray(cfg.pass, sizeof(cfg.pass));
  147. cfg.wifiValid = false;
  148. configManager.saveConfig(cfg);
  149. webSocket.sendTXT(num, "Wifi_Reset_Success");
  150. delay(200);
  151. EEPROM.commit();
  152. ESP.restart();
  153. }
  154. void setState(AppState newState) {
  155. Serial.printf("STATE %s -> %s\n", stateToString(state), stateToString(newState));
  156. state = newState;
  157. }
  158. void handleBoot() {
  159. if (configManager.getConfig().wifiValid) {
  160. WiFi.mode(WIFI_STA);
  161. WiFi.begin(configManager.getConfig().ssid, configManager.getConfig().pass);
  162. setState(STATE_CONNECTING);
  163. }
  164. else {
  165. startAP();
  166. setState(STATE_AP_MODE);
  167. }
  168. }
  169. void handleConnecting() {
  170. if (WiFi.status() == WL_CONNECTED) {
  171. startSTAWebServer();
  172. setState(STATE_STA_MODE);
  173. return;
  174. }
  175. if (millis() - connectStart > 10000) {
  176. Serial.println("Connect Timeout");
  177. startAP();
  178. setState(STATE_AP_MODE);
  179. }
  180. }
  181. void handleAPMode() {
  182. dnsServer.processNextRequest();
  183. webServer.handleClient();
  184. }
  185. void handleSTAMode() {
  186. webServer.handleClient();
  187. webSocket.loop();
  188. if (WiFi.status() != WL_CONNECTED) {
  189. Serial.println("WLAN verloren");
  190. WiFi.begin(configManager.getConfig().ssid, configManager.getConfig().pass);
  191. connectStart = millis();
  192. setState(STATE_CONNECTING);
  193. }
  194. }
  195. void handleError() {
  196. ledMngr.off();
  197. }
  198. void startSTAWebServer() {
  199. Serial.println("Starte STA Services");
  200. // Webseite
  201. webServer.on("/", handleSTAControlPage);
  202. // optional
  203. webServer.on("/save", HTTP_POST, handleSave);
  204. webServer.begin();
  205. // WebSocket
  206. webSocket.begin();
  207. webSocket.onEvent(webSocketEvent);
  208. Serial.print("HTTP Server: http://");
  209. Serial.println(WiFi.localIP());
  210. Serial.print("WebSocket: ws://");
  211. Serial.print(WiFi.localIP());
  212. Serial.println(":81/");
  213. }
  214. // ---------- SETUP ----------
  215. void setup() {
  216. Serial.begin(115200);
  217. ledMngr.begin();
  218. configManager.loadConfig();
  219. setState(STATE_BOOT);
  220. connectStart = millis();
  221. }
  222. // ---------- LOOP ----------
  223. void loop() {
  224. switch (state) {
  225. case STATE_BOOT:
  226. handleBoot();
  227. break;
  228. case STATE_AP_MODE:
  229. handleAPMode();
  230. break;
  231. case STATE_CONNECTING:
  232. handleConnecting();
  233. break;
  234. case STATE_STA_MODE:
  235. handleSTAMode();
  236. break;
  237. case STATE_ERROR:
  238. handleError();
  239. break;
  240. }
  241. }