How to Indicate Bluetooth Connection Status with LED using Control-Surface Library on ESP32

Hi everyone,

I'm relatively new to Arduino and have been working on a project (basic midi controller) involving an ESP32 board communicating with a PC using Bluetooth. I'm using the Control-Surface.h library for handling the MIDI communication (instantiating BluetoothMIDI_Interface).

I have successfully prototyped my project, and the midi over ble communication works fine. However, I'm now trying to implement a visual indicator using an LED to show the Bluetooth connection status.

My goal is to have the LED blinking when the ESP32 is searching for a connection, remain solid when connected, and turn off when not connected.
I have done some research and managed to control the LED using the BLEMIDI_ESP32.h and BLEMidi.h libraries. However, when I tried to incorporate the Control-Surface library (for which I am thankful to Pieter P for making it available), I faced some challenges in approaching the LED indication.
I apologize if the solution may seem like it is already written somewhere, but before deciding to ask, I have experimented and researched for days, and still, I cannot figure it out. I have consulted all the documentation and observed the class codes, trying to understand how to use them correctly. However, the logic behind the proper usage of the libraries/classes is still probably not entirely clear to me.

I would be extremely grateful if someone with experience in using Control-Surface could kindly guide me on how to achieve this. Any insights, code examples, or step-by-step explanations would be of immense help.

Thank you all in advance for your valuable assistance and support.

Best regards,
Ghisa

Can it be that difficult to check if there's a live connection or not? It sounds like You want to cross the river to get water.
Post the code. It shows how the connection is started and verified being activated.

Thank you for the feedback, currently the code is not anything concrete; I'm using it to learn how the library works and test the various components that I plan to integrate into the MIDI controller. The code is successfully uploaded to the ESP32, which connects via bluetooth properly to the PC, interfaces with the DAW, and the various components respond correctly. Now, among the various tests I'm conducting, I've hit a roadblock, as mentioned in the request, with managing the status LED of the connection.

#include <Control_Surface.h> // Include the Control Surface library

// Instantiate a MIDI over BLE interface.
BluetoothMIDI_Interface midi;

// Instantiate a shift register as output for the LEDs
SPIShiftRegisterOut<8> sreg {
  SPI,      // SPI interface to use
  5,       // Latch pin (ST_CP)
  MSBFIRST, // Byte order
};

// Instantiate NoteButton and NoteLED objects
NoteButton transport[] {
  {19, MCU::PLAY},
  {21, MCU::STOP},
  {16, MCU::REWIND},
  {17, MIDI_CC::General_Purpose_Controller_1}, //da togliere meglio usare i numeri non CC
  {13, 112}, 
};

NoteLED transpLed[] {
  {22, MCU::PLAY},
  {sreg.pin(1), MCU::STOP},
  {25, 112},
  {2, MIDI_CC::General_Purpose_Controller_1},
};


CCRotaryEncoder enc {
  {27, 14},       // pins
  MCU::V_POT_1, // MIDI address (CC number + optional channel)
  14,            // optional multiplier if the control isn't fast enough
};

void setup() {
  midi.setName("GHISABLE");
  RelativeCCSender::setMode(relativeCCmode::MACKIE_CONTROL_RELATIVE);  //x encoder
  Control_Surface.begin(); // Initialize Control Surface
  Serial.begin(115200);

}

void loop() {
  Control_Surface.loop(); // Update the Control Surface
}

I can't find the method to receive the connection status. With another library, I successfully used "BLEMidiServer.isConnected()" which worked fine (the rest of the code would turn on the internal LED). In this case, I should be able to add the LED management to the code, but I can't find the equivalent of isConnected() in the control-surface library.

I should add it to the BluetoothMIDI_Interface API when I find the time, but in the meantime, this should work:

#include <Control_Surface.h>

extern "C" uint16_t midi_get_connection_id();

// ...

void loop() {
  digitalWrite(ledPin, midi_get_connection_id() != 0 ? HIGH : LOW);
  // ...
}

Hello Pieter, it's great to be in touch with you. I've seen your prompt responses on several posts, and it happened again! :slightly_smiling_face: Thank you for the work you've put into the library and for your patience in answering our questions.

I tried the code you provided, but it doesn't seem to work. Just to be sure about hardware, I tested different LEDs on various pins (including those on the shift register) using different code approaches (defining variables, writing the pin directly, placing the code above or below the .begin() and .loop() functions), but unfortunately, the LED that should light up upon connection doesn't turn on.

