NeoPixel LedCount causes Blynk to Fail!

Hello Everyone!

I’m on a ambilight project for my Tv. I wanted to use blynk to control my led strip over wifi.
I have a setting on my strip code which defines the number of leds in the strip.
#define LEDCOUNT 50

if the led count is not so many like 50 or 75. Everything is fine, my program works like a charm.

But if increase the led count to 173 (real led count in my project), blynk starts to fail. I get “Failed to disable echo” or “Failed to set STA mode” error. If i reduce the led count to 50 after that, everything works again.
According to my research on the adafruit website, led count effects the ram usage of the code. I am not sure the problem is related to ram or whatever!

You can see my code below. If anyone has an idea regarding to my led count issue, it would be great to hear!

Thanks in advance.

Each NeoPixel requires about 3 bytes of RAM. This doesn’t sound like very much, but when you start using dozens or even hundreds of pixels, and consider that the mainstream Arduino Uno only has 2 kilobytes of RAM (often much less after other libraries stake their claim), this can be a real problem!
link!

#include <Adafruit_NeoPixel.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#include <SoftwareSerial.h>

/*---------------*/
/* Wifi Settings */
/*---------------*/

char auth[] = "6333a7e3c98d4ed2ba15618c8e6fe48f"; //for blynk cloud
//char auth[] = " 385beae3065847bc14bea96ef16ec522"; // local rasp3 openelecv6.03

// Set password to "" for open networks.
char ssid[] = "30_M_BALIKESIR";
char pass[] = "bilmiyorum";

SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 9600 // Your ESP8266 baud rate:

/*---------------*/
/* DHT Settings */
/*---------------*/
#define DHTTYPE DHT11     // DHT 11
#define DHTPIN 5          // What digital pin we're connected to

/*-------------------------------*/
/* Hyperion - Ambilight Settings */
/*-------------------------------*/

#define STARTCOLOR 0xffffff // LED colors at start
#define BLACK      0x000000  // LED color BLACK

#define DATAPIN    6       // Datapin
#define LEDCOUNT   50 //  Number of LEDs used for boblight 173
// LEDCOUNT value is local value in Arduino sketch, for hyperion it doesn't matter it sends prefx characters according to hyperion config
#define SHOWDELAY  200       // Delay in micro seconds before showing default 200
#define BAUDRATE   9600// Serial port speed, 460800 tested with Arduino Uno R3 23400 za MEGA, 115200 nano

int BRIGHTNESS = 70;   // Max. brightness in %
//Hyperion sends prefix characters based on number of LEDs in config file
// e.g. for 181 LEDs it will send 0xB4 and cheksum 0xE1
// keep in mind if you are using boblight config to calculate prefix that Boblight counts diodes from 1 and Hyperion from 0
// if you have problems try +1 or -1 diodes when generating prefix characters
// values to save some time: 178 B1 E4, 180 B3E6, 181 B4E1, 182 B5E0 
//hyperion code
//_ledBuffer[3] = ((ledValues.size() - 1) >> 8) & 0xFF; // LED count high byte
//    _ledBuffer[4] = (ledValues.size() - 1) & 0xFF;        // LED count low byte
//    _ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
const char prefix[] = {0x41, 0x64, 0x61, 0x00, 0xB4, 0xE1};  // Start prefix ADA
char buffer[sizeof(prefix)]; // Temp buffer for receiving prefix data 

// Init LED strand, WS2811/WS2912 specific
// These might work for other configurations:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDCOUNT, DATAPIN, NEO_GRB + NEO_KHZ800);

/* Variables */

ESP8266 wifi(&EspSerial);
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
WidgetLED led8(V8);

int state;                   // Define current state
#define STATE_WAITING   1    // - Waiting for prefix
#define STATE_DO_PREFIX 2    // - Processing prefix
#define STATE_DO_DATA   3    // - Handling incoming LED colors
int readSerial;           // Read Serial data (1)
int currentLED;           // Needed for assigning the color to the right LED


