Odd latency behaviour with multiple LED widgets

Hello All!

I’m having some odd latency issues with a Bluetooth LE connected stepper motor controller. With a single button widget and a single LED widget there is a latency of 10-12 seconds between button press and the led changing state.

However, if I add a display widget, one will have a long latency and the other virtually none.

This happens almost 100% of the time and I have no problem reproducing it

The hardware and libraries:
Nexus 6 with Android 7.0 installed
(I tried with a Nexus 7 and Android 6, but BLE widget won’t connect)
Sparkfun RedBoard (Uno clone)
Adafruit BlueFruit LE Shield
Big Easy Driver (for the stepper motor)
Blynk library v0.3.10

Below is a snippet that demonstrates what I’m doing. It has one button widget that toggles one LED widget on and off.

To start with one button and one LED. The LED widget has a latency of over 10 seconds.

BLYNK_WRITE(V0)
{
  int p = param.asInt();
  int l1 = led1.getValue();
  BLYNK_LOG("Button: %i -- LED1: %i", p, l1);

  if (p)
  {
    led1.on();
  }
  else if (!p)
  {
    led1.off();
  }
}

Now here’s the odd part. If I add a second LED Widget, both toggled with the same button. The first LED switches with a very low latency, but the second LED will now take 10-12 seconds.

BLYNK_WRITE(V0)
{
  int p = param.asInt();
  int l1 = led1.getValue();
  int l2 = led2.getValue();
  BLYNK_LOG("Button: %i -- LED1: %i -- LED2: %i", p, l1, l2);

  if (p)
  {
    led1.on(); // quick response
    led2.on(); // long latency
  }
  else if (!p)
  {
    led1.off(); // quick response
    led2.off(); // long latency
  }
}

If I swap the position of the LEDs in the code, the LED with the long latency is swapped.

  if (p)
  {
  led2.on(); // quick response
  led1.on(); // long latency
}
else if (!p)
{
  led2.off(); // quick response
  led1.off(); // long latency
}

If I then delete the “bottom” LED widget from the Blynk dashboard, but leave the line in the code there is still very little latency with the remaining LED.

  if (p)
  {
  led2.on(); // quick response
  led1.on(); // delete this widget but leave this line in the code
}
else if (!p)
{
  led2.off(); // quick response
  led1.off(); // // delete this widget but leave this line in the code
}

At this point I start spinning around in circles, because with just about any
combination of changing position in the, code or swapping virtual pin numbers
in the the result is predictable with one time the LED widget responding quickly
and the other taking about 12 seconds.

So what’s going on? Any ideas?

Below is the entire sketch

/////////////////////////////////////
//
// Simple example of Blynk, debug, and BLE
//
// V0 Button Widget
// V1 LED Widget
// V2 LED Widget

#define BLYNK_DEBUG        // Optional, this enables more detailed prints
#define BLYNK_PRINT Serial // Defines the object that is used for printing

#define BLYNK_USE_DIRECT_CONNECT // If trouble connecting trying commenting out

#include <SPI.h>
#include <SoftwareSerial.h>
#include <BlynkSimpleSerialBLE.h>

// Adafruit Bluefruit LE Shield using SPI
#include <Adafruit_BLE.h>
#include <Adafruit_BluefruitLE_SPI.h>

#define BLUEFRUIT_SPI_CS 8
#define BLUEFRUIT_SPI_IRQ 7
#define BLUEFRUIT_SPI_RST 4
#define BLUEFRUIT_VERBOSE_MODE false

Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

WidgetLED led1(V1);
WidgetLED led2(V2);


char auth[] = "xxxxxxxxxxxxxxxxxxxxx";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ble);
  ble.begin(BLUEFRUIT_VERBOSE_MODE);
  ble.factoryReset(); // Optional
  ble.setMode(BLUEFRUIT_MODE_DATA);
  // while (! ble.isConnected()) {
  //      delay(500);
  //  }
  led1.off();
  led2.off();
}

void loop()
{
  Blynk.run(); // All the Blynk Magic happens here...
}

BLYNK_WRITE(V0)
{
  int p = param.asInt();
  int l1 = led1.getValue();
  int l2 = led2.getValue();
  BLYNK_LOG("Button: %i -- LED1: %i -- LED2: %i", p, l1, l2);

  if (p)
  {
  led1.on();
  led2.on();
  }
  else if (!p)
  {
  led1.off();
  led2.off();
  }
}

Thanks for your report,

BLE is still in beta, due to some issues with it’s implementation. Seems we have the hardware you are using, so we’ll try to reproduce it.

@digver we just released a new build, which contains some fixes for BLE work - please check it.

No sorry, @BlynkAndroidDev it’s still the same.

It’s still happening almost, but not every time. One scenario is:

  1. run the project and it has the long latency for the last led in the code.
  2. l get a message in the project saying “Sorry can’t connect to server, please try again later”
  3. restart the project and the latency is gone

Out of curiosity I expended the LEDs to six.

if (p)
  {
    led1.on();
    led2.on();
    led3.on();
    led4.on();
    led5.on();
    led6.on();    // long latency
  }
  else if (!p)
  {
    led1.off();
    led2.off();
    led3.off();
    led4.off();
    led5.off();
    led6.off();    // long latency
  }

The first five turn off in sequence in less than a second. The last led6 takes a total of about 12 seconds to change state.

And the same as before if I delete the LED widget for led6, but leave the line in the code – the remaining 5 LED Widgets change state in 1 second, maybe less.

Could you provide your setup of led widget? What pin and update frequency? I could provide you with some test build which will log the received messages, so we could understand on what side (mobile/hardware) this issue arises.

@vshymanskyy could you check this issue?

@BlynkAndroidDev the complete sketch is in the OP. Are these the parts you are looking for? Sorry if I misunderstand.

WidgetLED led1(V1);
WidgetLED led2(V2);
WidgetLED led3(V3);
WidgetLED led4(V4);
WidgetLED led5(V5);
WidgetLED led6(V6);

BLYNK_WRITE(V0) // Button Widget with mode set as switch
{
  int p = param.asInt();

  if (p)
  {
    led1.on();
    led2.on();
    led3.on();
    led4.on();
    led5.on();
    led6.on();
  }
  else if (!p)
  {
    led1.off();
    led2.off();
    led3.off();
    led4.off();
    led5.off();
    led6.off();
  }

Only one LED is needed to show the long latency. Two will show the effect of the first being quick and the other long.

I’ve meant widget’s settings in the app

Not really much to it. Of course each led is connected to a different virtual pin and then named for it.

oh yeah, I’d be glad to load a test build on to troubleshoot this.

Please check the next build: https://www.dropbox.com/s/rgonu5cry0tzug3/main_app-log.apk?dl=0

Use it to reproduce this bug, it would be better if all other projects, except ble, would be not active - as it could create a large log.

To send the log to me - just make a long press on any screen’s title text and it would start sharing dialog - select there mail client and send to alexkipar@gmail.com (it should be prefilled, but who knows) the attached file

This build keeps crashing for me. I emailed you a log of it.

First, it crashes when I try to add a widget. Second, if I tap on a widget to set it up it crashes the app.

I get the dialog – “Blynk has stopped, open app again.” or “Blynk keeps stopping. Close app”

Yep, it’s a bug we just fixed. I’ll create for you a new build

please check this build

Thanks for your help with this, @BlynkAndroidDev

Just emailed you a log with the new build. About half way through it started working correctly, that is with all 6 leds having a quick response. Then after stopping and starting the project it returned to the last led on V6 having the very long latency.

If helpful I could remove all LEDs but one to show how just the one led has the very slow response.