The code is uploaded correctly to the board, and the other components continue to function correctly, but the connection status LED remains off. I'm wondering where I might be making a mistake? Here's the code:

#include <Control_Surface.h> // Include the Control Surface library

extern "C" uint16_t midi_get_connection_id();

// Instantiate a MIDI over BLE interface.
BluetoothMIDI_Interface midi;

// Instantiate a shift register as output for the LEDs
SPIShiftRegisterOut<8> sreg {
  SPI,      // SPI interface to use
  5,       // Latch pin (ST_CP)
  MSBFIRST, // Byte order
};

// define variables
int ledPin = 25;

// Instantiate NoteButton and NoteLED objects
NoteButton transport[] {
  {19, MCU::PLAY},
  {21, MCU::STOP},
  {16, MCU::REWIND},
  {17, MIDI_CC::General_Purpose_Controller_1}, //rimuovere, uso numeri
  {13, 112}, 
};

NoteLED transpLed[] {
  {22, MCU::PLAY},
  {sreg.pin(1), MCU::STOP},
  //{25, 112},
  {2, MIDI_CC::General_Purpose_Controller_1},
};


CCRotaryEncoder enc {
  {27, 14},       // pins
  MCU::V_POT_1, // MIDI address (CC number + optional channel)
  14,            // optional multiplier if the control isn't fast enough
};

void setup() {
  pinMode(ledPin, OUTPUT);
  midi.setName("GHISABLE");
  RelativeCCSender::setMode(relativeCCmode::MACKIE_CONTROL_RELATIVE);  //x encoder
  Control_Surface.begin(); // Initialize Control Surface
  Serial.begin(115200);


}

void loop() {
  digitalWrite(ledPin, midi_get_connection_id() != 0 ? HIGH : LOW);
  Control_Surface.loop(); // Update the Control Surface
}

I appreciate any insights you can provide. Thank you!

You're right, it seems like zero is a valid connection id, so the check I posted won't work. I'll see if I can add a function to check the connection status later today.

As a quick hack, you could perhaps change the initial value of midi_conn_id to 0xFFFF. Then the code from my previous post should work (untested). https://github.com/tttapa/Control-Surface/blob/f2fc513b713fdd521e586a3ab7d18bedd9cd2bc5/src/MIDI_Interfaces/BLEMIDI/ESP32/midi-app.c#L126

I tried testing it, but unfortunately, it didn't work as expected. I'm not sure if I'm making any mistakes, but here are the steps I followed:

  1. I navigated to the directory containing the file midi-app.c.
  2. I opened the file using a text editor.
  3. I modified the line you indicated: static uint16_t midi_conn_id = 0xFFFF;
  4. I saved and closed the file (I'm not sure if I need to use any specific procedure to reload the library after making the changes to the midi-app.c file.)
  5. I successfully loaded the code onto the ESP32.
  6. The behavior is as follows: the LED turns on after the code is loaded (the board is not yet connected via Bluetooth). However, as soon as I connect the board via Bluetooth (the connection is established correctly, and other components work and communicate properly with the DAW), the LED turns off. If I disconnect the Bluetooth connection, the LED remains off. Even if I reconnect the board, the LED stays off. When I press the reset button on the ESP32, the LED turns on again, but then behaves as described above.

With the above information provided:

  • If, as you mentioned in your message, you have a way to implement a function to solve this issue, I would be immensely grateful, and I eagerly await updates.
  • Alternatively, how else could I provide feedback to the user about a successful connection? Is there any other solution besides using the LED? Perhaps, even by adding a display, but regardless of the output tool, I would still face the dilemma of how to retrieve information about the connection status, right? (though I wanted to avoid it to keep things simple for one of my first projects).

Thanks

Try this: Change the argument from 0 to 0xFFFF in https://github.com/tttapa/Control-Surface/blob/f2fc513b713fdd521e586a3ab7d18bedd9cd2bc5/src/MIDI_Interfaces/BLEMIDI/ESP32/midi-connection.c#L51, and then change the condition in your loop to != 0xFFFF.

Okay, i made the additional edit and now it's working perfectly!

Thank you so much for your help and for the outstanding work done!
This feels like a five-star customer care service! :grinning:

Stefano

1 Like

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