OSC for Arduino and Embedded Processors

This project provides an OSC library for Arduino, Teensy and related embedded processor platforms. It is the most feature-rich implementation of the OSC encoding for these platforms.

Features:

  • Supports the four basic OSC data types (integers, floats, strings, and blobs) and some common type extensions
  • Send and receive messages over any transport layer that implements the Arduino’s Stream Class such as Serial, EthernetUdp, and more.
  • Address pattern matching
  • Dynamic memory consumption
  • Compatible with Arduino 1.0 API and coding style
  • Includes many examples for various host applications including Max/MSP and PD

Getting Started

Start by downloading the OSC for Arduino library from github. To #include the library in your sketch, unzip the folder and place it in a folder titled “libraries” in your default sketch directory. If the name of this folder ends in “-master” please change the folder name to just “OSC”. Then import the library by selecting it under Sketch->Import Library. Find the full instructions on creating and importing libraries on the Arduino website.

Basics

OSC for Arduino provides two classes: OSCMessage and OSCBundle. OSCMessage contains the address, type, and data and OSCBundle is a bundle of OSCMessages with a timetag. To start sending data over the Arduino’s Serial port simply construct an OSCBundle with Serial as the constructor argument, add some OSCMessages with some data, and send it off:


#include <OSCMessage.h>

//Teensy and Leonardo variants have special USB serial
#if defined(CORE_TEENSY)|| defined(__AVR_ATmega32U4__)
#include <SLIPEncodedUSBSerial.h>
SLIPEncodedUSBSerial SLIPSerial(Serial);
#else
// any hardware serial port
#include <SLIPEncodedSerial.h>
SLIPEncodedSerial SLIPSerial(Serial);
#endif


void setup() {
  //begin SLIPSerial just like Serial
  Serial.begin(38400);
  while(!Serial)
    ; //Leonardo "feature"
}


void loop(){
  //the message wants an OSC address as first argument
  OSCMessage msg("/analog/0");
  msg.add(analogRead(0));
  msg.send(SLIPSerial); // send the bytes to the SLIP stream
  SLIPSerial.endPacket(); // mark the end of the OSC Packet
  msg.empty(); // free space occupied by message

  delay(20);
}