Advertisement
microrobotics

Seeed Studio XIAO nRF52840

May 23rd, 2023
1,726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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.
  3.  
  4. First, ensure that the ArduinoBLE library is installed in your Arduino IDE.
  5.  
  6. 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.
  7.  
  8. 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.
  9.  
  10. Please replace the UUIDs and the characteristic properties according to your needs.
  11. */
  12.  
  13. #include <ArduinoBLE.h>
  14.  
  15. BLEService customService("180D"); // create a service with a custom UUID
  16. BLEIntCharacteristic customCharacteristic("2A37", BLERead | BLENotify); // create a characteristic with a custom UUID
  17.  
  18. void setup() {
  19.   Serial.begin(9600);
  20.  
  21.   // while not connected to the serial port, do nothing
  22.   while (!Serial);
  23.  
  24.   if (!BLE.begin()) {
  25.     Serial.println("starting BLE failed!");
  26.     while (1);
  27.   }
  28.  
  29.   BLE.setLocalName("XIAO nRF52840"); // set the name of the device
  30.   BLE.setAdvertisedService(customService); // advertise the custom service
  31.   customService.addCharacteristic(customCharacteristic); // add the custom characteristic to the service
  32.   BLE.addService(customService); // add the service
  33.  
  34.   customCharacteristic.writeValue(0); // set the initial value of the characteristic
  35.  
  36.   BLE.advertise(); // start advertising
  37.  
  38.   Serial.println("BLE device active, waiting for connections...");
  39. }
  40.  
  41. void loop() {
  42.   // wait for a BLE central device to connect
  43.   BLEDevice central = BLE.central();
  44.  
  45.   if (central) {
  46.     Serial.print("Connected to central: ");
  47.     Serial.println(central.address());
  48.    
  49.     while (central.connected()) {
  50.       // this is where you would write the sensor readings to the characteristic
  51.       // for the sake of example, let's just increment the characteristic value every second
  52.       int currentValue = customCharacteristic.value();
  53.       customCharacteristic.writeValue(currentValue + 1);
  54.       delay(1000);
  55.     }
  56.  
  57.     Serial.print("Disconnected from central: ");
  58.     Serial.println(central.address());
  59.   }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement