TI SensorTag

From eLinux.org
Revision as of 15:16, 21 April 2014 by Alvareap (talk | contribs) (Before You Start)
Jump to: navigation, search



Overview

--

BBB interfaceing with Adafruit's BLE Module

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.

TI Sensortag Wiki


The python scripts that interface with the BeagleBone Black were gently modified from msaunby's Raspberry Pi Scripts.

System Outline

  • 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.

Necessary Modifications

  • 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[0]) + " " + str(xyz[1]) + " " + str(xyz[2]) )
   # 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[1]<<8) + v[0] ) , 16 ) + 65536 
   dy =  twos_comp(  (  (v[3]<<8) + v[2]  ), 16 ) + 65536
   dz =  twos_comp( (  (v[5]<<8) + v[4]  ), 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
  • Modify BoneServer.js by appending said code:
   function sensorInit( socket ) { 
       /////////////////////////////////////////////////////////////////////////////
       // Executing Python script that communicates with Sensortag
       /////////////////////////////////////////////////////////////////////////////
    
       var net = require('net');
   
       // upon connection to ballAndCube.js via 'SensorTag' message, run sensorTag python script
       socket.on('SensorTag', function () { 
           
           // This server listens on a Unix socket at /tmp/py_soc
           var unixServer = net.createServer(function(client) {
               // Send data as it's recieved 
               client.on( 'data', function(data) {
                   var chunk;
                   // Prints recieved data to console
                   //console.log ("data: ", data.toString() );
                   socket.emit( 'SensorTag' , data.toString() );
               } );
   
               client.on('end', function() {
                   console.log('client disconnected');
               });
           });
            //location of the python socket as specified in sensortag.py 
           unixServer.listen('/tmp/py_soc');
   
            // EXEC python sensortag.py at given location (NOTE SPECIFIC TO YOUR LOCATION)
           var exec = require('child_process').exec, child;
   
           child = exec(  '~/ece497/exercises/realtime/sensortag.py' , function ( error, stdout, stderr) {	
       	    console.log( stdout );
                       if (error !== null) {
       		    console.log('exec error: ' + error );
       	        }	
           } );
       } );
   }

Upon receiving a 'SensorTag' message, the above code initializes a Unix socket server and the sensortag.py script. The SensorTag message comes from ballAndCube.js

  • Call sensorInit in body of
   io.sockets.on('connection', function (socket) {
       ...
       sensorInit(socket)
       ...
   }

Communication Flow

BallAndCube123.jpg

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>