07-06-2022, 05:46 PM
(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:tart() 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.