Wemos D1 Mini Frimware zur Steuerung einer RGBW-LED-Lampe
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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