BLE client connection problems

Kording
Posts: 7
Joined: Wed Jul 25, 2018 9:01 pm

BLE client connection problems

Postby Kording » Wed Jul 25, 2018 9:45 pm

Hey folks,

I am trying to connect to a BLE device (Shimano SC-E8000). For now I just want to read a few characteristics to ensure I have a stable connection. Unfortunatly I can´t get it right.

Here is what I did:
Using the nRF Connect App I got the Service and characteristic I want to read out.
I am interested in the Service: Device Information: UUID 0x180A
and the Manufacturer Name String: UUID 0x2A29
result image

With this information I opened Niel Kolbans BLE Client example and changed the serviceUUID and charUUID to my discovered UUIDs in the following way:

Code: Select all

serviceUUID("0000180A-0000-1000-8000-00805f9b34fb");
charUUID("00002A29-0000-1000-8000-00805f9b34fb");
basicly I added "-0000-1000-8000-00805f9b34fb" to the UUID. I got this information from Andreas Spiess, Youtube, (min: 9:50)
I also tried using the short UUIDs like this with the same result:

Code: Select all

serviceUUID((uint16_t)0x180A);
charUUID((uint16_t)0x2A29);
Is this the correct way of writing the UUIDs or is this allready the mistake I am looking for?

Now when I run the sketch on the ESP32 this is the result:

Code: Select all

Starting Arduino BLE Client application...
BLE Advertised Device found: Name: SCE8000, Address: d7:ed:14:15:38:03, serviceUUID: 000018ff-5348-494d-414e-4f5f424c4500
I noticed the ESP32 discovers the device I am looking for but does not connect to it.I guess I messed something up with the UUIDs? Also the serial Monitor gives me the information of this new service UUID ": 000018ff-5348-494d-414e-4f5f424c4500"
So I tried putting this in the sketch instead of the 0x180A UUID. Here is the result:

Code: Select all

Starting Arduino BLE Client application...
BLE Advertised Device found: Name: SCE8000, Address: d7:ed:14:15:38:03, serviceUUID: 000018ff-5348-494d-414e-4f5f424c4500
Found our device!  address: Forming a connection to d7:ed:14:15:38:03
 - Created client
 - Connected to server
 - Found our service
abort() was called at PC 0x401810d3 on core 1

Backtrace: 0x40090c38:0x3ffd5ab0 0x40090e3b:0x3ffd5ad0 0x401810d3:0x3ffd5af0 0x4018111a:0x3ffd5b10 0x4016ebb3:0x3ffd5b30 0x400d4579:0x3ffd5b50 0x400d1733:0x3ffd5bb0 0x400d1932:0x3ffd5c00 0x40188c90:0x3ffd5c60

Rebooting...
Now the ESP32 can connect to the device and finds the service I am looking for but aborts after that. Why is that? Is the device protected? Also the BLE device is restarting after this connection attampt.

I would highly appreciate your help.
L.Kording
Last edited by Kording on Thu Jul 26, 2018 9:05 am, edited 1 time in total.

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: BLE client connection problems

Postby ESP_Sprite » Thu Jul 26, 2018 4:50 am

An 'abort' normally implies a programming error of some kind, and normally device communication by itself shouldn't generate that. Can you take the backtrace and run it through addr2line, or alternatively use 'make monitor' to view the serial output, to see where the crash happens?

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE client connection problems

Postby chegewara » Thu Jul 26, 2018 6:13 pm

This is good code:

Code: Select all

serviceUUID("0000180A-0000-1000-8000-00805f9b34fb");
charUUID("00002A29-0000-1000-8000-00805f9b34fb");
This is not:

Code: Select all

serviceUUID((uint16_t)0x180A);
charUUID((uint16_t)0x2A29);
You cant use uint to retrieve service or characteristic because there is no such function, but you can write code like this:

Code: Select all

charUUID((uint16_t)0x2A29);
....
pRemoteCharacteristic = pRemoteService->getCharacteristic(BLEUUID(charUUID));

Kording
Posts: 7
Joined: Wed Jul 25, 2018 9:01 pm

Re: BLE client connection problems

Postby Kording » Thu Jul 26, 2018 10:28 pm

@ESP_Sprite
I am not using the ESP IDF but the Arduino IDE. Unfortunatly I was not able to get the ESP IDF started on my PC. Is there a similar way for the Ardunio IDE to see where the crash happens?

@chegewara
Thanks for the explanation. This was helpful.

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: BLE client connection problems

Postby ESP_Sprite » Fri Jul 27, 2018 1:14 am

I think Arduino also has the addr2line utility somewhere.

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE client connection problems

Postby chegewara » Fri Jul 27, 2018 4:33 am


Kording
Posts: 7
Joined: Wed Jul 25, 2018 9:01 pm

Re: BLE client connection problems

Postby Kording » Fri Jul 27, 2018 11:25 am

Thank you for your quick replies. This is the result I am getting with the ESP exception decoder:

Code: Select all

Decoding stack results
0x40090c38: invoke_abort at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/esp32/panic.c line 140
0x40090e3b: abort at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/esp32/panic.c line 149
0x401810d3: __cxxabiv1::__terminate(void (*)()) at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_terminate.cc line 47
0x4018111a: std::terminate() at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_terminate.cc line 57
0x4016ebb3: __cxxabiv1::__cxa_throw(void*, std::type_info*, void (*)(void*)) at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_throw.cc line 87
0x400d4579: BLERemoteService::getCharacteristic(BLEUUID) at C:\Users\InfoLabor\Documents\Arduino\hardware\espressif\esp32\libraries\BLE\src\BLERemoteService.cpp line 156
0x400d1733: connectToServer(BLEAddress) at C:\Users\InfoLabor\Documents\Arduino\ESP32\BLE_client_Shimano/BLE_client_Shimano.ino line 51
0x400d1932: loop() at C:\Users\InfoLabor\Documents\Arduino\ESP32\BLE_client_Shimano/BLE_client_Shimano.ino line 114
0x40188c90: loopTask(void*) at C:\Users\InfoLabor\Documents\Arduino\hardware\espressif\esp32\cores\esp32\main.cpp line 17
The only thing I changed to Niel Kolbans Client sketch was this:

