BLE buffer flush problem in Android app

I am working on a Blynk controlled motor vehicle with 2 DC motors, line followers, LED’s, buzzer controlled by Android Blynk app with sliders, joystick, button using BLE (HM-10) and basically it works. But … I found one issue where the virtualWrite seems to be buffered somewhere (I guess in the app); I isolated the problem, and here you can find some source code to reproduce the problem.

Setup:

  • Arduino nano
  • HM-10 module on pin 11,12
  • In Blynk app a LED-widget or a value-widget connected to the virtual port 5.

When you run the code, you will see the LED-widget or value-widget change between on(255) and off(0). And the debug output looks perfect. Only … it is totally wrong because the 2 values are just the opposite as they should be (the LED-widget goes on when it should be off and vice versa). The problem is that the commands are buffered somewhere. I guess in the app because the debug output looks perfect and on time. If you execute the led1.on/off two times, everything works perfect - that’s a workaround.

Here’s the code to reproduce the bug:

#define BLYNK_DEBUG
#define BLYNK_PRINT Serial

#define BLYNK_USE_DIRECT_CONNECT

#include <BlynkSimpleSerialBLE.h>

#include <SoftwareSerial.h>
#include <WidgetLED.h>
#include <SimpleTimer.h>

char auth[] = "YourAuthToken";

SoftwareSerial SerialBLE(11, 12); //  RX, TXconnected to HM-10
WidgetLED led1(V5);
SimpleTimer timer;

void setup()
{
  Serial.begin(9600);
  SerialBLE.begin(9600);        // Set Serial baud rate
  Blynk.begin(auth, SerialBLE);

  while (Blynk.connect() == false) {
  // Wait until connected
  }

  timer.setInterval(1000L, blinkLedWidget);
  }

void blinkLedWidget()
{
  if (led1.getValue()) {
    led1.off();
//    led1.off();
    Serial.println("LED on V1: off");
  } else {
    led1.on();
//    led1.on();
    Serial.println("LED on V1: on");
  }
}

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

And the debug output is

[0] Blynk v0.3.7 on Arduino Nano
[0] Connecting…
[6000] Connecting…
[12000] Connecting…
[18000] Connecting…
[23298] >[02|00|01|00]
[23349] >8caa28e4fe6b4cb78f317a0cc99b6cd4
[23350] Ready
[23350] <[00|00|01|00|C8]
[24415] <[14|00|01|00|08]vw[00]5[00]255
LED on V1: on
[25415] <[14|00|02|00|06]vw[00]5[00]0
LED on V1: off
[26415] <[14|00|03|00|08]vw[00]5[00]255
LED on V1: on
[27203] >[14|00|02|00|04]
[27207] >vr[00]5
[27415] <[14|00|04|00|06]vw[00]5[00]0
LED on V1: off
[28217] >[14|00|03|00|04]
[28222] >vr[00]5
[28416] <[14|00|05|00|08]vw[00]5[00]255
LED on V1: on
[29231] >[14|00|04|00|04]
[29236] >vr[00]5
[29415] <[14|00|06|00|06]vw[00]5[00]0

Could it be that the timer is confusing you:

timer.setInterval(1000L, blinkLedWidget);

This means that there could be up to 1 second delay between pressing the button and the light going on which might be confusing things. Try reducing this number (or rewrite your code so it doesn’t need a timer at all).

Hi,

Sorry to tell, but the timer is confusing you instead of me :wink: In my original code I dont’t use a timer, but I have a line follower module connected to the Arduino; use digitalRead where I do a virtualWrite to send the value to the app, when the value changes. I tried to make a program to reproduce the problem without the need of connecting wires/buttons/resistors to the Arduino, so I use a timer.
I use a long time (1 sec) so that you have the time to see the bug, when everything is fast it is not possible to see the bug. The idea is to compare what you see on the Serial monitor with the value in the app, and then it’s clear that always the “before the last” value is shown in the app instead of the last value.