phyphox Forums

Full Version: How to disconnect BLE when experiment finished?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I can connect my Phyphox with ESP32 and plot my results now, but when I quit the experiment, ESP32 can not be found by other app or Phypgox again. I will have to reboot the ESP32 to make it avaiable for Phyphox. It seems a "disconnect BLE" command missed before quit form the experiment. What should I do to avoid the problem?
I am aware that phyphox does not immediately release the BLE connection. You need to leave the experiment, so that another device could connect to the ESP. Alternatively, you could close phyphox, i.e., this typically means: remove it from the main memory.

If this does not help: could you please share your sketch. There might be an issue there then.
Thank you for your kind reply.

I did close Phyphox, but still not able to connect the ESP32 again unless I reboot the ESP32.

I used the following code for my ESP32:

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

void setup() {
  Serial.begin(115200);
  BLEDevice::init("ESP32");

  // Create the BLE Server
  pServer = BLEDevice::createServer();

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic:TongueROPERTY_NOTIFY
                    );

  // Start the service
  pService->start();

  // Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  BLEDevice:ConfusedtartAdvertising();
}

void loop() {
    // notify changed value
      int ran = random(0,11); //
      Serial.println(ran);
      pCharacteristic->setValue(ran); //
      pCharacteristic->notify();
      delay(100);
}
Thanks. We check…

Is there a specific reason why you do not utilise https://phyphox.org/arduino/?
(07-05-2022, 05:17 PM)Jens Noritzsch Wrote: [ -> ]Thanks. We check…

Is there a specific reason why you do not utilise https://phyphox.org/arduino/?

Thanks.

Phyphox Arduino library is good enough. But I am a very beginner of coding,  it seems a little complicated for me. Using a general BLE library and Phyphox web editor seems much suitable for me.
Hey,

just testet your code. The problem is that the ble server stops advertising once a connection is established. After the connection is canceld, you have to start advertising once again to allow new connections. The code below solves this issue:
Code:
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

class MyServerCallbacks: public BLEServerCallbacks {
    void onDisconnect(BLEServer* pServer) {
      BLEDevice::startAdvertising();
    }
};

void setup() {
  Serial.begin(115200);
  BLEDevice::init("ESP32");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_NOTIFY
                    );

  // Start the service
  pService->start();

  // Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  BLEDevice::startAdvertising();
}

void loop() {
    // notify changed value
      int ran = random(0,11); //
      Serial.println(ran);
      pCharacteristic->setValue(ran); //
      pCharacteristic->notify();
      delay(100);
}


Quote:Phyphox Arduino library is good enough. But I am a very beginner of coding,  it seems a little complicated for me. Using a general BLE library and Phyphox web editor seems much suitable for me.


hm ok, the basic idea behind the arduino library is that the user does not have to worry about the bluetooth low energy connection anymore. We think this would help beginners to focus on the code of the measurement setup itself.

Cheers Dominik
(07-06-2022, 09:46 AM)Dominik Dorsel Wrote: [ -> ]Hey,

just testet your code. The problem is that the ble server stops advertising once a connection is established. After the connection is canceld, you have to start advertising once again to allow new connections. The code below solves this issue:
Code:
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

class MyServerCallbacks: public BLEServerCallbacks {
    void onDisconnect(BLEServer* pServer) {
      BLEDevice::startAdvertising();
    }
};

void setup() {
  Serial.begin(115200);
  BLEDevice::init("ESP32");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_NOTIFY
                    );

  // Start the service
  pService->start();

  // Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  BLEDevice::startAdvertising();
}

void loop() {
    // notify changed value
      int ran = random(0,11); //
      Serial.println(ran);
      pCharacteristic->setValue(ran); //
      pCharacteristic->notify();
      delay(100);
}


Quote:Phyphox Arduino library is good enough. But I am a very beginner of coding,  it seems a little complicated for me. Using a general BLE library and Phyphox web editor seems much suitable for me.


hm ok, the basic idea behind the arduino library is that the user does not have to worry about the bluetooth low energy connection anymore. We think this would help beginners to focus on the code of the measurement setup itself.

Cheers Dominik

Thanks a lot, it did solve my problem.

About Phyphox Arduino library, can it be use with  Phyphox web editor? Because I don't want to make too many changes to my Arduino files.  If only PhyphoxBLE:Confusedtart() and PhyphoxBLE::write() needed, other works, such as read the data and plot them, can be implemented by Phyphox web editor, will make my work easier. 
  
Anyway, you've helped me a lot, thank you very much.
You are welcome.

(07-06-2022, 05:46 PM)donghaipark Wrote: [ -> ]About Phyphox Arduino library, can it be use with  Phyphox web editor? Because I don't want to make too many changes to my Arduino files.  If only PhyphoxBLE:Confusedtart() and PhyphoxBLE::write() needed, other works, such as read the data and plot them, can be implemented by Phyphox web editor, will make my work easier.
Sure. You will find the UUIDs in the library source or by connecting once to your device in order to receive a minimum .phyphox file containing these.
Great! Are there some detailed docments or videos can help me about that?
(07-07-2022, 03:42 AM)donghaipark Wrote: [ -> ]Great! Are there some detailed docments or videos can help me about that?

I have resolved the problem. The service UUID can be leaved blank.
Cheers!