Addressable RGB Strip, Wifi, Calling Class not Working

Hello,
I have been on this forum for quite some time but until now I just got help through other posts. I have searched a lot but didn’t find an answer so I figured, that I’m going to create a post.
I bought a WS2812B RGB Strip (https://www.amazon.de/gp/product/B01CDTEJBG/ref=ppx_yo_dt_b_asin_title_o06_s00?ie=UTF8&psc=1) and a ESP32 Dev Kit: (https://www.amazon.de/gp/product/B07Z83MF5W/ref=ppx_yo_dt_b_asin_title_o03_s00?ie=UTF8&psc=1) to program animations on my Strip and to call them through a WiFi Server. For my project I use the FastLED Library and the WiFi Library. With some tutorials and posts on this forum I have written a code which doesn’t work but I don’t know why. I have done some Trouble-Shooting but didn’t find a reason, why it isn’t working. The sketch has one main tab and then 3 others in which there are the different animations stored.

Code Main Tab:

#include <FastLED.h>
#include <WiFi.h>
#define NUM_LEDS  300
#define LED_PIN   5

CRGB leds[NUM_LEDS];
bool isRunning = false;
uint8_t Chase = 0;

#include "MovingDot.h"
#include "RainbowBeat.h"
#include "RedWhiteBlue.h"

const char* ssid     = "---";
const char* password = "---";

WiFiServer server(80);

void setup()
{
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);

  Serial.begin(115200);
  pinMode(5, OUTPUT);

  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  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());

  server.begin();

}

int value = 0;

void loop() {
  WiFiClient client = server.available();

  if (client) {
    Serial.println("New Client.");
    String currentLine = "";
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        if (c == '\n') {



          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();


            client.print("Click <a href=\"/D\">here</a> to run Moving Dot.
");
            client.print("Click <a href=\"/R\">here</a> to run Rainbow Beat.
");
            client.print("Click <a href=\"/W\">here</a> to run RedWhiteBlue.
");


            client.println();

            break;
          } else {
            currentLine = "";
          }
        } else if (c != '\r') {
          currentLine += c;
        }


        if (currentLine.endsWith("GET /D")) {
          Chase = 0;
        }
      }
      if (currentLine.endsWith("GET /R")) {
        Chase = 1;
      }
      if (currentLine.endsWith("GET /W")) {
        Chase = 2;
      }

    }

    Serial.println (Chase);
    client.stop();
    Serial.println("Client Disconnected.");

  }
  void switchChase();
}



void switchChase () {
  switch (Chase) {
    case 0:
      runMovingDot();
      break;
    case 1:
      runRainbowBeat();
      break;
    case 3:
      runRedWhiteBlue();
      break;
  }
}

void runMovingDot() {
  isRunning = true;
  MovingDot movingDot = MovingDot();
  while (isRunning) movingDot.runPattern();
}

void runRainbowBeat() {
  isRunning = true;
  RainbowBeat rainbowBeat = RainbowBeat();
  while (isRunning) rainbowBeat.runPattern();
}

void runRedWhiteBlue() {
  isRunning = true;
  RedWhiteBlue redWhiteBlue = RedWhiteBlue();
  while (isRunning) redWhiteBlue.runPattern();
}

Code Second Tab: MovingDot.h

#include "Arduino.h"

class MovingDot {
  public:
    MovingDot(){
    
    };
    void runPattern();
  private:
};

void MovingDot::runPattern() {

  uint16_t posBeat  = beatsin16(30, 0, NUM_LEDS - 1, 0, 0);
  uint16_t posBeat2 = beatsin16(60, 0, NUM_LEDS - 1, 0, 0);

  uint16_t posBeat3 = beatsin16(30, 0, NUM_LEDS - 1, 0, 32767);
  uint16_t posBeat4 = beatsin16(60, 0, NUM_LEDS - 1, 0, 32767);

  // Wave for LED color
  uint8_t colBeat  = beatsin8(45, 0, 255, 0, 0);

  leds[(posBeat + posBeat2) / 2]  = CHSV(colBeat, 255, 255);
  leds[(posBeat3 + posBeat4) / 2]  = CHSV(colBeat, 255, 255);

  fadeToBlackBy(leds, NUM_LEDS, 10);
  FastLED.show();
}

