Convert bluefruit.h Gatt Protocol to ESP32 BLE

Hi

RejsaRubberTrac is a project, that shows realtime tiretemperature in Harrys Lap Timer.

I try to "convert" the BLE Gatt Profile from Rejsa project, which is realized with the bluefruit.h lib in the ESP32 BLE lib. I digged in in the subject 2 days but dont manage to get it to work....
The code from Rejsa project should work out of the box with Harrys lap timer, because the two libs work so different i need a BLE pro to help me. all the code related to get the data from IR-cam, battery Status and so on does not mather, i can handle that, but i would like to have harrys laptimer to detect my M5-stick-c.

I dont know which are the key values to match, for working with harrys lap timer. for example, in rejsa i dont found UUIDs. I tested my code with the nRF-Connect app on android, and somehow it broadcasts values and work in some way but in the laptimer app it doesnt

This is my code :

[color=#c586c0]#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <M5StickC.h>

BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;
BLECharacteristic *pCharacteristic1 = NULL;
BLECharacteristic *pCharacteristic2 = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

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

class MyServerCallbacks : public BLEServerCallbacks
{
  void onConnect(BLEServer *pServer)
  {
    deviceConnected = true;
  };

  void onDisconnect(BLEServer *pServer)
  {
    deviceConnected = false;
  }
};

typedef struct {
  uint8_t  protocol;         // version of protocol used
  uint8_t  unused;
  int16_t  distance;         // millimeters
  int16_t  temps[8];         // all even numbered temp spots (degrees Celsius x 10)
} one_t;


typedef struct {
  uint8_t  protocol;         // version of protocol used
  uint8_t  charge;           // percent: 0-100
  uint16_t voltage;          // millivolts (normally circa 3500-4200)
  int16_t  temps[8];         // all uneven numbered temp spots (degrees Celsius x 10)
} two_t;


typedef struct {
  uint8_t  protocol;         // version of protocol used
  uint8_t  unused;
  int16_t distance;          // millimeters
  int16_t  temps[8];         // All 16 temp spots used as pairs of two each, max from each pair goes into these 8 temp values (degrees Celsius x 10)
} thr_t;

one_t datapackOne;
two_t datapackTwo;
thr_t datapackThr;

void setup()
{

  M5.begin();

  M5.Lcd.print("Hallo Gigu");

  Serial.begin(115200);

  // Create the BLE Device
  BLEDevice::init("RejsaRubber6412051B");

  // 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_READ |
      BLECharacteristic::PROPERTY_NOTIFY);

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

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


  // SET SOME DEFAULT VALUES IN THE DATA PACKETS
  datapackOne.distance = 0;
  datapackOne.protocol = PROTOCOL;
  datapackTwo.protocol = PROTOCOL;
  datapackThr.distance = 0;
  datapackThr.protocol = PROTOCOL;

  // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
  // Create a BLE Descriptor
  pCharacteristic->addDescriptor(new BLE2902());
  pCharacteristic1->addDescriptor(new BLE2902());
  pCharacteristic2->addDescriptor(new BLE2902());

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

  // Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
  BLEDevice::startAdvertising();
  Serial.println("Waiting a client connection to notify...");
}

void loop()
{
  // notify changed value
  if (deviceConnected)
  {
    pCharacteristic->setValue((uint8_t *)&datapackOne, sizeof(datapackOne));
    pCharacteristic->notify();

    pCharacteristic1->setValue((uint8_t *)&datapackTwo, sizeof(datapackTwo));
    pCharacteristic1->notify();

    pCharacteristic2->setValue((uint8_t *)&datapackThr, sizeof(datapackThr));
    pCharacteristic2->notify();
    value++;
    delay(500); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
  }
  // disconnecting
  if (!deviceConnected && oldDeviceConnected)
  {
    delay(500);                  // give the bluetooth stack the chance to get things ready
    pServer->startAdvertising(); // restart advertising
    Serial.println("start advertising");
    oldDeviceConnected = deviceConnected;
  }
  // connecting
  if (deviceConnected && !oldDeviceConnected)
  {
    // do stuff here on connecting
    oldDeviceConnected = deviceConnected;
  }
}[/color]

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