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.