December 1, 2015 at 11:17 pm (computers, internet, music) (, , , , )

I wanted to do something with MIDI and Arduino.  I’ve just picked up some cheap Arduino Nano Ethernet shields, based on the Microchip ENC28J60, so thought I’d combine the two.  The ENC28J60 and a cheap Arduino Nano makes for a very compact and economical Ethernet ready microcontroller and I have a nice Roland MT-32 Synth module gathering dust that I wanted to try again.

2015-12-01 21.16.05

My initial idea was to use MIDI to trigger sounds based on arp requests received by the Arduino, (hence the name MIDI Arp), but then decided that on my home network arp requests wouldn’t give a lot of variation, so I decided to see if I could trigger on the destination IP address of any packet received.

First I needed the MIDI interface.  I followed the simple circuit and example provided on the Arduino website, but wanted it all self-contained inside a MIDI plug rather than on a breadboard, so I soldered up the 220 resistor inside a 5 pin MIDI DIN plug as follows (MIDI pins are number 1, 4, 2, 5, 3 for some reason):

  • Arduino GND – Brown – MIDI Plug pin 2
  • Arduino 5v – Red – 220 Ohm resistor – MIDI Plug pin 4
  • Arduino TX (Pin 1) – Orange – MIDI Plug pin 5

This was then connected to the Arduino and the MIDI test programme showed that all works fine.

2015-12-01 21.16.212015-12-01 21.19.13

So, to the Ethernet side of things.  The following is an excellent starting place for the ENC28J60 based nano shield:

After reading this, I decided to use the UIPEthernet library as my starting point as I liked the idea of a plug-in replacement to the standard Arduino Ethernet library.  There was two major things to work out – first, how to set the device into some kind of promiscuous mode, assuming it supports it at all; second how to grab the destination IP address from any received packets.

From the ENC28J60 data sheet, the key register that controls the receiver filtering is the Ethernet Receive Filter Control Register – ERXFCON (see section 8 ‘Receive Filters’).  There are a number of modes for filtering and the UIPEthernet library is set up to filter for the unicast address associated with the MAC address configured for the module, for broadcasts, and to use the pattern matching filter to spot arp packets.  It turns out that to set the receiver into promiscuous mode, this register just has to be set to zero.

Now this is where things got lazy.  I just dived into the UIPEthernet library sitting in my Arduino library folder and hacked about. I might tidy this up one day and do it properly.

The low-level driver code can be found in utility/Enc38j60Network.cpp.  In the Enc28J60Network::init function, there is a line that sets up the ERXFCON register:


This needs to simply be changed to clear the register:

writeReg(ERXFCON, 0);

Next, how to store the destination IP address.  Again, simplicity ruled this one too.

The high-level interface to the library can be found in UIPEthernet.cpp and UIPEthernet.h.  I added two public functions and two private variables to the UIPEthernetClass class in UIPEthernet.h:

  IPAddress lastSrcIP();
  IPAddress lastDestIP();
  static uip_ipaddr_t uip_lastipsrc;
  static uip_ipaddr_t uip_lastipdest;

Then in the UIPEthernet.Cpp file, added the code to store the last source and destination IP addresses from received packets.

First, define a structure to dig into to the IP header (a bit of a layer violation, but I wasn’t after neat designs really).  Add the following after the definition of ETH_HDR near the top of the file.

#define IPBUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])

Then add two (static) global variables to the file:

uip_ipaddr_t UIPEthernetClass::uip_lastipsrc;
uip_ipaddr_t UIPEthernetClass::uip_lastipdest;

Add two accessor methods to retrieve the last source and destination IP addresses (with appropriate conversion to the Arduino Ethernet friendly IPAddress format):

IPAddress UIPEthernetClass::lastSrcIP()
  return ip_addr_uip(uip_lastipsrc);
IPAddress UIPEthernetClass::lastDestIP()
  return ip_addr_uip(uip_lastipdest);

Finally add the code to save the addresses to the UIPEthernetClass::tick() function, on reception of a packet.

          if (ETH_HDR ->type == HTONS(UIP_ETHTYPE_IP))
              uip_packet = in_packet; //required for upper_layer_checksum of in_packet!
              Serial.print(F("readPacket type IP, uip_len: "));
              if (uip_len > 0)
                // Extra code added here
                uip_ipaddr_copy(uip_lastipsrc, IPBUF->srcipaddr);
                uip_ipaddr_copy(uip_lastipdest, IPBUF->destipaddr);
                // Extra code ends

That should be all that is required to expose the destination IP address of any received packet via the UIPEthernet class (ok, breaking compatibility now with the standard Arduino Ethernet library).

The arduino sketch file now consists of the following:

MIDI based on 

UIPEthernet Examples used for rest
NB: Requires hacked UIPEthernet Library!
#include <SPI.h>
#include <UIPEthernet.h>
#include "IPAddress.h"

int lastIP;
int thisIP;

// Initialise note array with whole tone scales in octaves 3 through to 6
// C3 = 36
// C4 = 48
// C5 = 60
// C6 = 72
// C7 = 84
#define NOTES 24
int notes[NOTES] = {
  // C3  D3  E3  F#3 G#3 A#3
     36, 38, 40, 42, 44, 46,
  // C4
     48, 50, 52, 54, 56, 58,
  // C5
     60, 62, 64, 66, 68, 70,
  // C6
     72, 74, 76, 78, 80, 82

void setup() {
  //  Set MIDI baud rate:

  // Initialise the uIP and UIPEtherent stacks
  uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
  IPAddress myIP(192,168,0,6);
  lastIP = 0;
  thisIP = 0;

  // Initialise patch using program change
  // 32 = Synth 1, Fantasy
  midiCmd (0xC0, 32);

void loop() {
  IPAddress sip = Ethernet.lastSrcIP();
  IPAddress dip = Ethernet.lastDestIP();
  thisIP = 256*dip[2] + dip[3];
  if (thisIP != lastIP)
    lastIP = thisIP;
    int note = (thisIP & 0xff) % NOTES;  // Scale to number of notes
    int vel  = ((thisIP & 0xff00) >> 8)/4;        // Scale to val between 0 and 64

    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, notes[note], 16+vel);

//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(int cmd, int pitch, int velocity) {

void midiCmd(int cmd, int val) {

The final byte of the IP address determines which note from the array of notes to play (modulo the number of notes) and the third byte determines the volume to be used, scaled and with a minimum specified.

There is a control message to set the voice on the MT-32 to Synth1-Fantasy as this sounds suitably ambient. There are no note-off messages, so notes are allowed to keep ringing. As the note array defines four octaves of whole tone scales, the running-on notes create quite an interesting effect.

2015-12-01 21.40.12

There seems to be a regular drone set up, which I think is due to the IP address of my PC and router. These two addresses provide a sort of default back-drop of sound to anything else going on.

In order to get anything useful though, it would be no good just using a port on the router, as even the dumbest, cheap modern router will tend to do some MAC level filtering on ports. I had a laptop and the Arduino plugged into an old Netgear En104 4-port Ethernet hub, which has no intelligence (as far as I know) built in – so the Arduino could see everything coming out of the laptop.

The results were quite pleasing. Google has a nice enhanced drone to it. You can really hear the clutter of a site that is pulling in ads from all over the Internet – such as Amazon or YouTube or a news site.

Edited to add:  Here is a short video below showing them being opened.  It’s a bit crude but you get the general idea.

 Maybe next, I’ll see if I can do the same with a Wi-Fi link for the Arduino instead of wired Ethernet.  It might also be worth trying different scales and alternative mappings of notes to addresses.



Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: