Advertisement
Guest User

Untitled

a guest
Jun 17th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.16 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WebSocketsClient.h>
  3. #include <FastLED.h>
  4.  
  5. const char* ssid = "SSID";
  6. const char* password = "PASSWORD";
  7.  
  8. const char* twitch_oauth_token = "oauth:XXX";
  9. const char* twitch_nick = "XXX";    
  10. const char* twitch_channel = "#XXX";
  11.  
  12. WebSocketsClient webSocket;
  13.  
  14. String payloadString;
  15. String lastPayload = "";
  16. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  17.  
  18. #define NUM_LEDS 50
  19. #define ANALOG_PIN A0
  20. #define LED_PIN 5
  21. #define BUTTON_PIN 4
  22. #define LED_TYPE WS2811
  23. #define COLOR_ORDER GRB
  24.  
  25. #define COOLING 55
  26. #define SPARKING 120
  27. #define FRAMES_PER_SECOND 60
  28.  
  29. int buttonState;
  30. int lastButtonState = LOW;
  31. int BRIGHTNESS;
  32.  
  33. unsigned long lastDebounceTime = 0;
  34. unsigned long debounceDelay = 50;
  35.  
  36. uint8_t r = 255;
  37. uint8_t g = 255;
  38. uint8_t b = 255;
  39.  
  40. // Declare the LED array
  41. CRGB leds[NUM_LEDS];
  42.  
  43. void HsvToRgb(double hue, double saturation, double value, uint8_t& red, uint8_t& green, uint8_t& blue)
  44. {
  45.     double rR, gG, bB;
  46.  
  47.     auto i = static_cast<int>(hue * 6);
  48.     auto f = hue * 6 - i;
  49.     auto p = value * (1 - saturation);
  50.     auto q = value * (1 - f * saturation);
  51.     auto t = value * (1 - (1 - f) * saturation);
  52.  
  53.     switch (i % 6)
  54.     {
  55.     case 0: rR = value , gG = t , bB = p;
  56.         break;
  57.     case 1: rR = q , gG = value , bB = p;
  58.         break;
  59.     case 2: rR = p , gG = value , bB = t;
  60.         break;
  61.     case 3: rR = p , gG = q , bB = value;
  62.         break;
  63.     case 4: rR = t , gG = p , bB = value;
  64.         break;
  65.     case 5: rR = value , gG = p , bB = q;
  66.         break;
  67.     }
  68.  
  69.     red = static_cast<uint8_t>(rR * 255);
  70.     green = static_cast<uint8_t>(gG * 255);
  71.     blue = static_cast<uint8_t>(bB * 255);
  72. }
  73.  
  74. void webSocketEvent(WStype_t type, uint8_t* payload, size_t length) {
  75.   String payload_str = String((char*)payload);
  76.  
  77.   switch (type) {
  78.     case WStype_CONNECTED: {
  79.       Serial.printf("[WSc] Connected to: %s\n", payload_str.c_str());
  80.  
  81.       String passmsg_str = "PASS " + String(twitch_oauth_token);
  82.       String nickmsg_str = "NICK " + String(twitch_nick);
  83.       String joinmsg_str = "JOIN " + String(twitch_channel);
  84.  
  85.       webSocket.sendTXT(passmsg_str);
  86.       webSocket.sendTXT(nickmsg_str);
  87.       webSocket.sendTXT(joinmsg_str);
  88.       break;
  89.       }
  90.  
  91.     case WStype_TEXT: {
  92.       Serial.printf("> %s\n", payload_str.c_str());
  93.       if (payload_str.startsWith("PING ")) {
  94.         String pong = payload_str;
  95.         pong.replace("PING", "PONG");
  96.         webSocket.sendTXT(pong);
  97.         Serial.println("[WSc] Replied to PING");
  98.         return;
  99.       }
  100.  
  101.                 // Search for the beginning on the JSON-encoded message (":!")
  102.       int quote_start = payload_str.indexOf("$$");
  103.       // If the message is addressed to the chat bot
  104.       if(quote_start > 0) {
  105.         int quote_end = payload_str.length();
  106.         //Serial.println(quote_start);
  107.         //Serial.println(payload_str);
  108.         //Serial.println(quote_end);
  109.         //Serial.print(payload);
  110.         String pixel_str = payload_str.substring(quote_start+2, quote_end);
  111.         pixel_str.trim();
  112.         pixel_str.toUpperCase();
  113.         payloadString = pixel_str;
  114.         Serial.println("Command String: ");
  115.         Serial.println(pixel_str);
  116.  
  117.         if (payloadString.equalsIgnoreCase("RANDOM")){
  118.           double h = random(0, 255) / 255.0;
  119.           double s = 1.0;
  120.           double l = 1.0;
  121.  
  122.           HsvToRgb(h, s, l, r, g, b);
  123.  
  124.           fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
  125.           FastLED.show();
  126.         }  
  127.       }  
  128.       break;
  129.     }
  130.  
  131.     case WStype_DISCONNECTED: {
  132.       Serial.println("[WSc] Disconnected!");
  133.       break;
  134.     }
  135.  
  136.     default: {
  137.       break;
  138.     }
  139.   }
  140. }
  141.  
  142.  
  143. void setup() {
  144.   Serial.begin(115200);
  145.   WiFi.begin(ssid, password);
  146.   pinMode(BUTTON_PIN, INPUT);
  147.  
  148.   while(WiFi.status() != WL_CONNECTED) {
  149.     delay(500);
  150.     Serial.print(".");
  151.   }
  152.   Serial.println();
  153.   Serial.print("IP Address: "); Serial.println(WiFi.localIP());
  154.   Serial.println(" connected!");
  155.  
  156.   // Server address, port, and URL
  157.   //webSocket.setSSLClientCertKey(nullptr, nullptr, nullptr);
  158.   Serial.println("Beginning WebSocket connection...");
  159.   webSocket.begin("irc-ws.chat.twitch.tv", 80, "/");
  160.   webSocket.enableHeartbeat(15000, 3000, 2);  // ping interval, timeout, max lost pings
  161.   webSocket.onEvent(webSocketEvent);
  162.   webSocket.setReconnectInterval(5000);
  163.   Serial.println("WebSocket begin() called");
  164.   //// Initialize the FastLED object
  165.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  166.   FastLED.clear(true);
  167. }
  168.  
  169.  
  170.  
  171. // This function draws rainbows with an ever-changing,
  172. // widely-varying set of parameters.
  173. void pride()
  174. {
  175.   static uint16_t sPseudotime = 0;
  176.   static uint16_t sLastMillis = 0;
  177.   static uint16_t sHue16 = 0;
  178.  
  179.   uint8_t sat8 = beatsin88( 87, 220, 250);
  180.   uint8_t brightdepth = beatsin88( 341, 96, 224);
  181.   uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
  182.   uint8_t msmultiplier = beatsin88(147, 23, 60);
  183.  
  184.   uint16_t hue16 = sHue16;//gHue * 256;
  185.   uint16_t hueinc16 = beatsin88(113, 1, 3000);
  186.  
  187.   uint16_t ms = millis();
  188.   uint16_t deltams = ms - sLastMillis ;
  189.   sLastMillis  = ms;
  190.   sPseudotime += deltams * msmultiplier;
  191.   sHue16 += deltams * beatsin88( 400, 5,9);
  192.   uint16_t brightnesstheta16 = sPseudotime;
  193.  
  194.   for( uint16_t i = 0 ; i < NUM_LEDS; i++) {
  195.     hue16 += hueinc16;
  196.     uint8_t hue8 = hue16 / 256;
  197.  
  198.     brightnesstheta16  += brightnessthetainc16;
  199.     uint16_t b16 = sin16( brightnesstheta16  ) + 32768;
  200.  
  201.     uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
  202.     uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
  203.     bri8 += (255 - brightdepth);
  204.    
  205.     CRGB newcolor = CHSV( hue8, sat8, bri8);
  206.    
  207.     uint16_t pixelnumber = i;
  208.     pixelnumber = (NUM_LEDS-1) - pixelnumber;
  209.    
  210.     nblend( leds[pixelnumber], newcolor, 64);
  211.   }
  212. }
  213.  
  214.  
  215. void Fire2012()
  216. {
  217. // Array of temperature readings at each simulation cell
  218.   static uint8_t heat[NUM_LEDS];
  219.  
  220.   // Step 1.  Cool down every cell a little
  221.     for( int i = 0; i < NUM_LEDS; i++) {
  222.       heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  223.     }
  224.  
  225.     // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  226.     for( int k= NUM_LEDS - 1; k >= 2; k--) {
  227.       heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  228.     }
  229.    
  230.     // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  231.     if( random8() < SPARKING ) {
  232.       int y = random8(7);
  233.       heat[y] = qadd8( heat[y], random8(160,255) );
  234.     }
  235.  
  236.     // Step 4.  Map from heat cells to LED colors
  237.     for( int j = 0; j < NUM_LEDS; j++) {
  238.       CRGB color = HeatColor( heat[j]);
  239.       int pixelnumber;
  240.       pixelnumber = j;
  241.       leds[pixelnumber] = color;
  242.     }
  243. }
  244.  
  245. void juggle() {
  246.   // eight colored dots, weaving in and out of sync with each other
  247.   fadeToBlackBy( leds, NUM_LEDS, 20);
  248.   uint8_t dothue = 0;
  249.   for( int i = 0; i < 8; i++) {
  250.     leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  251.     dothue += 32;
  252.   }
  253. }
  254.  
  255. void rainbow2()
  256. {
  257.   // FastLED's built-in rainbow generator
  258.   fill_rainbow( leds, NUM_LEDS, gHue, 7);
  259. }
  260.  
  261. void confetti()
  262. {
  263.   // random colored speckles that blink in and fade smoothly
  264.   fadeToBlackBy( leds, NUM_LEDS, 10);
  265.   int pos = random16(NUM_LEDS);
  266.   leds[pos] += CHSV( gHue + random8(64), 200, 255);
  267. }
  268.  
  269. unsigned long lastAnalogRead = 0;
  270. const int analogInterval = 100;  // milliseconds
  271.  
  272. void loop() {
  273.   webSocket.loop();
  274.   //BRIGHTNESS = map(analogRead(ANALOG_PIN), 2, 1024, 0, 255);
  275.   unsigned long now = millis();
  276.   if (now - lastAnalogRead > analogInterval) {
  277.     lastAnalogRead = now;
  278.     int raw = analogRead(ANALOG_PIN);
  279.     BRIGHTNESS = map(raw, 2, 1024, 0, 255);
  280.   }
  281.  
  282.   FastLED.setBrightness(BRIGHTNESS);
  283.   FastLED.show();
  284.  
  285.   int reading = digitalRead(BUTTON_PIN);
  286.   if (reading != lastButtonState) {
  287.     lastDebounceTime = millis();
  288.   }
  289.   if ((millis() - lastDebounceTime) > debounceDelay) {
  290.     if (reading != buttonState) {
  291.       buttonState = reading;
  292.       if (buttonState == HIGH) {
  293.         Serial.println("Pressed");
  294.       }
  295.     }
  296.   }
  297.   lastButtonState = reading;
  298.   yield();  // or: delay(1);
  299.  
  300.   //if (payloadString.equalsIgnoreCase("RAINBOW")){
  301.   //    pride();
  302.   //    FastLED.show();
  303.   //    lastPayload = "RAINBOW";
  304.   //}
  305.   //else if (payloadString.equalsIgnoreCase("FIRE")){
  306.   //  Fire2012();
  307.   //  FastLED.show();
  308.   //  //FastLED.delay(1000 / FRAMES_PER_SECOND);
  309.   //  delay(50);
  310.   //  lastPayload = "FIRE";
  311.   //}
  312.   //else if (payloadString.equalsIgnoreCase("RANDOM")) {
  313.   //  lastPayload = "RANDOM";
  314.   //}
  315.   //else
  316.   if (payloadString.equalsIgnoreCase("BLINK")){
  317.     fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
  318.     FastLED.show();
  319.     delay(750);
  320.     FastLED.clear();
  321.     FastLED.show();
  322.     delay(750);
  323.     lastPayload = "BLINK";
  324.   }
  325.   //else if (payloadString.equalsIgnoreCase("BLINKFAST")){
  326.   //  fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
  327.   //  FastLED.show();
  328.   //  delay(75);
  329.   //  FastLED.clear();
  330.   //  FastLED.show();
  331.   //  delay(75);
  332.   //  lastPayload = "BLINKFAST";
  333.   //}
  334.   //else if (payloadString.equalsIgnoreCase("JUGGLE")){
  335.   //  juggle();
  336.   //  FastLED.show();
  337.   //  lastPayload = "JUGGLE";
  338.   //}
  339.   //else if (payloadString.equalsIgnoreCase("CONFETTI")){
  340.   //  confetti();
  341.   //  FastLED.show();
  342.   //  lastPayload = "CONFETTI";
  343.   //}  
  344.   //else if (payloadString.equalsIgnoreCase("RAINBOW2")){
  345.   //  rainbow2();
  346.   //  FastLED.show();
  347.   //  lastPayload = "Rainbow2";
  348.   //}  
  349.   else if (payloadString.equalsIgnoreCase("OFF")){
  350.     FastLED.clear();
  351.     FastLED.show();
  352.     lastPayload = "OFF";
  353.   }    
  354.   //else if (payloadString.startsWith("RGB") && payloadString.length() >= 12) {
  355.   //  // Extract RGB values
  356.   //  String rStr = payloadString.substring(3, 6);
  357.   //  String gStr = payloadString.substring(6, 9);
  358.   //  String bStr = payloadString.substring(9, 12);
  359. //
  360.   //  r = rStr.toInt();
  361.   //  g = gStr.toInt();
  362.   //  b = bStr.toInt();
  363. //
  364.   //  fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
  365.   //  FastLED.show();
  366.   //  lastPayload = "RGB";
  367.   //}  
  368.   else {
  369.     payloadString = lastPayload;
  370.   }
  371. //
  372.   //EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement