Blynk Disconnecting when entering functions

I am currently working on a Walkway floor WS2812 led strip project that is motion and wifi controlled. My problem right now is that it continues to disconnect when it calls functions that take a little more time to go through. I have searched through the forums and couldn’t find anything that helped fix it.

I am using an Arduino Mega and ESP 8266 wifi module

I am pretty sure it involves blynk being ran enough and it is timing out but I can’t figure out how to “reset” the “connection timer” (not sure how to phrase that) inside of a function.

The two functions that are causing issues are Rainbow (the for statements take some time to process as it is one inside of another) and ledClear (it will clear the led strip, then disconnect afterwards).

Edit: This is my first real Arduino project and I shoot big so I decided to use Blynk alongside it. I understand that there will be a lot of issues along the way and I have been patient with them but this one has continuously halted my progress.

//Library Setup
#include <BlynkSimpleShieldEsp8266.h>
#include <Adafruit_NeoPixel.h>

//Blynk Related Definitions, Variables, and Setups
char auth[] = "BwcgSJY_PNZSFHjnddyvZLX2zIoCkpoS";
char ssid[] = "Wireless 2.4Ghz";
char pass[] = "dozerhank";
#define BLYNK_PRINT Serial
#define EspSerial Serial1
ESP8266 wifi(&EspSerial);
#define ESP8266_BAUD 9600
BlynkTimer timer;

//LED Related Definitions, Variables, and Setups
#define ledCount 417
#define ledPin 5
int Duration = 10;
int Brightness = 50;
int ledTimer = 10;
Adafruit_NeoPixel strip(ledCount, ledPin, NEO_GRB + NEO_KHZ800);
int Red = 255;
int Green = 0;
int Blue = 0;
int Mode = 1;

//IR Motion Sensor Setup
#define sensorPin 7
#define indicator 13
int MotionSensor = 0; 


void setup() {
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  timer.setInterval(1000L, timerFunction);
  strip.begin();
  strip.fill(strip.Color(Red, Green, Blue), 0, ledCount);
  strip.setBrightness(Brightness);
  strip.show();
  pinMode(sensorPin, INPUT);
  pinMode(indicator, OUTPUT);
  Serial.println("Program Ready!");
}

void loop() {
  Blynk.run();
  timer.run();
}

void timerFunction()  {
  if (MotionSensor == 1)  {
    MotionDetect();
  }
}

//Adjusting the Mode
BLYNK_WRITE(V0) {
  switch(param[0].asInt())  {
    case 1:
      Mode = 1;
      strip.fill(strip.Color(Red, Green, Blue), 0, ledCount);
      strip.show();
      break;
    case 2:
      Mode = 2;
      Rainbow();
      break;
    case 3:
      Mode = 3;
      Fade();
      break;
  }
}

//Adjusting Color Values
BLYNK_WRITE(V1) {
  Red = param[0].asInt();
  if (Mode == 1)  {
    strip.fill(strip.Color(Red, Green, Blue), 0, ledCount);
    strip.show();
  }
}
BLYNK_WRITE(V2) {
  Green = param[0].asInt();
  if (Mode == 1)  {
    strip.fill(strip.Color(Red, Green, Blue), 0, ledCount);
    strip.show();
  }
}
BLYNK_WRITE(V3) {
  Blue = param[0].asInt();
  if (Mode == 1)  {
    strip.fill(strip.Color(Red, Green, Blue), 0, ledCount);
    strip.show();
  }
}

//Adjusting Brightness Value
BLYNK_WRITE(V4) {
  Brightness = param[0].asInt();
  strip.setBrightness(Brightness);
  strip.show();
}

//Adjusting MotionSensor Value
BLYNK_WRITE(V6) {
  MotionSensor = param[0].asInt();
  if (MotionSensor == 1)  {
    Serial.println("Motion Sensor On!");
  }
}

void MotionDetect() {
  if (digitalRead(sensorPin) > 0) {
    Serial.println("Somebody is in the room!");
    if (ledTimer == 0)  {
      ledTimer = Duration;
      Serial.println(ledTimer);
      switch(Mode)  {
        case 1:
          FillRoom();
          break;
        case 2:
          Rainbow();
          break;
        case 3:
          Fade();
          break;
      }
    } else if (ledTimer > 0)  {
      ledTimer = Duration;
    }
  } else {
    ledTimer--;
    if (ledTimer == -1) {
      ledTimer = 0;
    }
    if (ledTimer == 0)  {
      ledClear();
    }
  }
 }

void Rainbow()  {
  /*
  Serial.println("Rainbow Mode On");
  for (long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
    for(int i = 0; i < strip.numPixels(); i++)  {
      int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
      Blynk.run();
      if (Mode != 2)  {
        Serial.println("Mode Changed!");
        return;
      }
    }
    Blynk.run();
    strip.show();
    if (Mode != 2)  {
        Serial.println("Mode Changed!");
        return;
      }
  }  
  */
}

void Fade() {
  
}

void FillRoom() {
  for (int i = 0; i > ledCount + 1; i++)  {
    strip.setPixelColor(i, strip.Color(Red, Green, Blue));
    strip.show();
  }
}

void ledClear() {
  for (int i = ledCount; i > -1; i--) {
    strip.setPixelColor(i, strip.Color(0, 0, 0));
    strip.show();
  }
}

The simple solution is to add Blynk.run(); immediately before or after strip.show(); in your offending functions.

If the LED strip functions are acceptably smooth with this approach then that’s all you need to do. The alternative is to increase the Blynk timeout setting, but there are disadvantages to that approach.

Pete.

Thanks Pete! Will try that in the morning. I think I have tried that before and I don’t believe it had worked though, but that gave me another idea involving rearranging some code and adding the Blynk.run() as well.

Could you explain the Blynk timeout settings briefly? I have looked into it a bit but haven’t been able to comprehend it quite yet

Search the forum for “#define BLYNK_HEARTBEAT”

Pete.

Copy that

And just for the sake of documenting the issue and the solution, here is how I fixed this:
The fix to this was adding a variable holding the status of the led strip (0 for off, 1 for on). After clearing the led strip it is set to 0 and another parameter was added before calling ledClear() requiring ledStatus to be 1. Before this, the function ledClear() was continuously called causing Blynk to disconnect from timeout.

1 Like