SensorApps / Sensors2OSC

Android app for sending sensor data via Open Sound Control (OSC).
http://sensors2.org/osc
Other
66 stars 18 forks source link

Test server in Python #26

Closed PanderMusubi closed 2 years ago

PanderMusubi commented 4 years ago

For those who want to quickly start experimenting with this app, a test server in Python might come in handy. Below is one that could be added to the documentation to get people started without having to create one themselves. Advantage of this server is that it generically prints all data from Sensors2OSC in a nicely formatted way.

#!/usr/bin/env python3

# Prerequisits for Ubuntu
#     sudo apt-get install python3-wheel
#     sudo pip3 install python-osc

from pythonosc import dispatcher, osc_server
from socket import AF_INET, SOCK_DGRAM, socket

def get_ip_address():
    s = socket(AF_INET, SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    address = s.getsockname()[0]
    s.close()
    return address

def handler(addr, *message):
    output = '{:32}'.format(addr)
    for i in range(len(message)):
        output += ' {:8.3f}'.format(message[i])
    print(output)

if __name__ == "__main__":
    address = get_ip_address()
    port = 9000

    dispatcher = dispatcher.Dispatcher()
    dispatcher.map('/*', handler)

    server = osc_server.ThreadingOSCUDPServer((address, port), dispatcher)
    print('OSC server started on {}:{}'.format(address, port))
    server.serve_forever()