327 lines
8.3 KiB
C++
327 lines
8.3 KiB
C++
#include <Adafruit_NeoPixel.h>
|
|
#ifdef __AVR__
|
|
#include <avr/power.h>
|
|
#endif
|
|
|
|
#define PIN D6
|
|
#define NUMPIXELS 28
|
|
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
|
|
|
|
#include <ESP8266WiFi.h>
|
|
#include <DNSServer.h>
|
|
#include <ESP8266WebServer.h>
|
|
|
|
const byte DNS_PORT = 53;
|
|
IPAddress apIP(192, 168, 1, 1);
|
|
DNSServer dnsServer;
|
|
ESP8266WebServer webServer(80);
|
|
unsigned long previousMillis = 0;
|
|
unsigned long interval = 0;
|
|
int mode = 0;
|
|
int element = 0;
|
|
|
|
unsigned int colorval1 = 0;
|
|
unsigned int colorval2 = 0;
|
|
unsigned int colorval3 = 0;
|
|
|
|
unsigned int primary_r = 0;
|
|
unsigned int primary_g = 0;
|
|
unsigned int primary_b = 0;
|
|
|
|
unsigned int second_r = 0;
|
|
unsigned int second_g = 0;
|
|
unsigned int second_b = 0;
|
|
|
|
String responseHTML = ""
|
|
"<!DOCTYPE html><html><head><title>LedControl</title></head><body>"
|
|
"<h1>Control</h1><br> "
|
|
"<a style='font-size:100px;' href='http://192.168.1.1/on'>on</a><br><br><a style='font-size:100px;'href='http://192.168.1.1/off'>off</a>"
|
|
"<br><a style='font-size:100px;' href='http://192.168.1.1/1'>run</a></body><br><a style='font-size:100px;' href='http://192.168.1.1/3'>run single</a><br><a style='font-size:100px;' href='http://192.168.1.1/2'>freaky</a></body></html>"
|
|
"<br><a style='font-size:100px;' href='http://192.168.1.1/show?num=0'>0</a><br>"
|
|
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=1'>Run (once)</a><br>"
|
|
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=2'>Freaky breathe</a><br>"
|
|
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=3'>Run single</a><br>"
|
|
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=4'>Mode 4</a><br>"
|
|
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=5'>5</a><br>";
|
|
|
|
|
|
void handleOn() {
|
|
mode = 0;
|
|
for (int i = 0; i < NUMPIXELS; i++) {
|
|
strip.setPixelColor(i, strip.Color(200, 50, 0)); // Moderately bright green color.
|
|
}
|
|
strip.show();
|
|
webServer.send(200, "text/html", responseHTML);
|
|
}
|
|
|
|
void handle1() {
|
|
mode = 1;
|
|
Serial.println("LOL");
|
|
webServer.send(200, "text/html", responseHTML);
|
|
}
|
|
|
|
void handle2() {
|
|
mode = 2;
|
|
Serial.println("LOL2");
|
|
webServer.send(200, "text/html", responseHTML);
|
|
}
|
|
void handle3() {
|
|
mode = 3;
|
|
Serial.println("LOL3");
|
|
webServer.send(200, "text/html", responseHTML);
|
|
}
|
|
|
|
|
|
|
|
void handleOff() {
|
|
mode = 0;
|
|
for (int i = 0; i < NUMPIXELS; i++) {
|
|
strip.setPixelColor(i, strip.Color(0, 0, 0)); // off
|
|
}
|
|
strip.show();
|
|
webServer.send(200, "text/html", responseHTML);
|
|
}
|
|
void handleShow() {
|
|
for ( uint8_t i = 0; i < webServer.args(); i++ ) {
|
|
if (webServer.argName( i ) == "num") {
|
|
char buf[3];
|
|
webServer.arg( i ).toCharArray(buf, 3);
|
|
int num = atoi(buf);
|
|
switch (num) {
|
|
case 0:
|
|
mode = 0;
|
|
Serial.println("leds should be off here");
|
|
break;
|
|
case 1:
|
|
mode = 1;
|
|
Serial.println("mode 1 run");
|
|
break;
|
|
case 2:
|
|
mode = 2;
|
|
Serial.println("mode 2 runSingle");
|
|
break;
|
|
case 3:
|
|
mode = 3;
|
|
Serial.println("mode 3 freaky breath");
|
|
break;
|
|
case 4:
|
|
mode = 4;
|
|
Serial.println("mode 4");
|
|
break;
|
|
}
|
|
Serial.println(num);
|
|
}
|
|
}
|
|
webServer.send(200, "text/html", responseHTML);
|
|
}
|
|
void setup() {
|
|
strip.begin();
|
|
Serial.begin(115200);
|
|
Serial.println("Ready");
|
|
|
|
WiFi.mode(WIFI_AP);
|
|
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
|
|
WiFi.softAP("Scary?");
|
|
|
|
// if DNSServer is started with "*" for domain name, it will reply with
|
|
// provided IP to all DNS request
|
|
dnsServer.start(DNS_PORT, "*", apIP);
|
|
|
|
webServer.on("/on", handleOn);
|
|
webServer.on("/off", handleOff);
|
|
|
|
webServer.on("/show", handleShow);
|
|
|
|
|
|
webServer.onNotFound([]() {
|
|
webServer.send(200, "text/html", responseHTML);
|
|
});
|
|
|
|
webServer.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
dnsServer.processNextRequest();
|
|
webServer.handleClient();
|
|
|
|
if (millis() - previousMillis >= interval) {
|
|
// save the last time you blinked the LED
|
|
previousMillis = millis();
|
|
|
|
switch (mode) {
|
|
|
|
case 1:
|
|
interval = 100;
|
|
if (element < NUMPIXELS) {
|
|
element++;
|
|
|
|
}
|
|
else {
|
|
element = 0;
|
|
colorval1 = ((colorval1 == 50) ? 0 : 50);
|
|
}
|
|
|
|
strip.setPixelColor(element, strip.Color(255, colorval1, 0)); // Moderately bright green color.
|
|
strip.show();
|
|
break;
|
|
case 2:
|
|
interval = 10;
|
|
|
|
if (colorval2 == 0)colorval1--;
|
|
else colorval1++;
|
|
|
|
if (colorval1 > 150) colorval2 = 0;
|
|
if (colorval1 < 30) colorval2 = 1;
|
|
for (int i = 0; i < NUMPIXELS; i++) {
|
|
strip.setPixelColor(i, strip.Color(colorval1, colorval1, colorval1));
|
|
|
|
}
|
|
strip.show();
|
|
break;
|
|
case 3:
|
|
|
|
interval = 100;
|
|
if (element < NUMPIXELS)
|
|
element++;
|
|
else
|
|
element = 0;
|
|
for (int i = 0; i < NUMPIXELS; i++) {
|
|
strip.setPixelColor(i, strip.Color(((i == element) ? 100 : 200), ((i == element) ? 100 : 50), ((i == element) ? 100 : 0))); // Moderately bright green color.
|
|
}
|
|
strip.show();
|
|
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
previousMillis = millis();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void startShow(int i) {
|
|
mode = 0;
|
|
switch (i) {
|
|
case 0: colorWipe(strip.Color(0, 0, 0), 50); // Black/off
|
|
break;
|
|
case 1: colorWipe(strip.Color(255, 0, 0), 50); // Red
|
|
break;
|
|
case 2: colorWipe(strip.Color(0, 255, 0), 50); // Green
|
|
break;
|
|
case 3: colorWipe(strip.Color(0, 0, 255), 50); // Blue
|
|
break;
|
|
case 4: theaterChase(strip.Color(127, 127, 127), 50); // White
|
|
break;
|
|
case 5: theaterChase(strip.Color(127, 0, 0), 50); // Red
|
|
break;
|
|
case 6: theaterChase(strip.Color( 0, 0, 127), 50); // Blue
|
|
break;
|
|
case 7: rainbow(20);
|
|
break;
|
|
case 8: rainbowCycle(20);
|
|
break;
|
|
case 9: theaterChaseRainbow(50);
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
//####### Neopixeltricks
|
|
|
|
|
|
|
|
// Fill the dots one after the other with a color
|
|
void colorWipe(uint32_t c, uint8_t wait) {
|
|
for (uint16_t i = 0; i < strip.numPixels(); i++) {
|
|
strip.setPixelColor(i, c);
|
|
strip.show();
|
|
delay(wait);
|
|
}
|
|
}
|
|
|
|
void rainbow(uint8_t wait) {
|
|
uint16_t i, j;
|
|
|
|
for (j = 0; j < 256; j++) {
|
|
for (i = 0; i < strip.numPixels(); i++) {
|
|
strip.setPixelColor(i, Wheel((i + j) & 255));
|
|
}
|
|
strip.show();
|
|
delay(wait);
|
|
}
|
|
}
|
|
|
|
// Slightly different, this makes the rainbow equally distributed throughout
|
|
void rainbowCycle(uint8_t wait) {
|
|
uint16_t i, j;
|
|
|
|
for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
|
|
for (i = 0; i < strip.numPixels(); i++) {
|
|
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
|
|
}
|
|
strip.show();
|
|
delay(wait);
|
|
}
|
|
}
|
|
|
|
//Theatre-style crawling lights.
|
|
void theaterChase(uint32_t c, uint8_t wait) {
|
|
for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
|
|
for (int q = 0; q < 3; q++) {
|
|
for (int i = 0; i < strip.numPixels(); i = i + 3) {
|
|
strip.setPixelColor(i + q, c); //turn every third pixel on
|
|
}
|
|
strip.show();
|
|
|
|
delay(wait);
|
|
|
|
for (int i = 0; i < strip.numPixels(); i = i + 3) {
|
|
strip.setPixelColor(i + q, 0); //turn every third pixel off
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//Theatre-style crawling lights with rainbow effect
|
|
void theaterChaseRainbow(uint8_t wait) {
|
|
for (int j = 0; j < 256; j++) { // cycle all 256 colors in the wheel
|
|
for (int q = 0; q < 3; q++) {
|
|
for (int i = 0; i < strip.numPixels(); i = i + 3) {
|
|
strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
|
|
}
|
|
strip.show();
|
|
|
|
delay(wait);
|
|
|
|
for (int i = 0; i < strip.numPixels(); i = i + 3) {
|
|
strip.setPixelColor(i + q, 0); //turn every third pixel off
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Input a value 0 to 255 to get a color value.
|
|
// The colours are a transition r - g - b - back to r.
|
|
uint32_t Wheel(byte WheelPos) {
|
|
WheelPos = 255 - WheelPos;
|
|
if (WheelPos < 85) {
|
|
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
|
|
}
|
|
if (WheelPos < 170) {
|
|
WheelPos -= 85;
|
|
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
|
|
}
|
|
WheelPos -= 170;
|
|
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
|
|
}
|