My current project involves three raspberry pies playing video in sync. For this to work, they need to communicate with each other so any one of them can trigger the two others. Using pyosc, they can do so over ethernet in a python script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
""" 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! |