Send midi Control (cc) message

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Thanatos
Posts: 5
Joined: Wed Dec 02, 2020 6:21 pm

Send midi Control (cc) message

Post by Thanatos » Sun Dec 06, 2020 12:22 am

Hello

I am trying to send a midi control change message from my ESP32 to an instrument I have.
My setup is like this:

One ESP32 (remote) acts as a remote that sends data with the press of a button to another ESP32 (servant) via BLE. The servant is attached via the MIDI in to my musical instrument. The idea is that with the press of a button, I can send a midi cc message to the servant which forwards my message over a midi cable to my instrument (causing it to change some stuff in the instrument). Both ESP32s are ESP32-DevKitC V4 (Espressif).

The whole BLE setup is set up on both the remote and the servant. However, the only remaining part is sending the requested control to my musical instrument. I have tried so many things now, that I have no clue any more what to do. It is very hard to know what is going wrong. Right now, when sending data, my instrument doesn't react. A couple of potential flaws in my setup:
  • My instrument is broken.
  • My wiring is flawed.
  • My code is wrong.
  • ...
From top to bottom, the likeliness increases I think. This is what I have now:
Female DIN 5/180 is connected to my ESP32 with pin 2 to ground, pin 4 with 220 Ohm resistor to 5V and pin 5 to ESP32 Pin 19. My code for sending the midi looks like this now:

Code: Select all

uart = UART(1, 31250, tx=18, rx=19)  # init with given baudrate
# uart.init(31250, bits=8, parity=None, stop=1, pins=[18, 19])  # init with given parameters
cc_channel = 0xB0
cc_channel += 0
uart.write(cc_channel)
uart.write(data)
uart.write(struct.pack("I", 127))
This is a piece of the manual quoted:
The instrument can be controlled by incoming MIDI CC (control change) messages sent by external
MIDI gear.

Code: Select all

+-----+--------------+
| CC# |    Action    |
+-----+--------------+
|  49 | Footswitch 1 |
|  50 | Footswitch 2 |
| ... | ...          |
+-----+--------------+
I don't see what value I have to present for the CC#, since a cc message needs a value for the control as well I believe. I think it doesn't really matter.

Is this the correct approach to send this data over via UART? How do I "compose" a CC message to send over UART?
What does the uart.init provide over the UART constructor?

Thanks in advance <3!

EDIT:
Upon booting, I receive this message:

Code: Select all

I (13353) uart: ALREADY NULL

Thanatos
Posts: 5
Joined: Wed Dec 02, 2020 6:21 pm

Re: Send midi Control (cc) message

Post by Thanatos » Mon Dec 07, 2020 12:03 am

I was able to send a midi PC and CC message like so:

Code: Select all

uart = UART(1, 31250, tx=18, rx=19)
program = 55
program_value = 127
# PC message
uart.write(b"\xc0" + program.to_bytes(1, "big"))
# CC message 
uart.write(b"\xb0" + program.to_bytes(1, "big") + program_value.to_bytes(1, "big"))
Of course the program and program_value variables can be changed to your liking.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Send midi Control (cc) message

Post by SpotlightKid » Tue May 18, 2021 11:20 am

I know this thread is already a few months old, but for posterity:

To convert the MIDI message data from integers to bytes for sending them with uart.write(), just use the builtin bytes function.

Example:

Code: Select all

# send CC #64 (Sustain) on channel 3 with value 64 (=on)
channel = 2  # (zero-based channel counting)
uart.write(bytes([0xB0 | channel, 64, 64]))
I also have a convenience library for sending/receiving MIDI messages via a serial device:

https://github.com/SpotlightKid/micropy ... aster/midi

Post Reply