microrobotics icon

Seeed Studio XIAO nRF52840

microrobotics | PRO | 05/23/23 11:50:39 AM UTC | 0 ⭐ | 4206 👁️ | Never ⏰ | []
Arduino |

2.56 KB

|

None

|

0 👍

/

0 👎

/*
Interfacing Bluetooth with the Seeed Studio XIAO nRF52840 involves using the ArduinoBLE library. Below is a simple example showing how to create a BLE server that advertises a custom service and characteristic.
 
First, ensure that the ArduinoBLE library is installed in your Arduino IDE.
 
This example creates a Bluetooth device named "XIAO nRF52840" that has a custom service (with UUID 180D) and a custom characteristic (with UUID 2A37). The characteristic is readable and can notify a connected device when its value changes. When a central device (like your smartphone) connects to the XIAO, it increments the value of the characteristic every second until the central device disconnects.
 
Note: The example uses placeholder UUIDs (180D for the service and 2A37 for the characteristic) which you may want to replace with your own UUIDs. The UUIDs should adhere to the standard Bluetooth UUID format.
 
Please replace the UUIDs and the characteristic properties according to your needs.
*/
 
#include <ArduinoBLE.h>
 
BLEService customService("180D"); // create a service with a custom UUID
BLEIntCharacteristic customCharacteristic("2A37", BLERead | BLENotify); // create a characteristic with a custom UUID
 
void setup() {
  Serial.begin(9600);
  
  // while not connected to the serial port, do nothing
  while (!Serial);
 
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }
 
  BLE.setLocalName("XIAO nRF52840"); // set the name of the device
  BLE.setAdvertisedService(customService); // advertise the custom service
  customService.addCharacteristic(customCharacteristic); // add the custom characteristic to the service
  BLE.addService(customService); // add the service
  
  customCharacteristic.writeValue(0); // set the initial value of the characteristic
 
  BLE.advertise(); // start advertising
 
  Serial.println("BLE device active, waiting for connections...");
}
 
void loop() {
  // wait for a BLE central device to connect
  BLEDevice central = BLE.central();
 
  if (central) {
    Serial.print("Connected to central: ");
    Serial.println(central.address());
    
    while (central.connected()) {
      // this is where you would write the sensor readings to the characteristic
      // for the sake of example, let's just increment the characteristic value every second
      int currentValue = customCharacteristic.value();
      customCharacteristic.writeValue(currentValue + 1);
      delay(1000);
    }
 
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}
 

Comments