[SOLVED] NodeMCU connection drop with LED strip animation

Hi! I’m working on my first project with Blynk and I’m having overall good results.

I am using a NodeMCU to control one LED strip with the Adafruit Neopixel library. If I just want to change the color of the entire strip using the zeRGBra it works as expected however when I try to implement some efects, such as a rainbow, my Node goes offline when trying to stop the animation.

Here is part of the code:

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
#include <Adafruit_NeoPixel.h>

char blynk_token[34] = "BLYNK_TOKEN";

#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
SimpleTimer timer;

#define NUMPIXELS 23
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, D9, NEO_RGB + NEO_KHZ800);

void setup()
{

  Serial.begin(115200);

  Blynk.config(blynk_token);
  Blynk.connect();
  
  strip.begin();
  strip.show();

}

BLYNK_WRITE(V1) // When I turn on the button it works, if I try to turn it off the Node gets disconnected
{
  int pinValue = param.asInt(); 
  while(pinValue){
    rainbowCycle(20);
  }

}

BLYNK_WRITE(V2) //Use of zeRGBra, works fine
{
  int R = param[0].asInt();
  int G = param[1].asInt();
  int B = param[2].asInt();
  
  for(int i=0;i<NUMPIXELS;i++){
    strip.setPixelColor(i, strip.Color(R,G,B)); 
    strip.show(); 
  }

}



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

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}


// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) { 
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}


// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

I took off some parts that were not related to the problem, like WiFiManager, that’s why there’s no SSID, password.

TL;DR I want to click a button and see a rainbow effect for as long as the button is ON. Then be able to switch it off and change the color, turn on another effect…

If the code that produces the rainbow effect is in a closed loop (aka blocking) for more than a second or so, then the background Blynk functionality is stopped, and possibly the ESP based background WiFi functionality as well, and your device will get disconnected.

This is where using Blynk Timers and clever code becomes the norm.

Ok, I got it working now, I had to stop/start the timer. The idea of the code is this one:

void setup()
{
  Blynk.config(blynk_token);
  Blynk.connect();
  timer_id = timer_blynk.setInterval(1000L, rainbowTimer);
  timer_blynk.disable(timer_id);
  strip.begin();
  strip.show();
  

}

void rainbowTimer()
{
  if (arcoiro == 1)
  {
    rainbowCycle(15);
  }
}

BLYNK_WRITE(V1)
{
  arcoiro = param.asInt(); // assigning incoming value from pin V1 to a variable
  if(arcoiro == 1)
  {
    timer_blynk.enable(timer_id);
    timer_blynk.restartTimer(timer_id);
  }else
  {
    timer_blynk.disable(timer_id);
  }
}

1 Like