Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nudomarinero
Created November 22, 2015 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nudomarinero/dc653c17574ec53e73f6 to your computer and use it in GitHub Desktop.
Save nudomarinero/dc653c17574ec53e73f6 to your computer and use it in GitHub Desktop.
Get the acceleration data of a LightBlue Bean using Python
from __future__ import print_function
from bluetooth.ble import GATTRequester
from time import sleep
def get_accel(data):
"""
Get the acceleration values from the data
TODO: Add CRC check
See: http://legacy.punchthrough.com/bean/the-arduino-reference/accelerationreading/
"""
if (hex(ord(data[6])) == "0x20") and (hex(ord(data[7])) == "0x90"):
x = ord(data[9])*256+ord(data[8])
y = ord(data[11])*256+ord(data[10])
z = ord(data[13])*256+ord(data[12])
sensitivity = ord(data[14])
x_accel = x * sensitivity *1000 / 511
y_accel = y * sensitivity *1000 / 511
z_accel = z * sensitivity *1000 / 511
print("0 {} {} {} {}".format(x_accel, y_accel, z_accel, sensitivity))
else:
print("1")
class Requester(GATTRequester):
def on_notification(self, handle, data):
#print("- notification on handle: {}".format(hex(handle)))
#print("Data: {}".format("0x"+"".join([hex(ord(d))[2:].zfill(2) for d in data])))
get_accel(data)
try:
req = Requester("C4:BE:84:E5:76:ED", False)
req.connect(True)
req.write_by_handle(0x002f, str(bytearray([0x01, 0x00])))
req.write_by_handle(0x002e, str(bytearray([0x80, 0x02, 0x00, 0x20,
0x10, 0x7d, 0x7f, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00])))
sleep(1)
req.disconnect()
except RuntimeError:
print("2")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment