This wiki has been archived and made read-only.
For up-to-date information about TkkrLab and it's projects please visit our main website at tkkrlab.nl.

Arduino UDP multicast

From

Jump to: navigation, search

MIDI using multicast UDP

We want to use the Floppy Jukebox as a synthesizer for a MIDI keyboard. For this it would be handy that the Arduino accept multicast UDP packets.

Multicast is handy because several synthesizers can join a multicast IP address and respond to MIDI events simultaneously.

Arduino UDP Multicast

The default arduino ethernet library doesn't support multicast, however the W5100 chip on the ethernet shield does support one multicast address per socket.

W5100 data sheet Media:w5100.pdf

A multicast capable library compatible with the standard Ethershield library is now available: Media:EthernetMulti.tar.gz

One method UdpClass::beginMulti(uint16_t portMulti, uint8_t * addrMulti) has been added. the parameters are the UDP port portMulti, like UdpClass::begin(uint16_t port), and a multicast IP address addrMulti (no class D checking yet).

Modified example program UDPSendReceiveString listening on multicast address 239.0.0.57 port 12345

/*
  UDPSendReceive.pde:
 This sketch receives UDP message strings, prints them to the serial port
 and sends an "acknowledge" string back to the sender
 
 created 21 Aug 2010
 by Michael Margolis
 adapted by Henri Manson
 
 This code is in the public domain.
 */
 
 
#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <Udp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008
 
 
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 
  192,168,100,177 };
unsigned int localPort = 8888;      // local port to listen on
 
// Multicast
byte ipMulti[] = {
  239,0,0,57 };
unsigned int portMulti = 12345;      // local port to listen on
 
// the next two variables are set when a packet is received
byte remoteIp[4];        // holds received packet's originating IP
unsigned int remotePort; // holds received packet's originating port
 
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back
 
void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
// original unicast UDP
  //Udp.begin(localPort);
// multicast UDP
  Udp.beginMulti(portMulti, ipMulti);
 
  Serial.begin(9600);
}
 
void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.available(); // note that this includes the UDP header
  if(packetSize)
  {
    packetSize = packetSize - 8;      // subtract the 8 byte header
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
 
    // read the packet into packetBufffer and get the senders IP addr and port number
    Udp.readPacket(packetBuffer,UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort);
    Serial.println("Contents:");
    Serial.println(packetBuffer);
    Udp.sendPacket( ReplyBuffer, remoteIp, remotePort);
  }
  delay(10);
}

IP MIDI

From multimidicast
Default multicast address: 225.0.0.37
Default port number: 21928