Reading Bluetooth BLE data ArduinoBLE.h

Hello,
I have a simple sketch that uses the ArduinoBLE.h library. I have set up one ESP32 referred to as ESP32Sender to act as the device sending data and this appears to be working okay. I have set up two items of changing data as notify items on ESP32Sender. I can see it is sending out data as I can connect t it with my smartphone using the BLE Scanner app and see incoming data updating automatically. Please see screenshot.

I want to receive this data on another ESP32 referred to as ESP32Receiver. I can connect successfully to the ESP32Sender from ESP2Receiver. This is confirmed on the ESP32Sender both in serial monitor and the onboard led flashing as it sends out data.

What I cannot seem to manage is receive the data being sent out by ESP32Sender. I have tried for many hours to try and make this work but seem to be going round in circles.

I would be grateful if anybody could suggest a snippet of code that would allow me to receive the incoming data on ESP32Receiver. I just need to Serial.print it for now. I have marked a section in ESP32Receiver with //THIS IS WHERE UPDATED DATA IS RECEIVED as a place where the incoming data could go.

Code for both units is below and a screenshot from my smartphone is attached.

All help is greatly appreciated
Chazza

ESP32Sender code

#include <ArduinoBLE.h>

BLEService echoService("00000000-0000-1000-8000-00805f9b34fb");
BLEStringCharacteristic charac("741c12b9-e13c-4992-8a5e-fce46dec0bff", BLENotify, 40);
BLEStringCharacteristic cnt("741c12b9-e13c-4992-8a5e-fce46dec0bfe", BLENotify, 40);
BLEDescriptor Descriptor("beca6057-955c-4f8a-e1e3-56a1633f04b1", "Descriptor");
String var = "";


String animal[10] = { "cat", "dog", "bird", "mouse", "fox", "elephant", "lion", "tiger", "monkey", "giraffe" };

unsigned long previousMillis = 0;
const long interval = 500;  // interval
int count = 0;

int led = 2;

String animal[10] = { "cat", "dog", "bird", "mouse", "fox", "elephant", "lion", "tiger", "monkey", "giraffe" };


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

  if (!BLE.begin()) {
    Serial.println("starting BLE failed.");
    while (1)
      ;
  }


  BLE.setLocalName("ESP32Sender");
  BLE.setAdvertisedService(echoService);
  charac.addDescriptor(Descriptor);
  echoService.addCharacteristic(charac);
  echoService.addCharacteristic(cnt);
  BLE.addService(echoService);
  BLE.advertise();
  Serial.println("Bluetooth device active, waiting for connections...");
  Serial.println(" ");

  pinMode(led, OUTPUT);
}

void loop() {
  BLEDevice central = BLE.central();
  if (central) {
    Serial.println("* Connected to central device!");
    Serial.print("Connected to central : ");
    Serial.println(central.address());
    Serial.println(" ");

    while (central.connected()) {
      unsigned long currentMillis = millis();
      if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;

        digitalWrite(led, (!digitalRead(led)));

        charac.writeValue(animal[count]);  // name of animal from array
        cnt.writeValue(String(count));     // index of animal from array
        Serial.print(animal[count]);
        Serial.print(", ");
        count = count + 1;
        if (count == 10) {
          count = 0;
          Serial.println();
        }
        delay(50);
      }


    }
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

ESP32Receiver Code

#include <ArduinoBLE.h>


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

    // begin initialization
    if (!BLE.begin()) { // Initializes the BLE Device
        Serial.println("starting BLE failed!");

        while (1);
    }

    Serial.println("BLE Central - Peripheral Explorer");

    // start scanning for peripherals that are advertising, returns true on success
    BLE.scan();
}

void loop() {
    // check if a peripheral has been discovered
    BLEDevice peripheral = BLE.available(); //BLEDevice object named peripherical created. Query for a discovered BLE device that was found during the scan

    if (peripheral) {
        // discovered a peripheral, print out address, local name, and advertised service
        Serial.print("Found ");
        Serial.print(peripheral.address());
        Serial.print(" '");
        Serial.print(peripheral.localName());
        Serial.print("' ");
        Serial.print(peripheral.advertisedServiceUuid());
        Serial.println();

        // see if peripheral is a POD-2B_SN2981
        if (peripheral.localName() == "ESP32Sender") {
            // stop scanning
            BLE.stopScan();
            // we call this to get all the parameters from the peripheral
            explorerPeripheral(peripheral);

            // peripheral disconnected, we are done
            while (1) {
                // do nothing
            }
        }
    }
}


//initial connection to peripheral object
void explorerPeripheral(BLEDevice peripheral) {
    // connect to the peripheral
    Serial.println("Connecting ...");

    if (peripheral.connect()) {
        Serial.println("Connected");
    }
    else {
        Serial.println("Failed to connect!");
        return;
    }
    // discover peripheral attributes
    Serial.println("Discovering attributes ...");
    //discover attributes is a percurosr that is needed before servicecount I guess?
    if (peripheral.discoverAttributes()) {
        Serial.println("Attributes discovered");
    }
    else {
        Serial.println("Attribute discovery failed!");
        peripheral.disconnect();
        return;
    }
    int serviceCount = peripheral.serviceCount();

    Serial.print(serviceCount);
    Serial.println(" services discovered");
    BLEService tempService = peripheral.service("00000000-0000-1000-8000-00805f9b34fb");

    if (!tempService) {
        Serial.println("Service not found!");
        //  peripheral.disconnect();
      
    }
    else {
        Serial.println("Service found!");
    }


    exploreService(tempService);
}


void exploreService(BLEService service) {
    Serial.println("Exploring Service");

    BLECharacteristic simpleKeyCharacteristic = service.characteristic("741c12b9-e13c-4992-8a5e-fce46dec0bff");


    Serial.println("Subscribing to simple key characteristic ...");
    
    if (!simpleKeyCharacteristic) {
        Serial.println("no simple key characteristic found!");
      //  peripheral.disconnect();
        return;
    }
    else if (!simpleKeyCharacteristic.canSubscribe()) {
        Serial.println("simple key characteristic is not subscribable!");
      //  peripheral.disconnect();
        return;
    }
    else if (!simpleKeyCharacteristic.subscribe()) {
        Serial.println("subscription failed!");
     //   peripheral.disconnect();
        return;
    }
    else {
        
        Serial.println("Subscribed!");
      while(-1) {

//check if value has been updated 
  if (simpleKeyCharacteristic.valueUpdated()) {
    //THIS IS WHERE UPDATED DATA IS RECEIVED

  }

        
      }
       
    }


}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.