matlabbluetoothbluetooth-gatttilt-sensorbluetooth-hci

Get accelerometer data from ti sensortag using Matlab and HCI-command


I'm trying to get sensor data from ti sensortag using matlab, my work is based on the work posted in github site :

https://github.com/sid5291/SensorTag-Matlab

I'm trying to get accelorometer data instead of temperature and humidity data (in the original work)

Here is my code, the connection between matlab and sensortag is established and


GAP_initialise =  ['01';'00';'FE';'26';'08';'05';'00';'00';'00';'00';'00';'00';'00'
'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00'
'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'01'
'00';'00';'00'];

GAP_DeviceDiscovery = ['01';'04';'FE';'03';'03';'01';'00'];
SCAN_TYPE   = 1;
GAP_connect = ['01'; '09'; 'FE'; '09'; '00'; '00'; '00' ;'52';'DE'; 'AB' ;'29' ;'6A' ;'BC'];  
CONNECT_TYPE = 2;

% Reference    http://processors.wiki.ti.com/images/a/a8/BLE_SensorTag_GATT_Server.pdf
GATT_AccOn  = ['01'; '92'; 'FD'; '05'; '00'; '00'; '34'; '00'; '01'];
GATT_AccOff = ['01'; '92'; 'FD'; '05'; '00'; '00'; '34'; '00'; '00'];
WRITE_TYPE  = 3;
GATT_AccRd  = ['01'; '8A'; 'FD'; '04'; '00'; '00'; '30' ;'00' ];
READ_TYPE   = 4;

disp('Going to Intialize');
HCI_TXRX(GAP_initialise);
disp('Going to Scan');
disp('Make Sure Led D1 is blinking on Sensor Tag');
disp('Wait for Scan To End');
input('Press any key to continue');
HCI_TXRX(GAP_DeviceDiscovery,SCAN_TYPE);
disp('Going to Connect to Sensor Tag');
disp('LED D1 will turn off when Connected, if doesnt there is an error');

input('Press any key to continue');
HCI_TXRX(GAP_connect,CONNECT_TYPE);
disp('Going to Turn On Acc');
input('Press any key to continue');
HCI_TXRX(GATT_IRTAccOn,WRITE_TYPE);
disp('Going to Read from Sensors');
input('Press any key to continue');

while(1)
result = HCI_TXRX(GATT_AccRd,READ_TYPE);
x = hex2dec(result(1,:))/64;
y = hex2dec(result(2,:))/64;
z = hex2dec(result(3,:))/64;

char = input('Press any key to continue (x to exit)','s');
if(char == 'x')
    break;
end
 end 

Can any one help me to solve this problem please


Solution

  • i have found the error : just you need to put the right value of the accelerometer service which is :'31' for configuration, '2D' for reading data

     clear all
     close all
     clf
    GAP_initialise=['01';'00';'FE';'26';'08';'05';'00';'00';'00';'00';'00';'00';'00'
    '00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00'
    '00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'01'
    '00';'00';'00'];
    GAP_DeviceDiscovery = ['01';'04';'FE';'03';'03';'01';'00'];
    SCAN_TYPE = 1;
    GAP_connect = ['01'; '09'; 'FE'; '09'; '00'; '00'; '00' ;'52';'DE'; 'AB' ;'29' ;'6A' ;'BC'];  %BC:6A:29:AC:50:46 (1325 Tag)
    CONNECT_TYPE = 2;
    
    % Reference http://processors.wiki.ti.com/images/a/a8/BLE_SensorTag_GATT_Server.pdf
    
    % 0x31 is the address of the acceloremeter config in the GATT server
    
    GATT_AccOn = ['01'; '92'; 'FD'; '05'; '00'; '00'; '31'; '00'; '01'];
    GATT_AccOff = ['01'; '92'; 'FD'; '05'; '00'; '00'; '31'; '00'; '00'];
    
    % 0x31 is the address of the acceloremeter config in the GATT server
    
    
    GATT_AccPerMin = ['01'; '92'; 'FD'; '05'; '00'; '00'; '34'; '00'; '0A'];
    GATT_AccNoti = ['01'; '92'; 'FD'; '05'; '00'; '00'; '2E'; '00'; '01'];
    
    WRITE_TYPE = 3;
    
    % 0x2D is the address used to read the acceloremeter data in the GATT server
    
    GATT_AccRd = ['01'; '8A'; 'FD'; '04'; '00'; '00'; '2D' ;'00' ]; 
    READ_TYPE = 4;
    
    disp('Going to Intialize');
    HCI_TXRX(GAP_initialise);
    disp('Going to Scan');
    disp('Make Sure Led D1 is blinking on Sensor Tag');
    disp('Wait for Scan To End');
    input('Press any key to continue');
    HCI_TXRX(GAP_DeviceDiscovery,SCAN_TYPE);
    disp('Going to Connect to Sensor Tag');
    disp('LED D1 will turn off when Connected, if doesnt there is an error');
    HCI_TXRX(GAP_connect,CONNECT_TYPE);
    disp('Going to Turn On Acc');
    
    HCI_TXRX(GATT_AccOn,WRITE_TYPE);
    HCI_TXRX(GATT_AccPerMin,WRITE_TYPE);
    % HCI_TXRX(GATT_AccNoti,WRITE_TYPE);
    disp('Going to Turn On Accelerometer');
    
    i =1;
    clf
    while(1)
    result = HCI_TXRX(GATT_AccRd,READ_TYPE);
    
    % result(1,:) is not used (data from acc x:y:z  3 bytes )
    rawval = hex2dec([result(2,:); result(3,:); result(4,:)]);
    
    xval = rawval(1)/64; % calculate acc, unit g, range -2, +2
    yval = rawval(2)/64; 
    zval = rawval(3)/64; 
    
    % show data
    
    fprintf('Acc X: %f \n',xval);
    fprintf('Acc Y: %f \n',yval);
    fprintf('Acc Z: %f \n',zval);
    
    
    %char = input('Press any key to continue (x to exit)','s');
    
    
    % read 10 sample from accerometer 
    
    if(i == 100)
        break;
    end
    
    % update table of samples 
    
    graphe_x(i) = xval;
    graphe_y(i) = yval;
    graphe_z(i) = zval;
    
    % update graph with new samples
    figure(1), plot(graphe_x,'r'), hold on
    plot(graphe_y,'g'), hold on
    plot(graphe_z,'b')
    
    
    % increment counter 
    
    i = i + 1;
    
    
    end