#include /* Basic ESP WiFi support */ #include /* Needed by other libraries */ #include /* WebServer for config and RET api */ #include /* JSON handling for REST api */ #include /* Runtime WiFi Configuration */ #include /* Comfortable control for WS2812 */ // Pin the LED strip is connected to #define LED_PIN D4 /* D4 is not the same as just 4 */ // Number of LEDs in the strip #define LED_COUNT 150 // Allocate a runtime object for the LED strip Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); // Port for the REST api to control the LEDs #define HTTP_REST_PORT 80 // Webserver handling the REST api ESP8266WebServer http_rest_server(HTTP_REST_PORT); void get_leds() { StaticJsonDocument<200> jsonDocument; JsonObject jsonObj = jsonDocument.as(); char JSONmessageBuffer[200]; // if (led_resource.id == 0) // http_rest_server.send(204); // else { jsonObj["id"] = 1; // jsonObj["gpio"] = led_resource.gpio; // jsonObj["status"] = led_resource.status; serializeJsonPretty(jsonDocument, JSONmessageBuffer); //jsonObj.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer)); http_rest_server.send(200, "application/json", JSONmessageBuffer); // } } void config_rest_server_routing() { http_rest_server.on("/", HTTP_GET, []() { http_rest_server.send(200, "text/html", "Welcome to the ESP8266 REST Web Server"); }); http_rest_server.on("/leds", HTTP_GET, get_leds); //http_rest_server.on("/leds", HTTP_POST, post_put_leds); //http_rest_server.on("/leds", HTTP_PUT, post_put_leds); } void setup() { // Enable serial port for debugging Serial.begin(115200); Serial.println("Booting ..."); // Initialize the LED strip as soon as possible Serial.print("Initializing NeoPixel strip ... "); strip.begin(); /* Initialize NeoPixel strip object */ strip.show(); /* Turn off all pixels asap */ /* Set brightness to something manageable, * to reduce current draw and don't burn * the eyes of bystanders */ strip.setBrightness(30); Serial.println("done"); // Take care of the WiFi connection // Local intialization of the manager, not needed at runtime anymore Serial.println("Initializing WiFiManager"); WiFiManager wifiManager; //reset saved settings //wifiManager.resetSettings(); // Try to connect with existing credentials // If that doesn't work fall back to config mode // If nothing is configured until the timeout, fall back to AP mode wifiManager.autoConnect(); //if you get here you have connected to the WiFi Serial.println("WiFi connected"); Serial.print("Starting WebServer for REST api ... "); config_rest_server_routing(); http_rest_server.begin(); Serial.println("done"); } void loop() { http_rest_server.handleClient(); // Fill along the length of the strip in various colors... Serial.println("Red ColorWipe"); colorWipe(strip.Color(255, 0, 0), 50); // Red http_rest_server.handleClient(); Serial.println("Green ColorWipe"); colorWipe(strip.Color( 0, 255, 0), 50); // Green http_rest_server.handleClient(); Serial.println("Blue ColorWipe"); colorWipe(strip.Color( 0, 0, 255), 50); // Blue // Do a theater marquee effect in various colors... http_rest_server.handleClient(); Serial.println("White TheaterChase"); theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness http_rest_server.handleClient(); Serial.println("Red TheaterChase"); theaterChase(strip.Color(127, 0, 0), 50); // Red, half brightness http_rest_server.handleClient(); Serial.println("Blue TheaterChase"); theaterChase(strip.Color( 0, 0, 127), 50); // Blue, half brightness http_rest_server.handleClient(); Serial.println("Rainbow"); rainbow(10); // Flowing rainbow cycle along the whole strip http_rest_server.handleClient(); Serial.println("Rainbow Chase"); theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant } // Some functions of our own for creating animated effects ----------------- // Fill strip pixels one after another with a color. Strip is NOT cleared // first; anything there will be covered pixel by pixel. Pass in color // (as a single 'packed' 32-bit value, which you can get by calling // strip.Color(red, green, blue) as shown in the loop() function above), // and a delay time (in milliseconds) between pixels. void colorWipe(uint32_t color, int wait) { for(int i=0; i RGB strip.setPixelColor(c, color); // Set pixel 'c' to value 'color' } strip.show(); // Update strip with new contents delay(wait); // Pause for a moment firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames } } }