Simplest way to write value to BLE characteristic

shooks12
Posts: 2
Joined: Thu Sep 17, 2020 6:21 pm

Simplest way to write value to BLE characteristic

Postby shooks12 » Thu Sep 17, 2020 6:24 pm

I'm new to BLE on ESP32 and am trying to do something simple. Basically, I have an RGB bulb that uses BLE and I want to control it by writing a value to a characteristic. This is dead simple using gatttool on Linux. Once connected, it's just something like 'char-write-cmd 0x0016 00FF0000' where 0x0016 is the characteristic and 00FF0000 is the value.

What is the simplest way to do this on an ESP32? The BLE client example seems to do more than is necessary here and I'm wondering if something simpler would be easier for me to follow and understand.

Thanks!

shooks12
Posts: 2
Joined: Thu Sep 17, 2020 6:21 pm

Re: Simplest way to write value to BLE characteristic

Postby shooks12 » Sat Sep 19, 2020 5:42 am

I figured this out and got a simple test working. Got there through more research, posting on reddit, and lots of trial and error. It also really helped to have the nRF Connect app for testing the bulb and to find the UUIDs. Posting here in case it can help someone in the future.


  1. // Write to Playbulb via BLE
  2.  
  3. #include "BLEDevice.h"
  4.  
  5. // The remote service we wish to connect to.
  6. static BLEUUID serviceUUID("0000ff01-0000-1000-8000-00805f9b34fb");
  7. // The characteristic of the remote service we are interested in.
  8. static BLEUUID charUUID("0000fffc-0000-1000-8000-00805f9b34fb");
  9.  
  10. static BLERemoteCharacteristic* pRemoteCharacteristic;
  11.  
  12. void setup() {
  13.  
  14.   // start serial port at 1115200 bps:
  15.   Serial.begin(115200);
  16.   while (!Serial) {
  17.     ; // wait for serial port to connect. Needed for native USB port only
  18.   }
  19.  
  20.   BLEDevice::init("");
  21.   BLEClient*  pClient = BLEDevice::createClient();
  22.  
  23.   if (pClient->connect(BLEAddress("CD:B3:4B:0A:AC:E6"))) {
  24.     Serial.println("Connected!");
  25.   } else Serial.println("Failed to connect");
  26.  
  27.   BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
  28.   if (pRemoteService == nullptr) {
  29.     Serial.println("Failed to get service");
  30.     return;
  31.   } else Serial.println("Got service");
  32.  
  33.   pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  34.   if (pRemoteCharacteristic == nullptr) {
  35.     Serial.println("Failed to get characteristic");
  36.     return;
  37.   } else Serial.println("Got characteristic");
  38.  
  39. } // End of setup.
  40.  
  41.  
  42. void loop() {
  43.  
  44.   byte colors[2][4] = {
  45.     {0x00, 0x00, 0x00, 0xff}, // Blue
  46.     {0x00, 0xff, 0x00, 0x00}  // Red
  47.   };
  48.  
  49.   Serial.println("blue");
  50.   pRemoteCharacteristic->writeValue(colors[0], sizeof(colors[0]));
  51.   delay(2000);
  52.  
  53.   Serial.println("red");
  54.   pRemoteCharacteristic->writeValue(colors[1], sizeof(colors[1]));
  55.   delay(2000);
  56.  
  57. } // End of loop

Who is online

Users browsing this forum: No registered users and 100 guests