Code third tab: RainbiwBeat.h

#include "Arduino.h"

class RainbowBeat {
  public:
    RainbowBeat(){};
    void runPattern();
  private:
};

void RainbowBeat::runPattern() {
  
  uint16_t beatA = beatsin16(30, 0, 255);
  uint16_t beatB = beatsin16(20, 0, 255);
  fill_rainbow(leds, NUM_LEDS, (beatA+beatB)/2, 8);
  FastLED.show();
}

Code fourth tab: RedWhiteBlue.h

#include "Arduino.h"

class RedWhiteBlue {
  public:
    RedWhiteBlue(){};
    void runPattern();
  private:
};

void RedWhiteBlue::runPattern() {

  uint16_t sinBeat   = beatsin16(30, 0, NUM_LEDS - 1, 0, 0);
  uint16_t sinBeat2  = beatsin16(30, 0, NUM_LEDS - 1, 0, 21845);
  uint16_t sinBeat3  = beatsin16(30, 0, NUM_LEDS - 1, 0, 43690);

  leds[sinBeat]   = CRGB::Blue;
  leds[sinBeat2]  = CRGB::Red;
  leds[sinBeat3]  = CRGB::White;
  
  fadeToBlackBy(leds, NUM_LEDS, 10);
  FastLED.show();

}

What I am trying to do is to run different animations on my LED-Strip when I enter a certain IP in my browser on my PC. As you can see in the code the Class is being printed to the serial monitor to check whether the WiFi Server works or not. The Code below is just copy pasted out of the serial monitor but for private reasons I deleted some things and marked them with "---"

New Client.
GET /favicon.ico HTTP/1.1
Host: ---
Connection: keep-alive
User-Agent:---
Accept: ---
Referer: ---
Accept-Encoding: gzip, deflate
Accept-Language: ---

2
Client Disconnected.
New Client.
GET /R HTTP/1.1
Host: ---
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: ---
Accept: ---
Referer: ---
Accept-Encoding: gzip, deflate
Accept-Language: ---

1

As you can see the class changes from 2 to 1 so the WiFi server works, but it will not change the animation on the LED-Strip.

Maybe there is just a tiny mistake, but it could also be, that I have done something that is completely wrong.

Thanks to everyone who takes the time and tries to help me.

Have you done any "tried and true" divide and conquer methodology? For example, test the LED strip separately with a minimal test sketch? Test the Wifi part also separately?

There's a lot you didn't explicitly state. For example, it's an open question whether you have ever made the LEDs operate at all. Hence the above questions.

Yes I did some testing. I have another Sketch which works just fine but I dont change the animations wirelessly, i change them with buttons. The WiFi part also Works. I can use the WiFi part to turn on and off an LED if i modify the if statement to digitalWrite the LED Pin HIGH or LOW like this:

 if (currentLine.endsWith("GET /D")) {
          digitalWrite (5, HIGH);
        }

The code to change the animation with buttons. Its just the main part. There are 3 more tabs wich are the same as in the code, which is in my Thread, except that there is one more function which checks for the Button in each of the three other tabs:
btn1.tick();
btn2.tick();

#include <FastLED.h>
#include <OneButton.h>

#define NUM_LEDS  300
#define LED_PIN   5
#define BTN_PIN1  15
#define BTN_PIN2  21

CRGB leds[NUM_LEDS];
uint8_t patternCounter = 0;
bool isRunning = false;

OneButton btn1 = OneButton(BTN_PIN1, true, true);
OneButton btn2 = OneButton(BTN_PIN2, true, true);

#include "MovingDot.h"
#include "RainbowBeat.h"
#include "RedWhiteBlue.h"

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
  Serial.begin(57600);

  btn1.attachClick(nextPattern1);
  btn2.attachClick(nextPattern2);
}

