Hi! I'm working with a project in which i have to: from Device A send information to Device B and Device C at the same time.
For the three devices, I have to use arduino BLE with the Nano RP2040. I had tried making a characteristic so that they connect to it but it doesnt work.
I have already bought 3 of them from here, which is an official seller:
Can anyone guide me on how can i make that type of system? To be honest, the device A only has to send a 0 or a 1 (a bool type) to the other arduinos and the other arduinos will read that and do a custom simple function of turning on a led.
I tried to use the examples from the library but they didnt work... If you already have a code pls share it here. Pls help!
This is the Receiver code:
#include <ArduinoBLE.h>
BLEService myService("1234"); // BLE service UUID
BLECharacteristic myCharacteristic("5678", BLERead | BLENotify, 25); // Increased to 25 bytes
String fullMessage = "";
void setup() {
Serial.begin(9600);
while (!Serial);
if (!BLE.begin()) {
Serial.println("Starting BLE failed!");
while (1);
}
BLE.setLocalName("NanoReceiver");
Serial.println("Bluetooth Receiver ready");
BLE.scan();
}
void loop() {
BLEDevice peripheral = BLE.available();
if (peripheral) {
if (peripheral.localName() == "NanoSender") {
BLE.stopScan();
processPeripheral(peripheral);
BLE.scan();
}
}
}
void processPeripheral(BLEDevice peripheral) {
Serial.print("Connected to: ");
Serial.println(peripheral.address());
if (peripheral.connect()) {
Serial.println("Connected");
if (peripheral.discoverAttributes()) {
BLECharacteristic characteristic = peripheral.characteristic("5678");
if (characteristic) {
characteristic.subscribe();
while (peripheral.connected()) {
if (characteristic.valueUpdated()) {
byte value[25]; // Increased buffer size
int valueLength = characteristic.readValue(value, sizeof(value));
String receivedChunk = "";
for (int i = 0; i < valueLength; i++) {
receivedChunk += (char)value[i];
}
Serial.print("Received: ");
Serial.println(receivedChunk);
fullMessage += receivedChunk;
}
}
characteristic.unsubscribe();
}
}
peripheral.disconnect();
}
Serial.println("Full message received: " + fullMessage);
fullMessage = "";
Serial.println("Disconnected");
}
This is the sender code:
#include <ArduinoBLE.h>
BLEService myService("1234"); // BLE service
BLECharacteristic myCharacteristic("5678", BLERead | BLENotify, 25); // Increased to 25 bytes
void setup() {
Serial.begin(9600);
while (!Serial);
if (!BLE.begin()) {
Serial.println("Starting BLE failed!");
while (1);
}
BLE.setLocalName("NanoSender");
BLE.setAdvertisedService(myService);
myService.addCharacteristic(myCharacteristic);
BLE.addService(myService);
// Set initial value
myCharacteristic.writeValue("");
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connection...");
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to: ");
Serial.println(central.address());
while (central.connected()) {
String dataToSend = "Hello from the sender";
// Add string length check for safety
if (dataToSend.length() + 1 <= 25) { // +1 for null terminator
myCharacteristic.writeValue(dataToSend.c_str(), dataToSend.length());
Serial.println("Sent: " + dataToSend);
} else {
Serial.println("Message too long for characteristic!");
}
delay(1000);
}
Serial.println("Disconnected");
}
}
While in the IDE, this is the one im connecting to the board...

I have already make this code work for a pair of devices, but the moment I tried to connect 3 it stops working...
