I've been working on a project for a little while now attempting to create an interface box that can receive bluetooth MIDI commands and forward them along a DIN midi cable. I'm having issues with the DIN midi portion of the project specifically. When I attempt to send midi commands over serial it fails, and I get garbage messages when the dev board is starting up. I have succeeded in sending midi messages as raw serial when I initialize the port directly, but have not succeeded in getting midi library to work. I've tried a bunch of different approaches. I have two XIAO ESP32 c3 dev boards, one is for a simple controller attached to a 5-way switch. So far what works:
I can initialize BLE midi and connect the client and server and send BLE midi messages
I can send raw midi over serial
Things I've tried that don't work:
Sending messages using MIDI.h
Sending messages using Control_Surface.h
Sending messages using the default serial0
Sending messages using serial1 at all
Here is the code that works:
#include <MIDI.h>
#include <BLEMidi.h>
#include <HardwareSerial.h>
#include <Control_Surface.h>
void onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity, uint16_t timestamp)
{
// Code to test midi messages directly over serial
int noteON = 144;//144 = 10010000 in binary, note on command
int noteOFF = 128;//128 = 10000000 in binary, note off command
MIDImessage(noteON, 51, velocity);//turn note on
Serial.printf("Note on : channel %d, note %d, velocity %d (timestamp %dms)\n", channel, note, velocity, timestamp);
}
void setup() {
Serial.begin(115200);
Serial.println("Initializing Bluetooth...");
BLEMidiClient.begin("MIDI Client");
// Configure MySerial0 on pins TX=D6 and RX=D7 (-1, -1 means use the default)
MySerial0.begin(31250, SERIAL_8N1, -1, -1);
MySerial0.print("MySerial0");
Serial.println("Waiting for connections...");
// Register the Control Change callback
BLEMidiClient.setControlChangeCallback(handleControlChange);
BLEMidiClient.setOnDisconnectCallback([](){ // To show how to make a callback with a lambda function
Serial.println("Disconnected");
digitalWrite(led_pin, LOW);
});
BLEMidiClient.setOnConnectCallback([](){
Serial.println("Connected");
digitalWrite(led_pin, HIGH);
});
BLEMidiClient.setNoteOnCallback(onNoteOn);
}
void loop() {
if(!BLEMidiClient.isConnected()) {
// If we are not already connected, we try te connect to the first BLE Midi device we find
int nDevices = BLEMidiClient.scan();
if(nDevices > 0) {
if(BLEMidiClient.connect(0))
Serial.println("Connection established");
else {
Serial.println("Connection failed");
delay(3000); // We wait 3s before attempting a new connection
}
}
}
}
//send MIDI message
void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
MySerial0.write(command);//send note on or note off command
MySerial0.write(MIDInote);//send pitch data
MySerial0.write(MIDIvelocity);//send velocity data
}
I'm pretty new to Arduino, but have beed coding for many years. I suppose I could roll my own midi library to send raw messages over serial, but seems like a lot of wheel re-invention. Anyone have thoughts?