Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WebSocketsClient.h>
- #include <FastLED.h>
- const char* ssid = "SSID";
- const char* password = "PASSWORD";
- const char* twitch_oauth_token = "oauth:XXX";
- const char* twitch_nick = "XXX";
- const char* twitch_channel = "#XXX";
- WebSocketsClient webSocket;
- String payloadString;
- String lastPayload = "";
- uint8_t gHue = 0; // rotating "base color" used by many of the patterns
- #define NUM_LEDS 50
- #define ANALOG_PIN A0
- #define LED_PIN 5
- #define BUTTON_PIN 4
- #define LED_TYPE WS2811
- #define COLOR_ORDER GRB
- #define COOLING 55
- #define SPARKING 120
- #define FRAMES_PER_SECOND 60
- int buttonState;
- int lastButtonState = LOW;
- int BRIGHTNESS;
- unsigned long lastDebounceTime = 0;
- unsigned long debounceDelay = 50;
- uint8_t r = 255;
- uint8_t g = 255;
- uint8_t b = 255;
- // Declare the LED array
- CRGB leds[NUM_LEDS];
- void HsvToRgb(double hue, double saturation, double value, uint8_t& red, uint8_t& green, uint8_t& blue)
- {
- double rR, gG, bB;
- auto i = static_cast<int>(hue * 6);
- auto f = hue * 6 - i;
- auto p = value * (1 - saturation);
- auto q = value * (1 - f * saturation);
- auto t = value * (1 - (1 - f) * saturation);
- switch (i % 6)
- {
- case 0: rR = value , gG = t , bB = p;
- break;
- case 1: rR = q , gG = value , bB = p;
- break;
- case 2: rR = p , gG = value , bB = t;
- break;
- case 3: rR = p , gG = q , bB = value;
- break;
- case 4: rR = t , gG = p , bB = value;
- break;
- case 5: rR = value , gG = p , bB = q;
- break;
- }
- red = static_cast<uint8_t>(rR * 255);
- green = static_cast<uint8_t>(gG * 255);
- blue = static_cast<uint8_t>(bB * 255);
- }
- void webSocketEvent(WStype_t type, uint8_t* payload, size_t length) {
- String payload_str = String((char*)payload);
- switch (type) {
- case WStype_CONNECTED: {
- Serial.printf("[WSc] Connected to: %s\n", payload_str.c_str());
- String passmsg_str = "PASS " + String(twitch_oauth_token);
- String nickmsg_str = "NICK " + String(twitch_nick);
- String joinmsg_str = "JOIN " + String(twitch_channel);
- webSocket.sendTXT(passmsg_str);
- webSocket.sendTXT(nickmsg_str);
- webSocket.sendTXT(joinmsg_str);
- break;
- }
- case WStype_TEXT: {
- Serial.printf("> %s\n", payload_str.c_str());
- if (payload_str.startsWith("PING ")) {
- String pong = payload_str;
- pong.replace("PING", "PONG");
- webSocket.sendTXT(pong);
- Serial.println("[WSc] Replied to PING");
- return;
- }
- // Search for the beginning on the JSON-encoded message (":!")
- int quote_start = payload_str.indexOf("$$");
- // If the message is addressed to the chat bot
- if(quote_start > 0) {
- int quote_end = payload_str.length();
- //Serial.println(quote_start);
- //Serial.println(payload_str);
- //Serial.println(quote_end);
- //Serial.print(payload);
- String pixel_str = payload_str.substring(quote_start+2, quote_end);
- pixel_str.trim();
- pixel_str.toUpperCase();
- payloadString = pixel_str;
- Serial.println("Command String: ");
- Serial.println(pixel_str);
- if (payloadString.equalsIgnoreCase("RANDOM")){
- double h = random(0, 255) / 255.0;
- double s = 1.0;
- double l = 1.0;
- HsvToRgb(h, s, l, r, g, b);
- fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
- FastLED.show();
- }
- }
- break;
- }
- case WStype_DISCONNECTED: {
- Serial.println("[WSc] Disconnected!");
- break;
- }
- default: {
- break;
- }
- }
- }
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- pinMode(BUTTON_PIN, INPUT);
- while(WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println();
- Serial.print("IP Address: "); Serial.println(WiFi.localIP());
- Serial.println(" connected!");
- // Server address, port, and URL
- //webSocket.setSSLClientCertKey(nullptr, nullptr, nullptr);
- Serial.println("Beginning WebSocket connection...");
- webSocket.begin("irc-ws.chat.twitch.tv", 80, "/");
- webSocket.enableHeartbeat(15000, 3000, 2); // ping interval, timeout, max lost pings
- webSocket.onEvent(webSocketEvent);
- webSocket.setReconnectInterval(5000);
- Serial.println("WebSocket begin() called");
- //// Initialize the FastLED object
- FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.clear(true);
- }
- // This function draws rainbows with an ever-changing,
- // widely-varying set of parameters.
- void pride()
- {
- static uint16_t sPseudotime = 0;
- static uint16_t sLastMillis = 0;
- static uint16_t sHue16 = 0;
- uint8_t sat8 = beatsin88( 87, 220, 250);
- uint8_t brightdepth = beatsin88( 341, 96, 224);
- uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
- uint8_t msmultiplier = beatsin88(147, 23, 60);
- uint16_t hue16 = sHue16;//gHue * 256;
- uint16_t hueinc16 = beatsin88(113, 1, 3000);
- uint16_t ms = millis();
- uint16_t deltams = ms - sLastMillis ;
- sLastMillis = ms;
- sPseudotime += deltams * msmultiplier;
- sHue16 += deltams * beatsin88( 400, 5,9);
- uint16_t brightnesstheta16 = sPseudotime;
- for( uint16_t i = 0 ; i < NUM_LEDS; i++) {
- hue16 += hueinc16;
- uint8_t hue8 = hue16 / 256;
- brightnesstheta16 += brightnessthetainc16;
- uint16_t b16 = sin16( brightnesstheta16 ) + 32768;
- uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
- uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
- bri8 += (255 - brightdepth);
- CRGB newcolor = CHSV( hue8, sat8, bri8);
- uint16_t pixelnumber = i;
- pixelnumber = (NUM_LEDS-1) - pixelnumber;
- nblend( leds[pixelnumber], newcolor, 64);
- }
- }
- void Fire2012()
- {
- // Array of temperature readings at each simulation cell
- static uint8_t heat[NUM_LEDS];
- // Step 1. Cool down every cell a little
- for( int i = 0; i < NUM_LEDS; i++) {
- heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
- }
- // Step 2. Heat from each cell drifts 'up' and diffuses a little
- for( int k= NUM_LEDS - 1; k >= 2; k--) {
- heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
- }
- // Step 3. Randomly ignite new 'sparks' of heat near the bottom
- if( random8() < SPARKING ) {
- int y = random8(7);
- heat[y] = qadd8( heat[y], random8(160,255) );
- }
- // Step 4. Map from heat cells to LED colors
- for( int j = 0; j < NUM_LEDS; j++) {
- CRGB color = HeatColor( heat[j]);
- int pixelnumber;
- pixelnumber = j;
- leds[pixelnumber] = color;
- }
- }
- void juggle() {
- // eight colored dots, weaving in and out of sync with each other
- fadeToBlackBy( leds, NUM_LEDS, 20);
- uint8_t dothue = 0;
- for( int i = 0; i < 8; i++) {
- leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
- dothue += 32;
- }
- }
- void rainbow2()
- {
- // FastLED's built-in rainbow generator
- fill_rainbow( leds, NUM_LEDS, gHue, 7);
- }
- void confetti()
- {
- // random colored speckles that blink in and fade smoothly
- fadeToBlackBy( leds, NUM_LEDS, 10);
- int pos = random16(NUM_LEDS);
- leds[pos] += CHSV( gHue + random8(64), 200, 255);
- }
- unsigned long lastAnalogRead = 0;
- const int analogInterval = 100; // milliseconds
- void loop() {
- webSocket.loop();
- //BRIGHTNESS = map(analogRead(ANALOG_PIN), 2, 1024, 0, 255);
- unsigned long now = millis();
- if (now - lastAnalogRead > analogInterval) {
- lastAnalogRead = now;
- int raw = analogRead(ANALOG_PIN);
- BRIGHTNESS = map(raw, 2, 1024, 0, 255);
- }
- FastLED.setBrightness(BRIGHTNESS);
- FastLED.show();
- int reading = digitalRead(BUTTON_PIN);
- if (reading != lastButtonState) {
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- if (reading != buttonState) {
- buttonState = reading;
- if (buttonState == HIGH) {
- Serial.println("Pressed");
- }
- }
- }
- lastButtonState = reading;
- yield(); // or: delay(1);
- //if (payloadString.equalsIgnoreCase("RAINBOW")){
- // pride();
- // FastLED.show();
- // lastPayload = "RAINBOW";
- //}
- //else if (payloadString.equalsIgnoreCase("FIRE")){
- // Fire2012();
- // FastLED.show();
- // //FastLED.delay(1000 / FRAMES_PER_SECOND);
- // delay(50);
- // lastPayload = "FIRE";
- //}
- //else if (payloadString.equalsIgnoreCase("RANDOM")) {
- // lastPayload = "RANDOM";
- //}
- //else
- if (payloadString.equalsIgnoreCase("BLINK")){
- fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
- FastLED.show();
- delay(750);
- FastLED.clear();
- FastLED.show();
- delay(750);
- lastPayload = "BLINK";
- }
- //else if (payloadString.equalsIgnoreCase("BLINKFAST")){
- // fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
- // FastLED.show();
- // delay(75);
- // FastLED.clear();
- // FastLED.show();
- // delay(75);
- // lastPayload = "BLINKFAST";
- //}
- //else if (payloadString.equalsIgnoreCase("JUGGLE")){
- // juggle();
- // FastLED.show();
- // lastPayload = "JUGGLE";
- //}
- //else if (payloadString.equalsIgnoreCase("CONFETTI")){
- // confetti();
- // FastLED.show();
- // lastPayload = "CONFETTI";
- //}
- //else if (payloadString.equalsIgnoreCase("RAINBOW2")){
- // rainbow2();
- // FastLED.show();
- // lastPayload = "Rainbow2";
- //}
- else if (payloadString.equalsIgnoreCase("OFF")){
- FastLED.clear();
- FastLED.show();
- lastPayload = "OFF";
- }
- //else if (payloadString.startsWith("RGB") && payloadString.length() >= 12) {
- // // Extract RGB values
- // String rStr = payloadString.substring(3, 6);
- // String gStr = payloadString.substring(6, 9);
- // String bStr = payloadString.substring(9, 12);
- //
- // r = rStr.toInt();
- // g = gStr.toInt();
- // b = bStr.toInt();
- //
- // fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
- // FastLED.show();
- // lastPayload = "RGB";
- //}
- else {
- payloadString = lastPayload;
- }
- //
- //EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement