It is possible to receive and (tbd) send MIDI messages in node-red. Here we use the MIDI node, which is included in the preconfigured image or you can install it using npm install node-red-contrib-midi
in your node-red folder (default ~/.node-red).
You should see the Midi In node under the “functions” category:
Most class-compliant MIDI devices and adapter cables should work with this node. To configure it, you may have to find out which port to use, as it is currently not exposed in the most user friendly manner (tbd) – below we use the midiprobe utility (from rtmidi) to list the available ports on the system:
In this case, we’re interested in port #2, which is at index 1 of the list. Note that for typical USB-midi adapters you should only see one or two inputs and outputs instead of the large number shown above.
In the node configuration of the MIDI object (double click the node in your flow), set the port number to 1:
Now, you should be able to see incoming MIDI messages if you attach a debug node to it:
Of course, just seeing a list of numbers is not that exciting. Copy and paste the following flow to see how you can do some basic parsing of the message:
[{"id":"c2c6477f.a696e8","type":"midi","z":"29801501.b12f9a","name":"MIDI In","filtermessage":"","port":"1","x":72,"y":42,"wires":[["a8489adb.12f6c8"]]},{"id":"a8489adb.12f6c8","type":"template","z":"29801501.b12f9a","name":"grab midi msg","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.message}}","x":139,"y":90,"wires":[["6a918548.45b34c"]]},{"id":"6a918548.45b34c","type":"csv","z":"29801501.b12f9a","name":"","sep":",","hdrin":"","hdrout":"","multi":"one","ret":"\\n","temp":"","x":217,"y":41,"wires":[["cc1b3fe4.be9cc","7a9da23f.8e6a2c","fbdad48.cbcff28"]]},{"id":"cc1b3fe4.be9cc","type":"template","z":"29801501.b12f9a","name":"col1","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.col1}}","x":319,"y":83,"wires":[["b7bcfdb6.33e0f"]]},{"id":"7a9da23f.8e6a2c","type":"template","z":"29801501.b12f9a","name":"col2","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.col2}}","x":318.5,"y":117,"wires":[["3cd89838.d41078"]]},{"id":"fbdad48.cbcff28","type":"template","z":"29801501.b12f9a","name":"col3","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.col3}}","x":319.5,"y":151,"wires":[["4290cc8e.4d81b4","87071e9.6820ce"]]}]
Now, we have the message split up into easier to handle chunks!
We can even add some ui nodes to display the results in a nice panel, as shown below:
Check out these articles on the node-red-contrib-ui for more details on making your dashboards for node-red!
- http://noderedguide.com/index.php/2016/05/17/lecture-7-dashboards-and-ui-techniques-for-node-red/
- http://embeddedexperience.blogspot.ca/2016/02/rapid-dashboarding-with-node-red.html
Finally, we combine the interface for the LCD module described earlier to build the following flow, which displays the velocity of received MIDI messages on the display (code at end):
Now, we can monitor the MIDI note velocities on the external LCD as they are received!
[{"id":"c2c6477f.a696e8","type":"midi","z":"29801501.b12f9a","name":"MIDI In","filtermessage":"","port":"1","x":72,"y":42,"wires":[["a8489adb.12f6c8"]]},{"id":"a8489adb.12f6c8","type":"template","z":"29801501.b12f9a","name":"grab midi msg","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.message}}","x":139,"y":90,"wires":[["6a918548.45b34c"]]},{"id":"6a918548.45b34c","type":"csv","z":"29801501.b12f9a","name":"","sep":",","hdrin":"","hdrout":"","multi":"one","ret":"\\n","temp":"","x":217,"y":41,"wires":[["cc1b3fe4.be9cc","7a9da23f.8e6a2c","fbdad48.cbcff28"]]},{"id":"cc1b3fe4.be9cc","type":"template","z":"29801501.b12f9a","name":"col1","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.col1}}","x":319,"y":83,"wires":[["b7bcfdb6.33e0f"]]},{"id":"7a9da23f.8e6a2c","type":"template","z":"29801501.b12f9a","name":"col2","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.col2}}","x":318.5,"y":117,"wires":[["3cd89838.d41078"]]},{"id":"fbdad48.cbcff28","type":"template","z":"29801501.b12f9a","name":"col3","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.col3}}","x":319.5,"y":151,"wires":[["4290cc8e.4d81b4","87071e9.6820ce"]]},{"id":"b7bcfdb6.33e0f","type":"ui_text","z":"29801501.b12f9a","tab":"edfd6bdf.a4a1e8","name":"byte 1","group":"MIDI Input","order":1,"format":"{{msg.payload}}","x":437,"y":83,"wires":[]},{"id":"3cd89838.d41078","type":"ui_text","z":"29801501.b12f9a","tab":"edfd6bdf.a4a1e8","name":"byte 2","group":"MIDI Input","order":1,"format":"{{msg.payload}}","x":437.5,"y":117,"wires":[]},{"id":"4290cc8e.4d81b4","type":"ui_text","z":"29801501.b12f9a","tab":"edfd6bdf.a4a1e8","name":"byte 3","group":"MIDI Input","order":1,"format":"{{msg.payload}}","x":436.5,"y":151,"wires":[]},{"id":"87071e9.6820ce","type":"ui_slider","z":"29801501.b12f9a","tab":"edfd6bdf.a4a1e8","name":"Velocity","topic":"","group":"MIDI Input","order":1,"min":0,"max":"127","x":447,"y":186,"wires":[["7acee436.c781bc"]]},{"id":"9be6cd76.9f3a8","type":"i2clcd","z":"29801501.b12f9a","name":"1602 LCD","addr":"0x3f","x":523.5,"y":250,"wires":[[]]},{"id":"10654062.0aeeb","type":"function","z":"29801501.b12f9a","name":"make vel string","func":"var value_str = new String(msg.payload);\nwhile (value_str.length < 3) {\n value_str = '0' + value_str;\n}\nmsg.payload = \"vel = \" + value_str;\nmsg.topic = \"line2\";\n\nreturn msg;","outputs":1,"noerr":0,"x":351.5,"y":251,"wires":[["9be6cd76.9f3a8"]]},{"id":"7acee436.c781bc","type":"delay","z":"29801501.b12f9a","name":"10 msg/sec limit","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"10","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":161,"y":251,"wires":[["10654062.0aeeb"]]},{"id":"edfd6bdf.a4a1e8","type":"ui_tab","z":"29801501.b12f9a","name":"MIDI","icon":"dashboard","order":"1"}]
Pingback: node-red MIDI output – Infusion Systems