This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
python-osc-arduino [2014/05/26 08:43] zoza |
python-osc-arduino [2014/05/26 09:01] zoza |
||
---|---|---|---|
Line 6: | Line 6: | ||
import codecs, time | import codecs, time | ||
from datetime import datetime | from datetime import datetime | ||
+ | import OSC, threading | ||
print "i am here" | print "i am here" | ||
- | # open a file on the SD card and write the date and time of access | ||
f_out = codecs.open("/mnt/sd/arduino/www/pajton.csv", "w", encoding="utf-8") | 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') | currentTime = datetime.fromtimestamp(int(time.time())).strftime('%Y-%m-%d %H:%M:%S') | ||
f_out.write('pisao'+str(currentTime)) | f_out.write('pisao'+str(currentTime)) | ||
- | f_out.close() | ||
+ | # 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() | ||
</code> | </code> |