read bluetooth slow or missing data
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2021-11-10
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.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Modeling 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!