void setup()
{

  
  /* wifi */
  Serial.begin(BAUDRATE);   // Init serial speed
   delay(50);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(50);
  Blynk.begin(auth, wifi, ssid, pass); //for blynk cloud
  Serial.println("3");
  //Blynk.begin(auth, wifi, ssid, pass, "192.168.1.119"); // local rasp3 openelecv6.03
  dht.begin();
  Serial.println("4");
  timer.setInterval(1000L, sendSensor);

  /* strip */
  state = STATE_WAITING;    // Initial state: Waiting for prefix
  delay(10);
  strip.begin();            // Init LED strand, set all black, then all to startcolor
  strip.setBrightness( (255 / 100) * BRIGHTNESS );
  setAllLEDs(BLACK, 0);
  setAllLEDs(STARTCOLOR, 5);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer

  switch(state)
  {
    case STATE_WAITING:                  // *** Waiting for prefix ***
      if( Serial.available()>0 )
      {
        readSerial = Serial.read();      // Read one character
        
        if ( readSerial == prefix[0] )   // if this character is 1st prefix char
          { state = STATE_DO_PREFIX; }   // then set state to handle prefix
      }
      break;
      
      
    case STATE_DO_PREFIX:                // *** Processing Prefix ***
      if( Serial.available() > sizeof(prefix) - 2 ) 
      {
          Serial.readBytes(buffer, sizeof(prefix) - 1);
          
          for( int Counter = 0; Counter < sizeof(prefix) - 1; Counter++) 
          {
            if( buffer[Counter] == prefix[Counter+1] ) 
            {
              state = STATE_DO_DATA;     // Received character is in prefix, continue
              currentLED = 0;            // Set current LED to the first one
            }
            else 
            {
              state = STATE_WAITING;     // Crap, one of the received chars is NOT in the prefix
              break;                     // Exit, to go back to waiting for the prefix
            } // end if buffer
          } // end for Counter
      } // end if Serial
      break;
      
      
    case STATE_DO_DATA:                  // *** Process incoming color data ***
      if( Serial.available() > 2 )       // if we receive more than 2 chars
      {
        Serial.readBytes( buffer, 3 );   // Abuse buffer to temp store 3 charaters
        strip.setPixelColor( currentLED++, buffer[0], buffer[1], buffer[2]);  // and assing to LEDs
      }
  
      if( currentLED > LEDCOUNT )        // Reached the last LED? Display it!
      {
          strip.show();                  // Make colors visible
          delayMicroseconds(SHOWDELAY);  // Wait a few micro seconds
          
          state = STATE_WAITING;         // Reset to waiting ...
          currentLED = 0;                // and go to LED one
          
          break;                         // and exit ... and do it all over again
      }
      break; 
  } // switch(state)
}



/* Functions */
BLYNK_WRITE(V0)
{
  BRIGHTNESS = param.asInt(); // assigning incoming value from pin V0 to a variable
  strip.setBrightness( (255 / 100) * BRIGHTNESS );
  strip.begin();
  delay(10);
  strip.show();
  Serial.print("V0 Slider value is: ");
  Serial.println(BRIGHTNESS);
}

BLYNK_WRITE(V1) { //zeRGGa data from widget
    int r = param[0].asInt();
    int g = param[1].asInt();
    int b = param[2].asInt();
    colorAll(strip.Color(r, g, b), 1);
    strip.show();
}

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setAllLEDs(uint32_t color, int wait)
{
  for ( int Counter=0; Counter < LEDCOUNT; Counter++ )      // For each LED
  {
    strip.setPixelColor( Counter, color );      // .. set the color

    if( wait > 0 )                        // if a wait time was set then
    {
      strip.show();                     // Show the LED color
      delay(wait);                      // and wait before we do the next LED
    } // if wait
    
  } // for Counter
  
  strip.show();                         // Show all LEDs
} // setAllLEDs

void colorAll(uint32_t c, uint8_t wait) {
  uint16_t i;

  for(i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
  }
  strip.show();
  delay(1);
}
1 Like

** Code snippets should be formatted. Edit your post**

Wrap the code by adding 3 Backtick: ``` symbols:

Example:

 ``` cpp <--put 3 backticks BEFORE your code starts  (cpp means C++ language) 

   //Put your code here
   //..................
   //..................

 ``` <--insert 3 backticks AFTER your code

**This makes your code readable and with highlighted syntax, like this:**
//comment goes here 
void helloWorld() { 
   String message =  "hello" + "world"; 
}

Have you tried using the FastLED library instead of the NeoPixel one? It’s much more efficient. SoftSerial is also not the best way to connect an ESP to the Arduino. It’s probably better to use the ESP stand alone. It’s much more powerful then an Arduino anyway.

The Wemos D1 (Mini or Pro) is an awesome thing for projects like these and I’m pretty sure the Wemos Pro can handle all the LED’s you want.

Thank for the feedback.

I would prefer to change the Nexopixel to Fastled library and harware of project as a last chance.

I wonder what may cause to fail my Blink when i increase to led count. It would be perfect to find root cause and to solve the problem.

The WeMos mini pro has 16mb ram. :smiley:
So do the math… 16,000,000/3= roughly 5 milion leds😁

Who told you that ??? Kill him (joking).
You’re talking about 16Mb flash…

Hahaha sorry I misread :wink:

Hi, in case this issue still interests you I’ve had a similar issue in NeoPixels freeze while Blynk sends heartbeat.