void loop() {

  switch (patternCounter) {
    case 0:
      runMovingDot();
      break;
    case 1:
      runRainbowBeat();
      break;
    case 2:
      runRedWhiteBlue();
      break;
  }
}

void nextPattern1() {
  isRunning = false;
  patternCounter = 1;
}
void nextPattern2() {
  isRunning = false;
  patternCounter = 2;
}

void runMovingDot() {
  isRunning = true;
  MovingDot movingDot = MovingDot();
  while (isRunning) movingDot.runPattern();
}

void runRainbowBeat() {
  isRunning = true;
  RainbowBeat rainbowBeat = RainbowBeat();
  while (isRunning) rainbowBeat.runPattern();
}

void runRedWhiteBlue() {
  isRunning = true;
  RedWhiteBlue redWhiteBlue = RedWhiteBlue();
  while (isRunning) redWhiteBlue.runPattern();
}

The code to turn on or off an LED wirelessly (it is just the SimpleWiFiServer example Sketch):

 */

#include <WiFi.h>

const char* ssid     = "yourssid";
const char* password = "yourpasswd";

WiFiServer server(80);

void setup()
{
    Serial.begin(115200);
    pinMode(5, OUTPUT);   

    delay(10);

    // We start by connecting to a WiFi network

    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    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());
    
    server.begin();

}

int value = 0;

void loop(){
 WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 5 on.
");
            client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 5 off.
");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(5, HIGH);               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(5, LOW);                // GET /L turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

I modified the main Sketch a little to show me if the void switchChase () is even running and it seems like it doesnt even start. When I check the serial monitor theres is nothing printed out which is in the void switchChase().

#include <FastLED.h>
#include <WiFi.h>
#define NUM_LEDS  300
#define LED_PIN   5

CRGB leds[NUM_LEDS];
bool isRunning = false;
uint8_t Chase = 0;

#include "MovingDot.h"
#include "RainbowBeat.h"
#include "RedWhiteBlue.h"

const char* ssid     = "---";
const char* password = "---";

WiFiServer server(80);

void setup()
{
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);

  Serial.begin(115200);

  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  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());

  server.begin();

}

int value = 0;

void loop() {
  WiFiClient client = server.available();

  if (client) {
    Serial.println("New Client.");
    String currentLine = "";
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        if (c == '\n') {



          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();


            client.print("Click <a href=\"/D\">here</a> to run Moving Dot.
");
            client.print("Click <a href=\"/R\">here</a> to run Rainbow Beat.
");
            client.print("Click <a href=\"/W\">here</a> to run RedWhiteBlue.
");


            client.println();

            break;
          } else {
            currentLine = "";
          }
        } else if (c != '\r') {
          currentLine += c;
        }


        if (currentLine.endsWith("GET /D")) {
          Chase = 0;
        }
        if (currentLine.endsWith("GET /R")) {
          Chase = 1;
        }
        if (currentLine.endsWith("GET /W")) {
          Chase = 2;
        }
        void switchChase();
      }


    }

    Serial.println (Chase);
    client.stop();
    Serial.println("Client Disconnected.");


  }

}



void switchChase() {
  Serial.println ("I am working");
  switch (Chase) {
    case 0:
      runMovingDot();
      Serial.println ("Moving Dot is running");
      break;
    case 1:
      runRainbowBeat();
      Serial.println ("Rainbow Beat is running");
      break;
    case 2:
      runRedWhiteBlue();
      Serial.println ("Red White Blue is running");
      break;
  }
}

void runMovingDot() {
  isRunning = true;
  MovingDot movingDot = MovingDot();
  while (isRunning) movingDot.runPattern();
}

void runRainbowBeat() {
  isRunning = true;
  RainbowBeat rainbowBeat = RainbowBeat();
  while (isRunning) rainbowBeat.runPattern();
}

void runRedWhiteBlue() {
  isRunning = true;
  RedWhiteBlue redWhiteBlue = RedWhiteBlue();
  while (isRunning) redWhiteBlue.runPattern();
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.