read bluetooth slow or missing data

4 visualizzazioni (ultimi 30 giorni)
Karson Bader
Karson Bader il 10 Nov 2021
Commentato: Karson Bader il 10 Nov 2021
I am reading 3 sensors data via bluetooth low energy (ble) from an arduino. The code, for the arduino has the sensor data being sent 1 sensor after another:
sensor 1 → send using bluetooth
sensor 2 → send using bluetooth
sensor 3 → send using bluetooth
When reading the sensor data via bluetooth using matlab, the data is NOT received in order or received at all. In most cases one or more of the sensors are skipped multiple times such as:
Order of data that is read in when connected via bluetooth using matlab:
sensor 1
sensor 1
sensor 2
sensor 1
sensor 3
When I connect the arduino to an android phone, the data is received in order, as expected:
sensor 1
sensor 2
sensor 3
etc………
Is there a way to increase the rate at which the data is being read by matlab or could there be another reason why the data being read in by matlab is not in order, but the data is read in order when using an android device?
Matlab code to read sensor and count which sensor is being read:
b = ble("BLUENRG");
c = characteristic(b,b.Characteristics{6,2},b.Characteristics{6,4});
subscribe(c,'notification')
clearvars -except b c
tic
count1 = 1;
count2 = 1;
count3 = 1;
sensorArray = [0 0 0];
data = 20;
k = 1;
loopLimit = data + 1;
while k < loopLimit
while dot(sensorArray,sensorArray) ~= 3
data = char(read(c));
bin = str2double(data(1,1))
if (sensorArray(1,bin) == 0)
switch bin
case 1
count1 = count1 + 1;
sensorArray(1,1) = 1;
values(k,1) = toc;
case 2
count2 = count2 + 1;
sensorArray(1,2) = 1;
values(k,2) = toc;
case 3
count3 = count3 + 1;
sensorArray(1,3) = 1;
values(k,3) = toc;
end
end
end
k = k + 1
sensorArray = [0 0 0];
end
%{
for j = 1: size(values,1)
deltaSpread(j,1) = max(values(j,:)) - min(values(j,:));
end
mean(deltaSpread)
std(deltaSpread)
%}
toc
  2 Commenti
Walter Roberson
Walter Roberson il 10 Nov 2021
Could you confirm that what is sent on the channel is something that starts with a character that is the sensor number?
Note that you are growing your values array in place, which takes increasing time as it gets larger. You should preallocate it according to looplimit.
Karson Bader
Karson Bader il 10 Nov 2021
Walter,
I did what you recommended and the results where the same. BUT I did find a solution:
(From the help center): Use a callback function
'Use the @ operator to assign the function handle to the DataAvailableFcn property of the characteristic. When a new notification is available, the data appears in your command window.'
Code used:
c.DataAvailableFcn = @displayCharacteristicData;
function displayCharacteristicData(src,evt)
[data , timestamp] = read(src,'oldest')
disp(char(data))
disp(timestamp)
end
Unfortunately, I am not sure how to have the call back function stopped after a given number of iterations. Any suggesstions?
To answer your question/ address the issue you pointed out:
1) The data being recieved does start with the sensor number. Here is a small sample:
'1&0.1&30&0.2'
'1&0.1&30&0.2'
'2&0.0&0.0&0.0'
'1&0.0&0.0&1.0'
2) Preallocation didn't help.

Accedi per commentare.

Risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by