Code: Select all

// The remote service we wish to connect to.
static BLEUUID serviceUUID("000018ff-5348-494d-414e-4f5f424c4500");
// The characteristic of the remote service we are interested in.
static BLEUUID    charUUID("00002A29-5348-494d-414e-4f5f424c4500");
Does this mean I messed something up with the installation of the ESP library?
Furthermore I tried connecting to a different BLE device. Like before, the ESP finds the device but does not connect to it. It seams like it does not find the corresponding service UUID.

L.Kording

Kording
Posts: 7
Joined: Wed Jul 25, 2018 9:01 pm

Re: BLE client connection problems

Postby Kording » Tue Jul 31, 2018 2:19 pm

If it helps, I was able to form a connection and communicate with the ESP32 via this Bluetooth Terminal App. Does anyone know what the backtrace means or can point to a different solution for my issue?
Thanks in advance,

L.Kording

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE client connection problems

Postby chegewara » Wed Aug 01, 2018 1:40 am

Well, without code its hard to say, can only guessing you are trying to use BLERemoteService::getCharacteristic(BLEUUID) on empty remote service. Try to assert if service is not null before you try to getCharacteristic().

Kording
Posts: 7
Joined: Wed Jul 25, 2018 9:01 pm

Re: BLE client connection problems

Postby Kording » Wed Aug 01, 2018 4:39 am

The code was posted above as a link. Here is the complete code I uploaded to the ESP32:

Code: Select all

/**
 * A BLE client example that is rich in capabilities.
 */

#include "BLEDevice.h"
//#include "BLEScan.h"

// The remote service we wish to connect to.
static BLEUUID serviceUUID("000018ff-5348-494d-414e-4f5f424c4500");
// The characteristic of the remote service we are interested in.
static BLEUUID    charUUID("00002A29-5348-494d-414e-4f5f424c4500");

static BLEAddress *pServerAddress;
static boolean doConnect = false;
static boolean connected = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;

static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
    Serial.print("Notify callback for characteristic ");
    Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
    Serial.print(" of data length ");
    Serial.println(length);
}

bool connectToServer(BLEAddress pAddress) {
    Serial.print("Forming a connection to ");
    Serial.println(pAddress.toString().c_str());
    
    BLEClient*  pClient  = BLEDevice::createClient();
    Serial.println(" - Created client");

    // Connect to the remove BLE Server.
    pClient->connect(pAddress);
    Serial.println(" - Connected to server");

    // Obtain a reference to the service we are after in the remote BLE server.
    BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
    if (pRemoteService == nullptr) {
      Serial.print("Failed to find our service UUID: ");
      Serial.println(serviceUUID.toString().c_str());
      return false;
    }
    Serial.println(" - Found our service");


    // Obtain a reference to the characteristic in the service of the remote BLE server.
    pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
    if (pRemoteCharacteristic == nullptr) {
      Serial.print("Failed to find our characteristic UUID: ");
      Serial.println(charUUID.toString().c_str());
      return false;
    }
    Serial.println(" - Found our characteristic");

    // Read the value of the characteristic.
    std::string value = pRemoteCharacteristic->readValue();
    Serial.print("The characteristic value was: ");
    Serial.println(value.c_str());

    pRemoteCharacteristic->registerForNotify(notifyCallback);
}
/**
 * Scan for BLE servers and find the first one that advertises the service we are looking for.
 */
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
 /**
   * Called for each advertising BLE server.
   */
  void onResult(BLEAdvertisedDevice advertisedDevice) {
    Serial.print("BLE Advertised Device found: ");
    Serial.println(advertisedDevice.toString().c_str());

    // We have found a device, let us now see if it contains the service we are looking for.
    if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(serviceUUID)) {

      // 
      Serial.print("Found our device!  address: "); 
      advertisedDevice.getScan()->stop();

      pServerAddress = new BLEAddress(advertisedDevice.getAddress());
      doConnect = true;

    } // Found our server
  } // onResult
}; // MyAdvertisedDeviceCallbacks


void setup() {
  Serial.begin(115200);
  Serial.println("Starting Arduino BLE Client application...");
  BLEDevice::init("");

  // Retrieve a Scanner and set the callback we want to use to be informed when we
  // have detected a new device.  Specify that we want active scanning and start the
  // scan to run for 30 seconds.
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true);
  pBLEScan->start(30);
} // End of setup.


// This is the Arduino main loop function.
void loop() {

  // If the flag "doConnect" is true then we have scanned for and found the desired
  // BLE Server with which we wish to connect.  Now we connect to it.  Once we are 
  // connected we set the connected flag to be true.
  if (doConnect == true) {
    if (connectToServer(*pServerAddress)) {
      Serial.println("We are now connected to the BLE Server.");
      connected = true;
    } else {
      Serial.println("We have failed to connect to the server; there is nothin more we will do.");
    }
    doConnect = false;
  }

  // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  // with the current time since boot.
  if (connected) {
    String newValue = "Time since boot: " + String(millis()/1000);
    Serial.println("Setting new characteristic value to \"" + newValue + "\"");
    
    // Set the characteristic's value to be the array of bytes that is actually a string.
    pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
  }
  
  delay(1000); // Delay a second between loops.
} // End of loop

Who is online

Users browsing this forum: No registered users and 67 guests