Can proceed to loop() without BLE connection?

Hello,

Board is Arduino MKR GSM 1400. To present the matter a simple sketch is enough

#include <BlynkSimpleSerialBLE.h>
#define BLELink Serial1

auth[] = "xxxxxxxxxxxxxxxxxxxx"

setup()
{
Serial.begin(115200);
  while (!Serial) {
    ;
  }

BLELink.begin(115200);
Blynk.begin(BLELink, auth);
Serial.println("Waiting for connections...");
}

loop()
{
Blynk.run();

some code..
}

Problem is here: Blynk.begin(BLELink, auth);

If not previously connected to BLE (HM-10) it won’t pass any further beyond the point. Even trying to connect after sketch run, it is not possible to push the sketch to go. It will connect to BLE device but sketch is frozen at the above mentioned point.

I would ask for some clarification from Blynk developers is it some sort of intended behavior by some reason or I miss something?

What I would like to achieve is ability to allow running sketch if no Blynk app is connected and be able to connect with Blynk app any time. Thanks.

Blynk.begin is a blocking function. Your code won’t progress beyond Blynk.begin unless a connection is established.
With Wi-Fi connections there is the option to use Blynk.config and Blynk.connect instead - which aren’t permanently blocking functions, but I don’t think this works with BLE (although I might be wrong).

You could take a look at the libraries posted by @khoih, as I think he’s found a way to overcome this issue.

Pete.

1 Like

Thanks for reply. I tried looking for the lib but did not find any that could help me resolve the issue for Arduino. Can you recall where it was posted?

Anyway, one thing makes me wonder. Current issue looks as follows:

  • Run sketch (from compiler)
  • It reaches the Blynk.begin(BLELink, auth); line
  • attempting to connect with phone
  • stuck, can’t connect

However, if

  • connect to BLE device first
  • run sketch (also from compiler)
  • reach the Blynk.begin(BLELink, auth);
  • it passes trough and goes to loop()

It means I must be connected first, once it reaches Blynk.begin(BLELink, auth); line and phone was not connected to BLE device there is no way to connect, stuck. Must restart sketch.

Now, when I try to run the device itself (without USB connected) with loaded sketch then first scenario applies.

Should I consider it as possible bug?

The BLE is so unstable I am scared to say something is working but seems solutions was easy.

Serial1.begin(baudrate);
  while(!Serial1) {
    ;
  }
   
  Blynk.config(Serial1, auth);

I also modified Blynk lib to check connection status making it able to connect any time in the loop().

I will post it here later, now it is bit messy of digging.

A little new to the community, so bare with me, but I recently did a bunch of testing with a HC-05 Bluetooth module. While it’s not BLE, it uses the same BlynkSimpleSerialBLE.h

Are you possibly missing #define BLYNK_USE_DIRECT_CONNECT?

#define BLYNK_USE_DIRECT_CONNECT
#include <BlynkSimpleSerialBLE.h>
#define SerialBLE  Serial1

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

void setup() {
  SerialBLE.begin(115200);
  Blynk.begin(SerialBLE, auth);
}

void loop() {
  Blynk.run();
  // don't do complex stuff here, use timers instead
}

I think that would at least solve the problem of it connecting if not previously connected, as it should call conn.connect(); in the source.

#ifdef BLYNK_USE_DIRECT_CONNECT
        if (!tconn)
            conn.connect();
#else

However it doesn’t appear that it will fix your original goal of running the loop if not connected.

I’m definitely interested in your connection status check/reconnect, so I’m subscribing.

1 Like

I had direct connect defined. Seems I did not include it in the example sketch as I typed it via forum editor. My original project is pretty large so I decided to type short example where I missed the BLYNK_USE_DIRECT_CONNECT.

Not sure why, have no time to investigate at present but Blynk.begin(Serial1, auth); makes me stuck always.

What I did within setup() is:

Blynk.config(Serial1, auth);                            
Blynk.connect();  

The loop() has Blynk.run();. I also decreased a timeout from 6000 ms to 3000 ms within read function. Now if I won’t connect while in setup() it passes to loop() and additionally allows me to connect any time while being in loop() already.

With the last thing there is little issue as upon connection it makes ~2000 ms delay. I must look how to make it 1000 ms or less or even async.

Ah interesting, I suppose a simple reconnect could be handled like this then:

#define BLYNK_USE_DIRECT_CONNECT
#include <BlynkSimpleSerialBLE.h>
#define SerialBLE Serial1
BlynkTimer timer;

char auth[] = "*****";
bool connectedToBlynk = false;

void checkConnection() {
  if (Blynk.connected() != connectedToBlynk) {
    // connection state has changed
    connectedToBlynk = Blynk.connected();
    if (!connectedToBlynk) { 
      // not connected
      Blynk.connect();
    } else {
      // (re)connected
    }
  }
}

void setup() {
  SerialBLE.begin(115200);
  Blynk.config(SerialBLE, auth);                            
  Blynk.connect();  
  timer.setInterval(1000L, checkConnection);
}

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

I don’t have issues with the HC-05 and Blynk.begin(SerialBLE, auth); however I like this non-blocking alternative to allow things to keep running.

How to force Connecticut by app itself? :smiley:

The devs can correct me if I’m wrong, but as far as I’m aware and from my experience, the app should try connecting when hitting the play button in the upper right.

  1. Stop app and add bluetooth/ble widget
  2. Click widget while app is stopped
  3. Press “Connect Bluetooth Device”, and select your device. (You may need to pair the device in phone settings first). This setting appears to save across app reopens, and only needs to be done once.
  4. Click play button to start app again, this should automatically try to connect to the device you selected in the widget.

Starting and stopping the app with the play/stop button?

The fact that you’ve gotten this far already, I’m assuming you already knew all of that. I’m guessing if that isn’t working for you, this might be handled differently for BLE. I only have the HC-05 on hand so I can’t be sure.

Thanks for wide explanation. Indeed, I got this far. I was thinking about more automated solution, without involving my fingers :smiley:

this might be handled differently for BLE

Same. The main problematic thing for BLE is pairing. I paired my BLE with the phone and then Blynk could not see BLE device anymore, started to treat it as BT (indeed, device is visible under BT widget).

Wait I’m a little confused here. If the arduino is successfully handling connecting/reconnecting (no fingers required :wink:) while the app is running, what isn’t working for you?

Ah just saw your edit, I’m at loss then. Not sure if I can do much more here without a BLE module to test.

Maybe this is more of a “Why BLE vs BT?” thing. My understanding is BLE wasn’t really designed for large amounts of data transfer or a constant connection (like a app controlling a remote device for long periods of time). It’s more for sending small chunks and putting the module to sleep, thus making it more power efficient or “low energy”. It seems like BLE isn’t really the best option for this use case.

The HM-10 does have an AT command for preventing it from going to sleep: AT+PWRM1
I suppose this could be used, then AT+PWRM0 could be sent if/when you don’t need the constant connection. But that kind of negates the point of using a BLE module if it will always be in it’s powered on mode.

1 Like