The TI BLE SensorTag is a portable low-power module that uses Blueooth Low Energy (BLE, Bluetooth 4.0) and various sensors to communicate data to any BLE reciever.
The python scripts that interface with the BeagleBone Black were gently modified from msaunby's Raspberry Pi Scripts.
- The Beaglebone Black runs BoneServer.js which creates a webserver on port 9090.
- The user selects the ballAndCube picture, which launches ballAndCube.html, ballAndCube.js, and the sensortag.py locally on the server (i.e. the BLE dongle is connected to the server not the client).
- The sensortag.py python script outputs data, which is handled as an event streamer by node.js, to stream the data to ballAndCube.js which renders object, and interprets the Gyroscope/Accelerometer Data.
- Since the python script, which uses gattool, runs in its own process, the data that it receives is streamed without buffering to the client's ballAndCube.js script.
Before You Start
- Retrieve the original python BLE scripts from this github repo.
- Execute the sensortag.py script to determine if it executes properly (i.e. your linux distribution comes with pexpect); if not, retrieve pexpect online (I used v3.2)
- Retrive the BLE Address of your sensortag
$hciconfig hci0 up $hciconfig hci0: ... UP RUNNING ... $hcitool lescan LE Scan ... 90:59:AF:0B:84:57 (unknown) 90:59:AF:0B:84:57 SensorTag ^^^ Is the BLE address of your sensorTag
- Modify sensortag.py with your given bluetooth_adr (see main), socket communication code, and 2's complement code.
import socket import os, os.path ... # 2's complement as per StackOverflow post def twos_comp(val, bits): """compute the 2's compliment of int value val""" if( (val&(1<<(bits-1))) != 0 ): val = val - (1<<bits) return val ... # In accelerometer function # User "client.send( )" instead of print, to send data to your node.js server client.send( "A " + str(xyz) + " " + str(xyz) + " " + str(xyz) ) # the "A", serves as a tag to distinguish between each of the sensors ... # In gyroscope function # join two bytes to form 16 bit number, make unsigned. dx = twos_comp( ( (v<<8) + v ) , 16 ) + 65536 dy = twos_comp( ( (v<<8) + v ), 16 ) + 65536 dz = twos_comp( ( (v<<8) + v ), 16 ) + 65536 client.send( "G " + str( dx )+ " " + str( dy ) + " " + str(dz) ) ... # In main(): global client soc_fd = "/tmp/py_soc" # ^^ Unix socket file that python makes, modify as necessary client = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM ) client.connect( soc_fd ) # ^^ sets up a Unix stream socket @ soc_fd (see socket man pages) ... # comment out any sensor init code for sensors you don't wish to use ... except (KeyboardInterrupt, SystemExit): # cleanup code client.close() os.remove( soc_fd ) raise except: pass
Sample C Code
****************************************************************/ #include <errno.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <poll.h> #include <signal.h> #include <unistd.h>