#if defined(ESP8266) #include #else #include #endif #include //Local WebServer used to serve the configuration portal #include //https://github.com/tzapu/WiFiManager WiFi Configuration Magic #include #include "AsyncJson.h" #include "ArduinoJson.h" // LED count and control pin #define NUM_LEDS 8 #define DATA_PIN D3 // LED array CRGB leds[NUM_LEDS]; // Web server AsyncWebServer server(80); DNSServer dns; bool blink = false; CRGB singleColor = CRGB::Red; int brightness_size = 6; int brightness_index = 5; int brightness[7] = {20,60,120,160,200,255}; int ledSpeed_size = 6; int ledSpeed_index = 2; int ledSpeed[] = {10,20,50,100}; bool power = true; int mode = 0; void setup() { // Initialize serial Serial.begin(9600); while (!Serial) ; Serial.println("Serial initialized"); // Initialize onboard LED pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); Serial.println("Built in LED initialized"); // Initialize Wifi, start AP if no saved Wifi in range AsyncWiFiManager wifiManager(&server,&dns); wifiManager.autoConnect("WEMOS D1 mini pro"); Serial.println("WifiManager initialized"); // Initialize LEDs FastLED.addLeds(leds, NUM_LEDS); for(int i=0;isend(200, "text/html", getWebPage()); }); server.onNotFound(notFound); Serial.println("Webserver request handlers created"); server.onRequestBody([](AsyncWebServerRequest * request, uint8_t *data, size_t len, size_t index, size_t total) { Serial.println("Running"); if(request->url() == "/power"){ // Set mode to 0(single color) if(power){ power = false; mode = 0; FastLED.clear(true); } power = false; mode = 0; request->send(200, "text/plain", "done"); } if(request->url() == "/reset"){ ESP.restart(); request->send(200, "text/plain", "done"); } if(request->url() == "/brightness"){ Serial.println(getValue((const char*) data)); setBrightness(getValue((const char*) data).toInt()); request->send(200, "text/plain", "done"); } if(request->url() == "/speed"){ Serial.println(getValue((const char*) data)); request->send(200, "text/plain", "done"); } if(request->url() == "/color"){ String value = getValue((const char*) data).substring(1); Serial.println(value); long color = strtol(value.c_str(), NULL, 16); Serial.println(value.c_str()); Serial.println(color); setColor(color); request->send(200, "text/plain", "done"); } if(request->url() == "/mode"){ Serial.println(getValue((const char*) data)); request->send(200, "text/plain", "done"); } }); // Start webserver server.begin(); Serial.println("Webserver started"); } String getValue(const char* data){ DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.parseObject((const char*)data); if (root.success()) { if (root.containsKey("value")) { return root["value"].asString(); } } } void setColor(CRGB color){ for(int i=0;i 0){ brightness_index--; applyBrightness(); } } } void applyBrightness(){ FastLED.setBrightness(brightness[brightness_index]); FastLED.show(); } void loop() { } void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); blink = true; } String htmlSiteHead = " LED Control "; String htmlSiteBody = " "; String htmlSiteJs1 = ""; String getWebPage(){ return htmlSiteHead+htmlSiteBody+htmlSiteJs1+WiFi.localIP().toString()+htmlSiteJs2; }