NodeMCU esp8266 12E keeps deconnecting after 15s

Hi!
I’m currently building a sunrise simulator lamp for my 3yo son.
Read a lot of documentation, proceeded to many tests using esp8266 nodemcu 12E, worked fine using ZeRGBa and some virtual buttons to light a led strip in the colors I wanted, so as for different sliders.(still during tests).

I managed to write a sunrise code for the WS2811 RGB led strip (sorry if it’s not perfect, i’m quite new in coding), put it in a new project using the same auth,SSID and Wifi password.
Got the nodemcu connected, had the virtual button set up, nodemcu connected.
When I use it to begin, the sunrise starts and after 15-20 seconds a notification appears " test" (name of the project on blynk app) is offline.
I deleted all the code and set a virtual button to light the ledstrip in one color-> no deconection, same for ZeRGBa.
I assume it’s the whole code which is maybe too heavy for the nodemcu to manage it.
Thanks for your help.

50 ws2811 leds on the thed strip.
5V 8A power supply.
Esp8266 wired on the power supply using a nano usb port.
Led Strip wired also to the power supply (shared +5v and gnd for esp and led strip)
Data ledstrip connected on D4.

#include <FastLED.h>
#define LED_PIN     4                //CONFIGURE LED STRIP HERE
#define NUM_LEDS    50
#define BRIGHTNESS  255
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#define FASTLED_ESP8266_RAW_PIN_ORDER

#include <SPI.h>


char auth[] = "-----";
char ssid[] = "----";
char pass[] = "----";

int data=255;
int r,g,b;

//Previous value of RGB
int redPrevious, greenPrevious, bluePrevious = 0;

//Current value of RGB
float redCurrent, greenCurrent, blueCurrent = 0;

//Target value of RGB
int redTarget, greenTarget, blueTarget = 0;

int fade_delay = 100;
int steps = 200;

void setup() {
  Serial.begin(9600);
  delay( 3000 ); // power-up safety delay
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  Blynk.begin(auth, ssid, pass);
}
BLYNK_WRITE(V2)
{

int R = param[0].asInt();
int G = param[1].asInt();
int B = param[2].asInt();
Serial.println(R);
Serial.println(G);
Serial.println(B);
for(int i=0;i<NUM_LEDS;i++){
 
  FadeToColour(0, 0, 100);
  FadeToColour(0, 100, 100);
  FadeToColour(0, 100, 0);
  FadeToColour(0, 150, 0);
  FadeToColour(50, 150, 0);
  FadeToColour(100, 200, 0);
  FadeToColour(160, 200, 0);
  FadeToColour(200, 255, 0);
  FadeToColour(255, 255, 0);
  FadeToColour(255, 255, 255);
}
}

void FadeToColour(int r, int g, int b) {
  redPrevious = redCurrent;
  greenPrevious = greenCurrent;
  bluePrevious = blueCurrent;

  redTarget = r;
  greenTarget = g;
  blueTarget = b;

  float redDelta = redTarget - redPrevious;
  redDelta = redDelta / steps;

  float greenDelta = greenTarget - greenPrevious;
  greenDelta = greenDelta / steps;

  float blueDelta = blueTarget - bluePrevious;
  blueDelta = blueDelta / steps;

  for (int j = 1; j < steps; j++) {
    redCurrent = redPrevious + (redDelta * j);
    greenCurrent = greenPrevious + (greenDelta * j);
    blueCurrent = bluePrevious + (blueDelta * j);
    setColour(redCurrent, greenCurrent, blueCurrent, 6);
    delay(fade_delay);  //Delay between steps
  }
  delay(1000); //Wait at peak colour before continuing
}

void setColour(int r, int g, int b, int lednum) {
  Serial.println(r);
  for(int i=0; i<NUM_LEDS;i++){
  leds[i] = CRGB(r, g, b);
   FastLED.show();
  }
}
void loop()
{

Blynk.run();
}


The thing about Blynk is that it does some very clever stuff in the background. It’s constantly talking to the server to see if the user has made any changes to the widgets in the app. Ideally this communication should be dozens of times per second or more. If a device stops talking to the server for around 10 seconds then the server decides that it’s MIA and lists it as missing, presumed dead - which translates to “offline” in the app.
It’s the Blynk.run commands that trigger the device to server communication.

I’m assuming that your ‘for’ loops are taking quite a long time to execute?
If that’s the case then the Blynk library isn’t being told to ‘phone home’ while the functions that contain these loops are executing, so the device is being shown as offline.
The simplest solution is probably to add additional Blynk.run commands inside your for loops, so that the Blynk library gets some processor time while the loops are executing.

Pete.

thanks for this fast reply.
I added some Blynk.run in the loops but got the esp deconnected again.
My gess is the first loop with color fading instructions is too long (even if i added one Blynk.run after each fade).

