TWO Xiao nrf52840s connecting together via ble

I have two Xiao nrf52840s and I want to connect them via Bluetooth, I am having a difficult time with this, my commands are a bit complex, so I do not mind starting off with something simple and just connecting them together and sending a simple command, any sample code that can do this?

Hi there,

and welcome Here…

Indeed there is , have you checked the Example’s in the Arduino IDE,(under the Arduino BLE) they have a Central and a Peripheral examples, one for each end, Or if you want just a cable replacement with BLE then UART example is your thing?
It’s easy , however there is reading involved , :smile: seems to me your here so you can handle it. Check out the wiki for the Nrf52840 there is enough to get you going with the Mechanics of loading and BOOT mode etc. plus some basic Blink the LED as well.
I have several demos on here of BLE USE in various scenarios check those and the code examples.
Explain more of what you want to do and LOTS of smart folks on here to help. :+1:
HTH
GL :slight_smile: PJ :v:

Stuff like sending BLE Accelerometer and gyro data, FALL , motion.orientation , etc.

thanks, will check it out!

1 Like

I think they are connected but I cannot seem to get the Xiao to send the Hello phrase where I can see it on the other Xiao’s serial port so I am hesitant if it is working or not, wish there was a sample.
Peripheral Code:
#include <bluefruit.h>

BLEUart bleuart;

void setup() {
delay(1000);
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println(“Peripheral: Starting setup…”);

Bluefruit.begin();
Bluefruit.setName(“Xiao Peripheral”);
Serial.println(“Peripheral: BLE initialized.”);

bleuart.begin();
Serial.println(“Peripheral: BLE UART service started.”);

Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addService(bleuart);
Bluefruit.Advertising.setInterval(32, 244);
Bluefruit.Advertising.start(0);
Serial.println(“Peripheral: Advertising started.”);
}

void loop() {
if (bleuart.available()) {
char data = bleuart.read();
Serial.print("Peripheral: Received → ");
Serial.println(data);

bleuart.write(data);
Serial.print("Peripheral: Echoed -> ");
Serial.println(data);

}
}

Central Code
#include <bluefruit.h>

BLEClientUart clientUart;

void setup() {
delay(1000);
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println(“Central: Starting setup…”);

Bluefruit.begin(0, 1);
Bluefruit.setName(“Xiao Central”);
Serial.println(“Central: BLE initialized.”);

clientUart.begin();
Serial.println(“Central: BLE UART client started.”);

Bluefruit.Scanner.setRxCallback(scan_callback);
Bluefruit.Scanner.restartOnDisconnect(true);
Bluefruit.Scanner.filterUuid(clientUart.uuid);
Bluefruit.Scanner.start(0);
Serial.println(“Central: Scanning started…”);
}

void loop() {
if (clientUart.discovered()) {
if (clientUart.write(“Hello”)) {
Serial.println(“Central: Sent → Hello”);
} else {
Serial.println(“Central: Failed to send data.”);
}
delay(1000);

while (clientUart.available()) {
  char received = clientUart.read();
  Serial.print("Central: Received -> ");
  Serial.println(received);
}

}
}

void scan_callback(ble_gap_evt_adv_report_t* report) {
Serial.println(“Central: Device found.”);
if (Bluefruit.Scanner.checkReportForUuid(report, clientUart.uuid)) {
Serial.println(“Central: Found Peripheral with matching UUID.”);
Bluefruit.Scanner.stop();
Bluefruit.Central.connect(report);
Serial.println(“Central: Connecting to Peripheral…”);
}
}

while !Serial will stop program execution until you connect the serial monitor… I doubt you have both connected to the serial monitor t the same time

Hi there,

Sounds like you want a cable replacement in that case the UART example is what you want.
It’s point to point, WHat you have now is the Master Slave setup with Central and Peripheral
If you use a Program like NRF_connect for Desktop and $9 dongle you can See better what is going on in the BLE realm…

Multiple devices can connect to it and pass data /NOTIFY, /INDICATE , etc…
Your making progress so just keep going , It is NOT rocket science but a process everyone can learn (the BLE handshake) . Check out @msfujino’s demo with code the Central/Peripheral collecting data and saving to SD for the Central connect to pass to…pretty cool. :+1: Not to mention the POWER it doesn’t use :smiley: he takes major advantage of the Sleep modes so there is something in there for everyone.

HTH
GL :slight_smile: PJ :v:

plus using the RGB as a status indicator can alleviate the need for the second serial port connection also…FYI

Thank you, will check out the page.

1 Like