Raspberry Pi and OSC: Part 2

In part one we installed and tested the pyOSC library on the Raspberry Pi. In this tutorial we will go through the Python code to send OSC on the Raspberry Pi.

I am going to continue using the command line, however the actual python code can be written in any text editor in GUI.

In the shell type:

nano myOSC.py

This will bring up the nano text editor(a very useful tool to learn when doing command line editing). Here is some more information on how to effectively use this tool.

You will be brought to a nearly blank screen within the command line. This is where we will write our program. Start off by typing:

#!/user/bin/python

This lets the text interpreter know what language we are using.

The following is the full code to send a OSC message:

import OSC

send_address = '192.168.1.1' , 9000

c = OSC.OSCClient()
c.connect(send_address)

msg = OSC.OSCMessage()
msg.setAddress("/print")
msg.append(44)

c.send(msg)

This is all its takes to send an OSC message. The message being received at 192.168.1.1:9000 will be interpreted by an OSC listener with a stream address of “/print” and the data as 44.

I will take you though each line of code and explain what it does:

import OSC

Imports OSC library. Kind of important seeing that’s what we want to use.

send_address = '192.168.1.1' , 9000

Creates a tuple for the IP address and port of the machine receiving OSC.

c = OSC.OSCClient()
c.connect(send_address)

This creates a new OSC client named “c”, and then gives “c” the address and the port specified earlier.

msg = OSC.OSCMessage()

This creates a new OSC message under the variable “msg”.

msg.setAddress("/print")
msg.append(44)

These lines give “msg” its stream address(“/print”) and adds a data value off 44 to it. We can send ints, floats, and strings as appends and we can add as many appends as we like, but only one stream address per message.

c.send(msg)

And finally we send “msg” to the client “c”.

This code can be easily adapted to send OSC when an event happens, such as a button being pushed, a knob being turned, or an object appearing on a camera. One example of using this is with the IOS app Luminair, which is a tablet based lighting controller. Details on how to use OSC with Luminair can be found in the Luminair user manual page 56. There it lists out all of the OSC commands you can use to control the app.