BLE Connection of 3 devices using Arduino Nano rp2040

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...

image


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

Can you provide more info re a sample that didn't work. We need those to work as that is where we send folks that need help.
Is this the board you selected? There are several similarly named boards, it's critical you get the right one. Perhaps you could post a photo of the board both sides.
Screenshot 2025-01-09 at 18.14.51

Ready! I edited the post so to include more information.

If you can guide me on what i have to try differently would help me a lot! im learning on how to use BLE and have no info on how to make it work.

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 think there may be a simple solution to your application which avoids all the complications of connections between the devices. You can put the 0 or 1 in the advertised manufacturer data of the peripheral, and it will broadcast with the advertising packet. You can read the manufacturer data with the two centrals without ever connecting. This architecture was used in this project:

I have also seen this architecture used in a commercial barbecue where temperature data was placed in the advertising data.

Start your work with the peripheral device which will run the code to decide on the 0 or 1 (or it can be more) and then place the value you want in the manufacturer data. Then you can use a phone with LightBlue or nrfConnect to debug that code.

When the code for the peripheral is complete and it is advertising with the manufacturer data, then you can develop the code of the devices which need to read the 1 or 0 and take action.

2 Likes

That is what the library samples are for. We learn by reading.

I found the answer!

I tried it using 3 devices and works!

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