NodeMCU ESP12-E Disconnecting

Hello all. I’m currently having troubles with my NodeMCU ESP-12E automatically disconnecting after a while. What’s weird is that I can’t find a source for it’s disconnecting. If I open up my app every 5 minutes or so to check on it, it won’t turn off. But if I leave it on for an extended amount of time, lets say 30+ min then it’ll disconnect. The only way to connect it back is to turn it off then turn it back on.

As for my project, it’s basically just a remote-controlled LED light. I have two modes, color-choosing mode and rainbow mode. I’ve also implemented RGB sliders, an RGB color picker that updates those sliders, a brightness slider, and an on/off button. It also saves the previous chosen color, previous mode, and previous state in the EEPROM. So it can turn on as fast as possible without having to connect to the Blynk servers to grab the values.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <EEPROM.h>
#include <Adafruit_NeoPixel.h>

#define BLYNK_PRINT Serial
#define LED_PIN 4

char auth[] = "~removed for privacy~";

char ssid[] = "~removed for privacy~";
char pass[] = "~removed for privacy~";

int currentMode;
int currentStatus;
int rgb[3];
int brightness;

unsigned long lastRainbow = 0;
uint16_t rainbowJ;
unsigned long lastPrint = 0;
int rainbowI = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(79, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  EEPROM.begin(6);
  
  currentMode = EEPROM.read(0);
  currentStatus = EEPROM.read(1);
  rgb[0] = EEPROM.read(2);
  rgb[1] = EEPROM.read(3);
  rgb[2] = EEPROM.read(4);
  brightness = EEPROM.read(5);
  
  strip.begin();
  
  updateLEDs();
  
  Blynk.begin(auth, ssid, pass);
}

void eepromWrite(int addr, int val) {
  EEPROM.write(addr, val);
  EEPROM.commit();
}

void loop() {
  Blynk.run();

  updateLEDs();
}

void updateLEDs() {
  if(currentStatus == 1) { //ON
    if(currentMode == 1) { //COLOR
      for(int i=0; i< strip.numPixels(); i++)
        strip.setPixelColor(i, rgb[0] * brightness / 100, rgb[1] * brightness / 100, rgb[2] * brightness / 100);
    } else if(currentMode == 2) { //RAINBOW
      if(lastRainbow == 0)
        lastRainbow = millis();

      if(millis() - lastRainbow >= 20) {
        lastRainbow = millis();
        rainbowJ++;
      }
      
      for(int i=0; i< strip.numPixels(); i++)
        strip.setPixelColor(i, Wheel(((256 / strip.numPixels()) + rainbowJ) & 255));

      if(rainbowJ == 1279)
        rainbowJ = 0;

        rainbowI++;
        if(rainbowI >= strip.numPixels())
          rainbowI = 0;
    }
  } else { //OFF
    for(int i = 0; i < strip.numPixels(); i++)
      strip.setPixelColor(i, 0, 0, 0);
  }

  strip.show();
}

void setMode(byte val) {
  currentMode = val;
  eepromWrite(0, val);
}

void setStatus(byte val) {
  currentStatus = val;
  eepromWrite(1, val);
}

void setRGB(byte vals[]) {
  for(int i = 0; i < 3; i++)
    rgb[i] = vals[i];
  eepromWrite(2, vals[0]); //RED
  eepromWrite(3, vals[1]); //GREEN
  eepromWrite(4, vals[2]); //BLUE
}

void setBrightnessLevel(byte val) {
  brightness = val;
  eepromWrite(5, val);
}

BLYNK_WRITE(V0) {
  setMode(param.asInt());
}

BLYNK_WRITE(V1) {
  setStatus(param.asInt());
}

BLYNK_WRITE(V2) {
  byte vals[3];
  vals[0] = param[0].asInt();
  vals[1] = param[1].asInt();
  vals[2] = param[2].asInt();
  Blynk.virtualWrite(V4, vals[0]);
  Blynk.virtualWrite(V5, vals[1]);
  Blynk.virtualWrite(V6, vals[2]);
  setRGB(vals);
}

BLYNK_WRITE(V3) {
  setBrightnessLevel(param.asInt());
}

BLYNK_WRITE(V4) {
  byte vals[3];
  vals[0] = param.asInt();
  vals[1] = rgb[1];
  vals[2] = rgb[2];
  setRGB(vals);
}

BLYNK_WRITE(V5) {
  byte vals[3];
  vals[0] = rgb[0];
  vals[1] = param.asInt();
  vals[2] = rgb[2];
  setRGB(vals);
}

BLYNK_WRITE(V6) {
  byte vals[3];
  vals[0] = rgb[0];
  vals[1] = rgb[1];
  vals[2] = param.asInt();
  setRGB(vals);
}

uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color((255 - WheelPos * 3) * brightness / 100 , 0, (WheelPos * 3) * brightness / 100);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, (WheelPos * 3) * brightness / 100, (255 - WheelPos * 3) * brightness / 100);
  }
  WheelPos -= 170;
  return strip.Color((WheelPos * 3) * brightness / 100, (255 - WheelPos * 3) * brightness / 100, 0);
}

I fixed your code formatting… the correct key is the backtick:

As for your issue, firstly, using the EEPROM so much, so frequently might end up damaging it over time. The last value of a variable is retained in memory as long as the hardware sketch is running.

Accessing the server is required to keep the hardware online…

This is just a guess, but when you pop into the app, you probably also poke a button or so, and that breaks out of the updateLEDs() loop enough to restore synchronicity with the server… leaving it running that loop over time might be the reason for the disconnection. Try placing another Blynk.run(); command somewhere in that loop… experiment with best placement.

http://docs.blynk.cc/#blynk-firmware-connection-management-blynkrun

Also, here is a recent and possibly similar LED control topic that has had disconnections resolved via timer use:

https://community.blynk.cc/t/solved-nodemcu-connection-drop-with-led-strip-animation/13936/3