lunedì 16 gennaio 2012

Datalogger su Bluetooth

Esempio di comunicazione via Bluetooth
Per funzionare si deve caricare sia sul telefono che sul computer la libreria PyBluez.
Per il telefono si deve seguire la procedura per importare i moduli di Py4a e quindi si deve scaricare il file .egg da posizionare poi nella directory download della scheda SD
Per la libreria sul computer ho riscontrato che scaricando i file tgz ed eseguendo python setup.py install si genera un errore di compilazione su gcc. Per questo motivo ho installato il pacchetto Debian gia' compilato python-bluez
----------------------------------------------------------------------------------------------------------------------------
# file: rfcomm-server.py
# auth: Albert Huang <albert@csail.mit.edu>
# desc: simple demonstration of a server application that uses RFCOMM sockets
#
# $Id: rfcomm-server.py 518 2007-08-10 07:20:07Z albert $


from bluetooth import *


server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)


port = server_sock.getsockname()[1]


uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"


advertise_service( server_sock, "SampleServer",
                   service_id = uuid,
                   service_classes = [ uuid, SERIAL_PORT_CLASS ],
                   profiles = [ SERIAL_PORT_PROFILE ], 
#                   protocols = [ OBEX_UUID ] 
                    )
                   
print "Waiting for connection on RFCOMM channel %d" % port


client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info


try:
    while True:
        data = client_sock.recv(1024)
        if len(data) == 0: break
        print "%s" % data
except IOError:
    pass


print "disconnected"


client_sock.close()
server_sock.close()
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------

# file: rfcomm-client.py
# auth: Albert Huang <albert@csail.mit.edu>
# desc: simple demonstration of a client application that uses RFCOMM sockets
#       intended for use with rfcomm-server
#
# $Id: rfcomm-client.py 424 2006-08-24 03:35:54Z albert $


from bluetooth import *
import sys
import android
import time
import math


addr = None


droid = android.Android();
droid.startSensingTimed(1,1000)
time.sleep(3)


uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
service_matches = find_service( uuid = uuid, address = addr )


if len(service_matches) == 0:
    print "couldn't find the SampleServer service =("
    sys.exit(0)


first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]


sock=BluetoothSocket( RFCOMM )
sock.connect((host, port))


print "Connesso"
while True:
az = str(math.degrees(droid.sensorsReadOrientation().result[0]))
pitch = str(math.degrees(droid.sensorsReadOrientation().result[1]))
roll =  str(math.degrees(droid.sensorsReadOrientation().result[2]))
    data = az+";"+pitch+";"+roll
    if len(data) == 0: break
    sock.send(data)


sock.close()

----------------------------------------------------------------------------------------------------------------------------