User Tools

Site Tools


python-osc-arduino

Python code used to receive OSC transmission and communicate the results to Arduino

""" pythonic test on Arduino Yun
"""
import codecs, time
from datetime import datetime
import OSC, threading

print "i am here"

f_out = codecs.open("/mnt/sd/arduino/www/pajton.csv", "w", encoding="utf-8")
currentTime = datetime.fromtimestamp(int(time.time())).strftime('%Y-%m-%d %H:%M:%S')
f_out.write('pisao'+str(currentTime))

# tupple with ip, port
receive_address = '0.0.0.0', 50000

# OSC Server. there are three different types of server.
s = OSC.OSCServer(receive_address) # basic

# this registers a 'default' handler (for unmatched messages),
# an /'error' handler, an '/info' handler.
# And, if the client supports it, a '/subscribe' & '/unsubscribe' handler
s.addDefaultHandlers()

def all_handler(addr, tags, stuff, source):
    print "received new osc msg from %s" % OSC.getUrlStr(source)
    f_out.write(OSC.getUrlStr(source)+'; ')
    print "with addr : %s" % addr
    f_out.write(addr+'; ')
    print "typetags %s" % tags
    print "data %s" % stuff
    f_out.write(addr+'; ')
    f_out.write(str(stuff[0])+'; ')
    f_out.write(str(time.time())+'; ')
    f_out.write('\n')


s.addMsgHandler("/conversation", all_handler) # adding our function
s.addMsgHandler("/data", all_handler)
s.addMsgHandler("/sms", all_handler)
s.addMsgHandler("/signal", all_handler)

# checking which handlers we have added
print "Registered Callback-functions are :"
for addr in s.getOSCAddressSpace():
    print addr

# Start OSCServer                                                  
print "\nStarting OSCServer. Use ctrl-C to quit."                  
st = threading.Thread(target = s.serve_forever)                    
st.start()                                                         
                                                                   
try:                                                               
    while 1:                                                       
        time.sleep(5)                                              
                                                                   
except KeyboardInterrupt:                                          
    print "\nClosing OSCServer."                 
    s.close()                                    
    print "Waiting for Server-thread to finish"  
    st.join() ##!!!                              
    print "Done"                                 
    f_out.close()  
python-osc-arduino.txt · Last modified: 2014/05/26 09:01 by zoza