212 lines
7.9 KiB
C++
212 lines
7.9 KiB
C++
#include <ESP8266WiFi.h> /* Basic ESP WiFi support */
|
|
#include <DNSServer.h> /* Needed by other libraries */
|
|
#include <ESP8266WebServer.h> /* WebServer for config and RET api */
|
|
#include <ArduinoJson.h> /* JSON handling for REST api */
|
|
#include <WiFiManager.h> /* Runtime WiFi Configuration */
|
|
#include <Adafruit_NeoPixel.h> /* 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<JsonObject>();
|
|
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<strip.numPixels(); i++) { // For each pixel in strip...
|
|
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
|
|
strip.show(); // Update strip to match
|
|
delay(wait); // Pause for a moment
|
|
}
|
|
}
|
|
|
|
// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
|
|
// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
|
|
// between frames.
|
|
void theaterChase(uint32_t color, int wait) {
|
|
for(int a=0; a<10; a++) { // Repeat 10 times...
|
|
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
|
|
strip.clear(); // Set all pixels in RAM to 0 (off)
|
|
// 'c' counts up from 'b' to end of strip in steps of 3...
|
|
for(int c=b; c<strip.numPixels(); c += 3) {
|
|
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
|
|
}
|
|
strip.show(); // Update strip with new contents
|
|
delay(wait); // Pause for a moment
|
|
}
|
|
}
|
|
}
|
|
|
|
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
|
|
void rainbow(int wait) {
|
|
// Hue of first pixel runs 5 complete loops through the color wheel.
|
|
// Color wheel has a range of 65536 but it's OK if we roll over, so
|
|
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
|
|
// means we'll make 5*65536/256 = 1280 passes through this outer loop:
|
|
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
|
|
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
|
|
// Offset pixel hue by an amount to make one full revolution of the
|
|
// color wheel (range of 65536) along the length of the strip
|
|
// (strip.numPixels() steps):
|
|
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
|
|
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
|
|
// optionally add saturation and value (brightness) (each 0 to 255).
|
|
// Here we're using just the single-argument hue variant. The result
|
|
// is passed through strip.gamma32() to provide 'truer' colors
|
|
// before assigning to each pixel:
|
|
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
|
|
}
|
|
strip.show(); // Update strip with new contents
|
|
delay(wait); // Pause for a moment
|
|
}
|
|
}
|
|
|
|
// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
|
|
void theaterChaseRainbow(int wait) {
|
|
int firstPixelHue = 0; // First pixel starts at red (hue 0)
|
|
for(int a=0; a<30; a++) { // Repeat 30 times...
|
|
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
|
|
strip.clear(); // Set all pixels in RAM to 0 (off)
|
|
// 'c' counts up from 'b' to end of strip in increments of 3...
|
|
for(int c=b; c<strip.numPixels(); c += 3) {
|
|
// hue of pixel 'c' is offset by an amount to make one full
|
|
// revolution of the color wheel (range 65536) along the length
|
|
// of the strip (strip.numPixels() steps):
|
|
int hue = firstPixelHue + c * 65536L / strip.numPixels();
|
|
uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> 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
|
|
}
|
|
}
|
|
}
|