Skip to content

Instantly share code, notes, and snippets.

@pipoblak
Created November 11, 2016 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pipoblak/2d63e052ff74612b7429a45ef0eeab22 to your computer and use it in GitHub Desktop.
Save pipoblak/2d63e052ff74612b7429a45ef0eeab22 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <WebSocketsServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <Adafruit_NeoPixel.h>
#include <Thread.h>
#include <ThreadController.h>
#define PIN 4
#define NUM_LEDS 300
#define PIN2 0
#define NUM_LEDS2 300
#define PIN3 12
#define NUM_LEDS3 0
#define USE_SERIAL Serial
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(NUM_LEDS2, PIN2, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip3 = Adafruit_NeoPixel(NUM_LEDS3, PIN3, NEO_GRB + NEO_KHZ800);
//VARIABLES
const char* ssid = "RocketzNode";
const char* password = "node1708";
IPAddress ip(192, 168, 1, 201); // where xx is the desired IP Address
IPAddress gateway(192, 168, 1, 1); // set gateway to match your network
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your
boolean ida1 = true, ida2 = true, ida3 = true;
int i = 1;
int ID = 0;
int indexR, indexG, indexB, indexS, indexSize, indexD = 0;
int EventID1 = 0, EventID2 = 0, EventID3 = 0, DELAY1 = 0, DELAY2 = 0, DELAY3 = 0;
String command = "", commandBalance = "";
ThreadController controll = ThreadController();
Thread threadRead = Thread();
Thread threadLight = Thread();
ThreadController groupOfThreads = ThreadController();
uint16_t contJ1 = 0, contJ2 = 0, contJ3 = 0;
int Red = 0, Green = 0, Blue = 0, Red1 = 0, Green1 = 0, Blue1 = 255, Red2 = 0, Green2 = 0, Blue2 = 255, Red3 = 0, Green3 = 0, Blue3 = 0, Speed = 5;
int contTempo1 = 0, contTempo2 = 0, contTempo3 = 0;
boolean staticColor = true, staticColor2 = true, staticColor3 = true;
unsigned long previousMillis = 0;
long interval = 2000;
WebSocketsServer webSocket = WebSocketsServer(81);
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
switch (type) {
case WStype_DISCONNECTED:
USE_SERIAL.printf("[%u] Disconnected!\n", num);
break;
case WStype_CONNECTED: {
IPAddress ip = webSocket.remoteIP(num);
USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
// send message to client
webSocket.sendTXT(num, "Connected");
}
break;
case WStype_ERROR:{
break;
}
case WStype_TEXT: {
//USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);
// Serial.println(recivedDataStr);
//DETECT EVENT
if (payload[0] == '$') {
webSocket.sendTXT(num, "Coloreyeze Device PC");
}
//SETTING RGB like #R255G255B255S0
if (payload[0] == '#') {
for (int iC = 1; iC <= lenght; iC++) {
if (payload[iC] == 'R')
indexR = iC;
if (payload[iC] == 'G')
indexG = iC;
if (payload[iC] == 'B')
indexB = iC;
if (payload[iC] == 'S')
indexS = iC;
}
int idStrip = 0;
command = (char*)payload;
commandBalance = "";
char char_array[lenght];
command.toCharArray(char_array, lenght + 1);
for (int cont = indexS + 1; cont <= lenght; cont++) {
commandBalance.concat(char_array[cont]);
}
idStrip = commandBalance.toInt();
commandBalance = "";
for (int cont = indexR + 1; cont < indexG; cont++) {
commandBalance.concat(char_array[cont]);
}
Red = commandBalance.toInt();
commandBalance = "";
for (int cont = indexG + 1; cont < indexB; cont++) {
commandBalance.concat(char_array[cont]);
}
Green = commandBalance.toInt();
commandBalance = "";
for (int cont = indexB + 1; cont < indexS; cont++) {
commandBalance.concat(char_array[cont]);
}
Blue = commandBalance.toInt();
commandBalance = "";
if (idStrip == 0 ) {
Red1 = Red;
Green1 = Green;
Blue1 = Blue;
Red2 = Red;
Green2 = Green;
Blue2 = Blue;
Red3 = Red;
Green3 = Green;
Blue3 = Blue;
}
if (idStrip == 1) {
Red1 = Red;
Green1 = Green;
Blue1 = Blue;
}
else if (idStrip == 2) {
Red2 = Red;
Green2 = Green;
Blue2 = Blue;
}
else if (idStrip == 3) {
Red3 = Red;
Green3 = Green;
Blue3 = Blue;
}
*payload = 0;
}
if (payload[0] == '@') {
for (int iC = 1; iC <= lenght; iC++) {
if (payload[iC] == 'D')
indexD = iC;
if (payload[iC] == 'S')
indexS = iC;
}
int idStrip = 0;
command = (char*)payload;
commandBalance = "";
char char_array[lenght];
command.toCharArray(char_array, lenght + 1);
for (int cont = 1; cont < indexD; cont++) {
commandBalance.concat(char_array[cont]);
}
ID = commandBalance.toInt();
commandBalance = "";
for (int cont = indexD + 1; cont < indexS; cont++) {
commandBalance.concat(char_array[cont]);
}
Speed = commandBalance.toInt();
commandBalance = "";
for (int cont = indexS + 1; cont < lenght + 1; cont++) {
commandBalance.concat(char_array[cont]);
}
int idStri = commandBalance.toInt();
Serial.println(idStri);
commandBalance = "";
if (idStri == 1) {
EventID1 = ID;
contTempo1 = 0;
contJ1 = 0;
DELAY1 = Speed;
}
else if (idStri == 2) {
EventID2 = ID;
contTempo2 = 0;
contJ2 = 0;
DELAY2 = Speed;
}
else if (idStri == 3) {
EventID3 = ID;
contTempo3 = 0;
contJ3 = 0;
DELAY3 = Speed;
}
else if (idStri == 0) {
EventID1 = ID;
EventID2 = ID;
EventID3 = ID;
contTempo1 = 0;
contTempo2 = 0;
contTempo3 = 0;
contJ1 = 0;
contJ2 = 0;
contJ3 = 0;
DELAY1 = Speed;
DELAY2 = Speed;
DELAY3 = Speed;
}
}
if (payload[0] == '!') {
}
break;
default:{
break;
}
}
}
}
//CALBACKS
void lightCallback() {
//FITA 1
if (EventID1 == 0) {
rainbowCycleS(DELAY1, 1);
}
else if (EventID1 == 1) {
staticColorEffect(Red1, Green1, Blue1, 1);
}
else if (EventID1 == 2) {
FadeInOut(Red1, Green1, Blue1, 10, 1);
}
else if (EventID1 == 3) {
CylonBounce(Red1, Green1, Blue1, 5, 10, 1);
}
else if (EventID1 == 4) {
rainbowCycleSincro(0);
}
else if (EventID1 == 5) {
CylonSincro(Red1, Green1, Blue1, 5, 1);
}
//FITA 2
if (EventID2 == 0) {
rainbowCycleS(DELAY2, 2);
}
else if (EventID2 == 1) {
staticColorEffect(Red2, Green2, Blue2, 2);
}
else if (EventID2 == 2) {
FadeInOut(255, 255, 255, 10, 2);
}
else if (EventID2 == 3) {
CylonBounce(Red2, Green2, Blue2, 195, 0, 2);
}
else if (EventID2 == 4) {
}
else if (EventID2 == 5) {
}
//FITA 3
if (EventID3 == 0) {
rainbowCycleS(DELAY3, 3);
}
else if (EventID3 == 1) {
staticColorEffect(Red3, Green3, Blue3, 3);
}
else if (EventID3 == 2) {
FadeInOut(Red3, Green3, Blue3, 10, 3);
}
else if (EventID3 == 3) {
CylonBounce(Red3, Green3, Blue3, 2, 10, 3);
}
else if (EventID3 == 4) {
}
else if (EventID2 == 5) {
}
}
void webSocketRun() {
webSocket.loop();
}
void setup() {
Serial.begin(115200);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.setDebugOutput(true);
// start webSocket server
webSocket.begin();
webSocket.onEvent(webSocketEvent);
if (MDNS.begin("esp8266")) {
USE_SERIAL.println("MDNS responder started");
}
// Add service to MDNS
MDNS.addService("ws", "tcp", 81);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
strip2.begin();
strip2.show();
strip3.begin();
strip3.show();
threadRead.onRun(webSocketRun);
threadRead.setInterval(1);
threadLight.onRun(lightCallback);
threadLight.setInterval(1);
controll.add(&threadRead);
groupOfThreads.add(&threadLight);
controll.add(&groupOfThreads);
}
void loop() {
controll.run();
}
//EVENTOS DE LUz1
void showStrip(int stripID) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
if (stripID == 1) {
strip.show();
}
else if (stripID == 2) {
strip2.show();
}
else if (stripID == 3) {
strip3.show();
}
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
void setPixel(int Pixel, byte red, byte green, byte blue, int stripID) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
if (stripID == 1) {
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
}
else if (stripID == 2) {
strip2.setPixelColor(Pixel, strip2.Color(red, green, blue));
}
else if (stripID == 3) {
strip3.setPixelColor(Pixel, strip3.Color(red, green, blue));
}
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
void setAll(byte red, byte green, byte blue, int stripID) {
int numberLeds;
if (stripID == 1) {
numberLeds = NUM_LEDS;
}
else if (stripID == 2) {
numberLeds = NUM_LEDS2;
}
else if (stripID == 3) {
numberLeds = NUM_LEDS3;
}
for (int i = 0; i < numberLeds; i++ ) {
setPixel(i, red, green, blue, stripID);
}
showStrip(stripID);
}
//STATICCOLOR
void staticColorEffect(int r, int g, int b, int stripID) {
if (stripID == 1) {
if (staticColor == true)
setAll(r, g, b, stripID);
}
else if (stripID == 2) {
if (staticColor2 == true)
setAll(r, g, b, stripID);
}
else if (stripID == 3) {
if (staticColor3 == true)
setAll(r, g, b, stripID);
}
}
//RAINBOW
void rainbowCycleS(int SpeedDelay, int stripID) {
byte *c;
int contTempo;
int numberLeds;
int cont;
if (stripID == 1) {
contTempo = contTempo1;
contTempo1++;
numberLeds = NUM_LEDS;
cont = contJ1;
}
else if (stripID == 2) {
contTempo = contTempo2;
contTempo2++;
numberLeds = NUM_LEDS2;
cont = contJ2;
}
else if (stripID == 3) {
contTempo = contTempo3;
contTempo3++;
numberLeds = NUM_LEDS3;
cont = contJ3;
}
if (contTempo >= SpeedDelay) {
for (i = 0; i < numberLeds; i++) {
c = Wheel(((i * 256 / numberLeds) + cont) & 255);
setPixel(i, *c, *(c + 1), *(c + 2), stripID);
}
contTempo = 0;
if (stripID == 1) {
contTempo1 = contTempo;
contJ1++;
}
else if (stripID == 2) {
contTempo2 = contTempo;
contJ2++;
}
else if (stripID == 3) {
contTempo3 = contTempo;
contJ3++;
}
showStrip(stripID);
}
}
//RAINBOW SINCRONIZADO
void rainbowCycleSincro(int SpeedDelay) {
byte *c;
int contTempo;
int numberLeds;
int cont;
numberLeds = NUM_LEDS + NUM_LEDS2 + NUM_LEDS3;
contTempo = contTempo1;
contTempo1++;
cont = contJ1;
if (contTempo >= SpeedDelay) {
for (i = 0; i <= numberLeds; i++) {
c = Wheel(((i * 256 / numberLeds) + cont) & 255);
if (i <= NUM_LEDS) {
setPixel(i, *c, *(c + 1), *(c + 2), 1);
}
else if (i >= NUM_LEDS && i <= NUM_LEDS + NUM_LEDS2) {
setPixel(i - NUM_LEDS, *c, *(c + 1), *(c + 2), 2);
}
else if (i >= NUM_LEDS2 && i <= numberLeds) {
setPixel(NUM_LEDS3 - (i - (NUM_LEDS + NUM_LEDS2)), *c, *(c + 1), *(c + 2), 3);
}
}
contTempo = 0;
contTempo1 = contTempo;
contJ1++;
showStrip(1);
showStrip(2);
showStrip(3);
}
}
byte * Wheel(byte WheelPos) {
static byte c[3];
if (WheelPos < 85) {
c[0] = WheelPos * 3;
c[1] = 255 - WheelPos * 3;
c[2] = 0;
} else if (WheelPos < 170) {
WheelPos -= 85;
c[0] = 255 - WheelPos * 3;
c[1] = 0;
c[2] = WheelPos * 3;
} else {
WheelPos -= 170;
c[0] = 0;
c[1] = WheelPos * 3;
c[2] = 255 - WheelPos * 3;
}
return c;
}
//EFEITO PULSE
void FadeInOut(byte red, byte green, byte blue, int SpeedDelay, int stripID) {
float r, g, b;
boolean verif;
int cont, contTempo;
if (stripID == 1) {
cont = contJ1;
verif = ida1;
contTempo = contTempo1;
contTempo1++;
}
else if (stripID == 2) {
cont = contJ2;
verif = ida2;
contTempo = contTempo2;
contTempo2++;
}
else if (stripID == 3) {
cont = contJ3;
verif = ida3;
contTempo = contTempo3;
contTempo3++;
}
if (contTempo >= SpeedDelay) {
if (verif == true) {
if (cont < 256) {
r = (cont / 256.0) * red;
g = (cont / 256.0) * green;
b = (cont / 256.0) * blue;
setAll(r, g, b, stripID);
if (cont == 255) {
verif = false;
}
else {
cont = cont + 15;
}
}
}
else {
r = (cont / 256.0) * red;
g = (cont / 256.0) * green;
b = (cont / 256.0) * blue;
setAll(r, g, b, stripID);
if (cont == 0) {
verif = true;
}
else {
cont = cont - 15;
}
}
contTempo = 0;
if (stripID == 1) {
contTempo1 = contTempo;
contJ1 = cont;
ida1 = verif;
}
else if (stripID == 2) {
contTempo2 = contTempo;
contJ2 = cont;
ida2 = verif;
}
else if (stripID == 3) {
contTempo3 = contTempo;
contJ2 = cont;
ida3 = verif;
}
}
}
//TESTANDO
void contador(int stripID) {
int cont, contTempo;
if (stripID == 1) {
cont = contJ1;
contTempo = contTempo1;
contTempo1++;
}
else if (stripID == 2) {
cont = contJ2;
contTempo = contTempo2;
contTempo2++;
}
if (contTempo >= 24000) {
Serial.println(contTempo);
contTempo = 0;
if (stripID == 1) {
contTempo1 = contTempo;
contJ1 = cont;
}
else if (stripID == 2) {
contTempo2 = contTempo;
contJ2 = cont;
}
}
}
//CYLON
void CylonBounce(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int stripID) {
float r, g, b;
boolean verif;
int cont, contTempo;
int numberLeds;
if (stripID == 1) {
cont = contJ1;
verif = ida1;
contTempo = contTempo1;
contTempo++;
numberLeds = NUM_LEDS;
}
else if (stripID == 2) {
cont = contJ2;
verif = ida2;
contTempo = contTempo2;
contTempo++;
numberLeds = NUM_LEDS2;
}
else if (stripID == 3) {
cont = contJ3;
verif = ida3;
contTempo = contTempo3;
contTempo++;
numberLeds = NUM_LEDS3;
}
if (contTempo >= SpeedDelay) {
if (verif == true) {
if (cont <= numberLeds) {
setPixel(cont - 1, 0, 0, 0, stripID);
setPixel(cont, red / 10, green / 10, blue / 10, stripID);
for (int j = 1; j <= EyeSize; j++) {
setPixel(cont + j, red, green, blue, 1);
}
setPixel(cont + EyeSize, red / 10, green / 10, blue / 10, stripID);
showStrip(stripID);
}
cont++;
if (cont == numberLeds) {
verif = false;
}
}
else if (verif == false) {
if (cont <= numberLeds) {
setPixel(cont, red / 10, green / 10, blue / 10, stripID);
for (int j = 1; j <= EyeSize; j++) {
setPixel(cont + j, red, green, blue, stripID);
}
setPixel(cont + EyeSize, 0, 0, 0, stripID);
showStrip(stripID);
}
cont--;
if (cont == 0) {
verif = true;
}
}
contTempo = 0;
}
if (stripID == 1) {
contTempo1 = contTempo;
contJ1 = cont;
ida1 = verif;
}
else if (stripID == 2) {
contTempo2 = contTempo;
contJ2 = cont;
ida2 = verif;
}
else if (stripID == 3) {
contTempo3 = contTempo;
contJ3 = cont;
ida3 = verif;
}
}
//CYLON SINCRONIZADO
void CylonSincro(byte red, byte green, byte blue, int EyeSize, int SpeedDelay) {
float r, g, b;
boolean verif;
int cont, contTempo;
int numberLeds;
cont = contJ1;
verif = ida1;
contTempo = contTempo1;
contTempo++;
numberLeds = NUM_LEDS + NUM_LEDS2 + NUM_LEDS3;
if (contTempo >= SpeedDelay) {
if (verif == true) {
if (cont <= NUM_LEDS) {
setPixel(cont - 1, 0, 0, 0, 1);
setPixel(cont, red / 10, green / 10, blue / 10, 1);
for (int j = 1; j <= EyeSize; j++) {
setPixel(cont + j, red, green, blue, 1);
}
setPixel(cont + EyeSize + 1, red / 10, green / 10, blue / 10, 1);
showStrip(1);
}
if (cont > NUM_LEDS && cont <= NUM_LEDS + NUM_LEDS2) {
setPixel((cont - 1) - NUM_LEDS, 0, 0, 0, 2);
setPixel(cont - NUM_LEDS, red / 10, green / 10, blue / 10, 2);
for (int j = 1; j <= EyeSize; j++) {
setPixel((cont + j) - NUM_LEDS, red, green, blue, 2);
}
setPixel((cont + EyeSize + 1) - NUM_LEDS, red / 10, green / 10, blue / 10, 2);
showStrip(2);
}
if (cont > NUM_LEDS2 && cont <= numberLeds) {
setPixel(NUM_LEDS3 - ((cont - 1) - (NUM_LEDS + NUM_LEDS2)), 0, 0, 0, 3);
setPixel(NUM_LEDS3 - (cont - NUM_LEDS - NUM_LEDS2), red / 10, green / 10, blue / 10, 3);
for (int j = 1; j <= EyeSize; j++) {
setPixel(NUM_LEDS3 - ((cont + j) - NUM_LEDS - NUM_LEDS2), red, green, blue, 3);
}
setPixel(NUM_LEDS3 - ((cont + EyeSize + 1) - NUM_LEDS - NUM_LEDS2), red / 10, green / 10, blue / 10, 3);
showStrip(3);
}
cont++;
if (cont == numberLeds - (EyeSize + 2)) {
verif = false;
}
}
if (verif == false) {
if (cont <= NUM_LEDS) {
setPixel(cont, red / 10, green / 10, blue / 10, 1);
for (int j = 1; j <= EyeSize; j++) {
setPixel(cont + j, red, green, blue, 1);
}
setPixel(cont + EyeSize + 1, 0, 0, 0, 1);
showStrip(1);
}
if (cont > NUM_LEDS && cont <= NUM_LEDS + NUM_LEDS2) {
setPixel(cont - NUM_LEDS, red / 10, green / 10, blue / 10, 2);
for (int j = 1; j <= EyeSize; j++) {
setPixel((cont + j) - NUM_LEDS, red, green, blue, 2);
}
setPixel((cont + EyeSize + 1) - NUM_LEDS, 0, 0, 0, 2);
showStrip(2);
}
if (cont > NUM_LEDS2 && cont <= numberLeds) {
setPixel(NUM_LEDS3 - (cont - NUM_LEDS - NUM_LEDS2), red / 10, green / 10, blue / 10, 3);
for (int j = 1; j <= EyeSize; j++) {
setPixel(NUM_LEDS3 - ((cont + j) - NUM_LEDS - NUM_LEDS2), red, green, blue, 3);
}
setPixel(NUM_LEDS3 - ((cont + EyeSize + 1) - NUM_LEDS - NUM_LEDS2), 0, 0, 0, 3);
showStrip(3);
}
cont--;
if (cont == 0) {
verif = true;
}
}
contTempo = 0;
contTempo1 = contTempo;
contJ1 = cont;
ida1 = verif;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment