ESP-32 + Neopixel with Openhab

Hello everyone,
The last few days I experimented with the LED Stripes and actually got them to work. Now I have a 5 meter Neopixel ws2812b stripe and have already written a program for it. However, the LEDs flicker like this:
https://1drv.ms/v/s!ApwRFzdtBkGqg5cGsoZUigPgdA_6Hg
The LEDs always flicker, whether a fixed program or color values. I use an external 5V 3A power supply. Here is my program:
// INCLUDE
#include <WiFi.h>
#include <PubSubClient.h>
#include <Adafruit_NeoPixel.h>
#ifdef AVR
#include <avr/power.h>
#endif

// Adafruit_NeoPixel
#define PIN            19
#define NUMPIXELS      300
int delayval = 20;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

// WIFI
const char* ssid = "Philipp";
const char* password = "PW";

// MQTT
const char* mqtt_server = "192.168.200.1";

// INIT
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

// Pin
const int ledPin = 19;

// Var
int sMode = 0;
int A = 0;
int Bs = 0;

int R = 0;
int G = 0;
int B = 0;
// SETUP

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  #if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  #endif
  // End of trinket special code
  pixels.begin(); // This initializes the NeoPixel library.
}



void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  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());
}




void callback(char* topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  // Feel free to add more if statements to control more GPIOs with MQTT

  // If a message is received on the topic esp32/output, you check if the message is either "on" or "off". 
  // Changes the output state according to the message
  if (String(topic) == "/WandLED/Mode") {
    Serial.print("WandLED Modus: ");
    Serial.println(messageTemp);
    sMode = messageTemp.toInt();
  }
  if (String(topic) == "/WandLED/R") {
    Serial.print("WandLED R: ");
    Serial.println(messageTemp);
    R = messageTemp.toInt();
  }
  if (String(topic) == "/WandLED/G") {
    Serial.print("WandLED G: ");
    Serial.println(messageTemp);
    G = messageTemp.toInt();
  }
  if (String(topic) == "/WandLED/B") {
    Serial.print("WandLED B: ");
    Serial.println(messageTemp);
    B = messageTemp.toInt();
  }
}




void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      // Subscribe
      client.subscribe("/WandLED/Mode");
      client.subscribe("/WandLED/R");
      client.subscribe("/WandLED/G");
      client.subscribe("/WandLED/B");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}


void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  if (sMode == 0){
        for(int i=0;i<150;i++){
            pixels.setPixelColor(i, pixels.Color(R,G,B)); // Moderately bright green color.
            pixels.show(); // This sends the updated pixel color to the hardware.
            delay(20);
        };
        delay(20);
  } else if (sMode == 1){
    A = 150;
    Bs = 150;
      for(int i=0;i<150;i++){

    pixels.setPixelColor(A, pixels.Color(0,100,0)); // Moderately bright green color.
    pixels.setPixelColor(Bs, pixels.Color(0,100,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    A--;
    Bs++;
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
  A = 150;
  Bs = 150;
  for(int i=0;i<150;i++){

    pixels.setPixelColor(A, pixels.Color(0,0,100)); // Moderately Bsright green color.
    pixels.setPixelColor(Bs, pixels.Color(0,0,100)); // Moderately Bsright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    A--;
    Bs++;
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
  A = 150;
  Bs = 150;
  for(int i=0;i<150;i++){

    pixels.setPixelColor(A, pixels.Color(100,0,0)); // Moderately Bsright green color.
    pixels.setPixelColor(Bs, pixels.Color(100,0,0)); // Moderately Bsright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    A--;
    Bs++;
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
  A = 150;
  Bs = 150;
  } else if (sMode == 2){
    for(int i=0;i<NUMPIXELS;i++){

    // pixels.Color takes RGBs values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,100,0)); // Moderately Bsright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
  for(int i=0;i<NUMPIXELS;i++){

    // pixels.Color takes RGBs values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,0,100)); // Moderately Bsright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
  for(int i=0;i<NUMPIXELS;i++){

    // pixels.Color takes RGBs values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(100,0,0)); // Moderately Bsright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }

  };
}

Thanks for your help!:+1::heart_eyes:

  • First of all, I would ask this question in the adafruit forum. This is not directly related to openHAB
  • Second, I would start by double checking ALL my connections, especially the power ones to the strip and monitor with a voltmeter.
  • Third, I would start debugging by adding `Serial.println(“blah blah” in the different sections of you code to see what it happening in real time inside the code.