Data not sending through BLE on two Seed Xiao Nrf58240 Senses

Hello everyone,

I am currently working on a project that requires sending data from a pressure sensor from one chip to another chip that uses this data to control a servo motor. Currently I have been able to establish a connection between the two boards. However, the pressure data from the sensor on the central device does not seem to match with the one displaying on the peripheral device ( around 800 on central and 43 on peripheral). Please provide any guidance on my code below as I am new to the field.

Central device code:

#include <ArduinoBLE.h>
#include <Wire.h>

const char* deviceServiceUuid = "19B10000-E8F2-537E-4F6C-D104768A1214";
const char* deviceServiceCharacteristicUuid = "19B10000-E8F2-537E-4F6C-D104768A1214";

int pressure = -1;
int oldPressureValue = -1;  

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

    if (!BLE.begin()) {
    Serial.println("* Starting Bluetooth® Low Energy module failed!");
    while (1);
  }
  
  BLE.setLocalName("Pressure sensor (Central)"); 
  BLE.advertise();

  Serial.println("Pressure sensor (Central Device)");
  Serial.println(" ");
}

void loop() {
  connectToPeripheral();
}

void connectToPeripheral(){
  BLEDevice peripheral;
  
  Serial.println("- Discovering peripheral device...");

  do
  {
    BLE.scanForUuid(deviceServiceUuid);
    peripheral = BLE.available();
  } while (!peripheral);
  
  if (peripheral) {
    Serial.println("* Peripheral device found!");
    Serial.print("* Device MAC address: ");
    Serial.println(peripheral.address());
    Serial.print("* Device name: ");
    Serial.println(peripheral.localName());
    Serial.print("* Advertised service UUID: ");
    Serial.println(peripheral.advertisedServiceUuid());
    Serial.println(" ");
    BLE.stopScan();
    controlPeripheral(peripheral);
  }
}

void controlPeripheral(BLEDevice peripheral) {
  Serial.println("- Connecting to peripheral device...");

  if (peripheral.connect()) {
    Serial.println("* Connected to peripheral device!");
    Serial.println(" ");
  } else {
    Serial.println("* Connection to peripheral device failed!");
    Serial.println(" ");
    return;
  }

  Serial.println("- Discovering peripheral device attributes...");
  if (peripheral.discoverAttributes()) {
    Serial.println("* Peripheral device attributes discovered!");
    Serial.println(" ");
  } else {
    Serial.println("* Peripheral device attributes discovery failed!");
    Serial.println(" ");
    peripheral.disconnect();
    return;
  }

  BLECharacteristic pressureCharacteristic = peripheral.characteristic(deviceServiceCharacteristicUuid);
    
  if (!pressureCharacteristic) {
    Serial.println("* Peripheral device does not have pressure_type characteristic!");
    peripheral.disconnect();
    return;
  } else if (!pressureCharacteristic.canWrite()) {
    Serial.println("* Peripheral does not have a writable pressure_type characteristic!");
    peripheral.disconnect();
    return;
  }
  
  while (peripheral.connected()) {
    pressure = analogRead(A0);

    if (oldPressureValue != pressure) {  
      oldPressureValue = pressure;
      Serial.print("* Writing value to gesture_type characteristic: ");
      Serial.println(pressure);
      pressureCharacteristic.writeValue((byte)pressure);
      Serial.println(" ");
    }
  
  }
  Serial.println("- Peripheral device disconnected!");
}

Peripheral code:

#include <ArduinoBLE.h>

const char* deviceServiceUuid = "19B10000-E8F2-537E-4F6C-D104768A1214";
const char* deviceServiceCharacteristicUuid = "19B10000-E8F2-537E-4F6C-D104768A1214";

int pressure = -1;

BLEService pressureService(deviceServiceUuid); 
BLEByteCharacteristic pressureCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial);

  if (!BLE.begin()) {
    Serial.println("- Starting Bluetooth® Low Energy module failed!");
    while (1);
  }

  BLE.setLocalName("Motor control (Peripheral)");
  BLE.setAdvertisedService(pressureService);
  pressureService.addCharacteristic(pressureCharacteristic);
  BLE.addService(pressureService);
  pressureCharacteristic.writeValue(-1);
  BLE.advertise();

  Serial.println("Motor control (Peripheral Device)");
  Serial.println(" ");
}

void loop() {
  // put your main code here, to run repeatedly:

  BLEDevice central = BLE.central();
  Serial.println("- Discovering central device...");
  delay(5000);

  if (central) {
    Serial.println("* Connected to central device!");
    Serial.print("* Device MAC address: ");
    Serial.println(central.address());
    Serial.println(" ");

    while (central.connected()) {
      if (pressureCharacteristic.written()) {
         pressure = pressureCharacteristic.value();
         //writePressure(pressure);
         Serial.println(pressure);
       }
    }
    
    Serial.println("* Disconnected to central device!");
  }
}

Hi there,
So I’m wondering why not just advertise the Pressure from the remote sensor, and Read the advertised data, is one way.
Or Subscribe and Notify method of said pressure reading, Check the examples for a more robust method just establishing a connection is only part of the process. Is the dat a String or a float?
Passing IMU data is more like what you are wanting to achieve. Look at the motion, free-fall demos I have on here for more incite to the connection and passing the data in the aforementioned techniques.

You are not far off, Give it a look and ask any questions you might have.
HTH
GL :slight_smile: PJ
:v:

It will work if you do this.

//int pressure = -1;
//int oldPressureValue = -1;
byte pressure = -1;
byte oldPressureValue = -1;
1 Like

Thanks for the reply PJ,
I was under the impression that I had to advertise the pressure sensoring service as a whole since every guide so far has mentioned such. Could you elaborate further on how to advertise just the pressure in my case? On the subscribe and notify method, do you have any links of any examples I can learn from? Lastly, I can’t seem to find your demos anywhere either, I’d love to have a look for inspiration too. Thank you so much!

Thanks for your reply msfujino,
After trying the change, both data displayed on central and peripheral are now in the 40 range. I should have mentioned that I needed the 800 range to be sent over to the peripheral device as this is the accurate reading from the pressure sensor. Thank you and please let me know if there is any other changes I could try!

Sketch previously written. I don’t know if it will help, but here is a link to it below.
Attitude monitor using Peripheral:XIAO_BLE_Sence and Central:XIAO_BLE with mbed 2.7.2 and ArduinoBLE

Hi there,
Sounds like a coded PHY range type requirement. the data is small payload?
nordic has a demo of range outside of 1300 meters WOW!
using 2 dev kits , I’m looking into trying something like this with 2 Xiao’s
Stay tuned.
HTH
GL :slight_smile: PJ :v:

Hey PJ,
Thankfully I’ve figured out how to send the data over. The subscribe and notify method seemed to have worked so thank you! I am interested in your long range project too so I’ll for sure stay tuned!