Edit#2 I even divided each fading instruction in single shorter ones
ex {…(0,0,10);
Blynk.run;
}
{ (…0,0,20);
Blynk.run;
}
etc instead of (0,0,100) + a blynk.run after each segment.
I shortened fading delay, reduced number of steps… still disconnected.
I must be missing something quite simple

Please post your new sketch , so we could analyze it.
Sure @PeteKnight is right about your for loops and delays.

1 Like

I’ll do it as soon as soon as I came home tonight.

1 Like

The problem of your code is that it does too many things for a too long time in BLYNK_WRITE() and prevents the necessary communications between the board and Blynk Server.
One call to FadeToColor() will take more than ( (200x100ms) + 1s ) = 21 s to finish. And you have (NUM_LEDS x 10 ) FadeToColor() calls in each BLYNK_WRITE().
That is a certain way the server decides that it’s MIA.

You can try e following modified code, using BlynkTimer to perform similar task. Certainly you have to modify a little bit more to make your lovely son happier

#define BLYNK_PRINT Serial

#include <SPI.h>
#include <BlynkSimpleEsp8266.h>
#include <FastLED.h>

#define LED_PIN             4                //CONFIGURE LED STRIP HERE
#define NUM_LEDS            50
#define BRIGHTNESS          255
#define LED_TYPE            WS2811
#define COLOR_ORDER         GRB
#define UPDATES_PER_SECOND  100

#define FASTLED_ESP8266_RAW_PIN_ORDER

char auth[] = "***";
char ssid[] = "***";
char pass[] = "***";

//Previous value of RGB
int redPrevious, greenPrevious, bluePrevious = 0;

//Current value of RGB
int redCurrent, greenCurrent, blueCurrent = 0;

//Target value of RGB
int redTarget, greenTarget, blueTarget = 0;

float redDelta, greenDelta, blueDelta;

// Change LED colour every LED_INTERVAL_MS (currently 100ms)
#define LED_INTERVAL_MS         100

#define MAX_COLOUR_STEPS        200

int currentStep = 0;

CRGB leds[NUM_LEDS];

#define BLYNK_VPIN_ZERGBA     V4

BlynkTimer LEDTimer;

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(BLYNK_VPIN_ZERGBA);
}

// ZeRGBa widget
BLYNK_WRITE(BLYNK_VPIN_ZERGBA)
{
  int R = param[0].asInt();
  int G = param[1].asInt();
  int B = param[2].asInt();
  Serial.println("Target => R = " + String(R) + ", G = " + String(G) + ", B = " + String(B));

  FadeToColour(R, G, B);
}

void FadeToColour(int redTarget, int greenTarget, int blueTarget)
{
  redPrevious   = redCurrent;
  greenPrevious = greenCurrent;
  bluePrevious  = blueCurrent;

  redDelta    = (float) (redTarget - redPrevious) / MAX_COLOUR_STEPS;

  greenDelta  = (float) (greenTarget - greenPrevious) / MAX_COLOUR_STEPS;

  blueDelta   = (float) (blueTarget - bluePrevious) / MAX_COLOUR_STEPS;

  // Reset currentStep to enable LEDTimer running
  currentStep = 0;
}

void setColour(int r, int g, int b, int lednum)
{
  for (int i = 0; i < NUM_LEDS; i++)
  {
    leds[i] = CRGB(r, g, b);
    FastLED.show();
  }
}

void displayLED(void)
{
  if (currentStep <= MAX_COLOUR_STEPS)
  {
    redCurrent = redPrevious + (redDelta * currentStep);
    greenCurrent = greenPrevious + (greenDelta * currentStep);
    blueCurrent = bluePrevious + (blueDelta * currentStep);
    setColour(redCurrent, greenCurrent, blueCurrent, 6);
  
    Serial.println("R = " + String(redCurrent) + ", G = " + String(greenCurrent) + ", B = " + String(blueCurrent));

    currentStep++;
  }
}

void setup()
{
  // You have to change terminal speed to 11500 baud
  Serial.begin(115200);
  delay( 3000 ); // power-up safety delay
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  Blynk.begin(auth, ssid, pass);
  //Blynk.begin(auth, ssid, pass, "account.duckdns.org", 8080);

  // Change LED colour every LED_INTERVAL_MS (currently 100ms)
  LEDTimer.setInterval(LED_INTERVAL_MS, displayLED);
}

void loop()
{
  Blynk.run();
  LEDTimer.run();
}
3 Likes

Hi Khoih,
with the given informations from Pete about coms and delay of MIA, I assumed the “for” loop was far too long to execute. (and should be longer as it’s only a test mode… will take about 30 mins to completely go from deep blue to full bright white) and your comment proves me so.
Thanks for the provided code, i’ll test it as soon as I come home ( night shift @ work), will modify it as wished and come back to report it.
Many thanks anyway :slight_smile:

1 Like

Hi again,
Many thanks Khoih. Code works perfect using ZeRGBa. Now, only need to find a way to set all values and start the séquence auto instead of changing Colors manually.
Will dig it today :wink:

1 Like