How can I transform incoming BLE data in matlab to normal decimal values.

2 views (last 30 days)
i am trying to decode the sensorvalues that i send from my microcontroller via ble to matlab. however if I check the data it is displayed as ASCII and not as a decimal number.
% Connect to the BLE device
b = ble("C4DEE2C32522");
% Define the service UUID and characteristic UUID
serviceUUID = "4fafc201-1fb5-459e-8fcc-c5c9c331914b";
charUUID1 = "beb5483e-36e1-4688-b7f5-ea07361b26a8";
% Create a characteristic object
char = characteristic(b, serviceUUID, charUUID1);
% Subscribe to notifications
subscribe(char);
% Read data as unsigned 8-bit integers
data = read(char);
I am using this code here. For testing measures I am sending a constant value via BLE: 743.50 from my microcontroller.
matlab com window displays the read(char) as 55 52 51
i do not know how to parse this ASCII (if I am not mistaken) into a numerical value

Answers (1)

Jack
Jack on 30 Mar 2023
The values you are seeing, 55 52 51, are the ASCII codes for the characters '7', '4', and '3' respectively. To convert these ASCII codes to the numerical value you sent from your microcontroller, you can use the str2double function in MATLAB.
Here's how you can modify your code to convert the received data to a numerical value:
% Connect to the BLE device
b = ble("C4DEE2C32522");
% Define the service UUID and characteristic UUID
serviceUUID = "4fafc201-1fb5-459e-8fcc-c5c9c331914b";
charUUID1 = "beb5483e-36e1-4688-b7f5-ea07361b26a8";
% Create a characteristic object
char = characteristic(b, serviceUUID, charUUID1);
% Subscribe to notifications
subscribe(char);
% Read data as unsigned 8-bit integers
data = read(char);
% Convert the received data to a numerical value
num_val = str2double(char(data)');
disp(num_val);
This code converts the data received from the BLE characteristic to a string using the char function, and then uses the str2double function to convert the string to a numerical value. The resulting numerical value is stored in the num_val variable and printed to the console using the disp function.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!