My current project involves three raspberry pies playing video in sync. For this to work, they need to communicate with each other so one of them can trigger the others. Using pyosc, they can do so over ethernet in a python script.
"""
For detailed info on pyOSC functionality check the OSC.py file
or run pydoc pyOSC.py. you can also get the docs by opening a python shell and doing
>>> import OSC
>>> help(OSC)
This is a simple setup for 3 machines to communicate together.
"""
import OSC
import time, threading, socket
ip = socket.gethostbyname(socket.gethostname())
if ip == '192.777.0.0':
receive_address = '192.777.0.0', 9000
send_address1 = '192.777.0.1', 9000
send_address2 = '192.777.0.2', 9000
elif ip == '192.777.0.1':
send_address1 = '192.777.0.0', 9000
receive_address = '192.777.0.1', 9000
send_address2 = '192.777.0.2', 9000
elif ip == '192.777.0.2':
send_address1 = '192.777.0.0', 9000
send_address2 = '192.777.0.1', 9000
receive_address = '192.777.0.2', 9000
### OSC clients
# this sets up clients for this machine to send messages to
clients = []
clients.append(OSC.OSCClient())
clients.append(OSC.OSCClient())
clients[0].connect( send_address1 ) # set the address for all following messages
clients[1].connect( send_address2 ) # set the address for all following messages
### OSC recieve
# this sets up this machine to recieve messages
s = OSC.OSCServer(receive_address) # basic
s.addDefaultHandlers()
# define a message-handler function for the server to call.
def printing_handler(addr, tags, stuff, source):
print "---"
print "received new osc msg from %s" % OSC.getUrlStr(source)
print "with addr : %s" % addr
print "typetags %s" % tags
print "data %s" % stuff
print "---"
s.addMsgHandler("/print", printing_handler) # adding our function
# Start OSCServer
print "\nStarting OSCServer. Use ctrl-C to quit."
st = threading.Thread( target = s.serve_forever )
st.start()
# single message
msg = OSC.OSCMessage()
msg.setAddress("/print") # set OSC address
msg.append(44) # int
msg.append(4.5233) # float
msg.append( "the white cliffs of dover" ) # string
[c.send(msg) for c in clients] # send it!
# bundle : few messages sent together
# use them to send many different messages on every loop for instance in a game. saves CPU and it is faster
bundle = OSC.OSCBundle()
bundle.append(msg) # append prev mgs
bundle.append( {'addr':"/print", 'args':["bundled messages:", 2]} ) # and some more stuff ...
bundle.setAddress("/*print")
bundle.append( ("no,", 3, "actually.") )
[c.send(msg) for c in clients] # send it!