UUID for a service and its characteristics.
This chapter presents Bluetooth Low Energy (BLE) applications in MATLAB. Through these applications we acquire measurement data from BLE compatible sensors to PC. The sensors are CC2541 Keyfob and CC2650 Sensor Tag. The first one contains an accelerometer and a temperature sensor while the second one contains more sensors, but inertial sensors and magnetometer are invoked. The PC should be equipped with a general USB BLE adapter. The most important steps for implementing a BLE application are presented: scanning, connecting, configuring and data reading. Following this, more detailed applications are presented: a wireless sensor network for temperature measurement with three Keyfob-based nodes, an application that displays in real time accelerometer data and a heading computed method using either the gyroscope or the magnetometer of CC2650 Sensor Tag. The most important MATLAB elements that are used to implement these applications are different types of variables such as structure, table and object, methods to implement endless loops and real-time display of acquired data and using quaternions to handle 3D orientation of a device.
- Bluetooth low energy
- temperature sensors
- movement sensors
- callback function
- 3D orientation
MATLAB represents a programming language that is used for designing, simulating and testing of different technical systems . This chapter provides examples of Bluetooth low energy (BLE) applications implemented in MATLAB. In this section, the main aspects regarding developing a BLE MATLAB application are presented. First of all, basics about BLE technology are presented [2, 3, 4, 5]. BLE means exchange data between two or more devices by radio waves over short distances. Mainly, a BLE device can be scanner or advertiser. The advertiser signals its presence by sending its name and address. The scanner finds advertiser devices and can connect to one or more of them. Then the advertiser becomes a server and can send data to the scanner which is now a client. According to BLE architecture, the server can offer services to the client. Some examples of services are battery service, accelerometer service and heart rate measurements. Each service contains more characteristics. The most important attribute of a characteristic is its value, which in general represents sensor data. In addition, a characteristic has one or more of the following properties: read, write and notify.
Starting with 2019b release, MATLAB has introduced a set of functions that allow a simple implementation of BLE application . The minimum setup involves a laptop having an embedded BLE adaptor or a desktop having an USB BLE adapter and some BLE compatible devices.
Scanning for BLE devices can be done using
It can be seen that three BLE devices have been discovered. The connection with Keyfobde99 is achieved, and b1 is a
A characteristic can be accessed either by service name and characteristic name or by service universal unique identifier (UUID) and characteristic (UUID) . According to the information from Figure 2 it can be seen that only the second option can be used because there are more Custom services or characteristics. Furthermore, there is no information about the functionalities of these services or characteristics. Therefore, to start the application, some information is necessary that can be obtained using another application such as BLE Device Monitor  or just Wikipedia . Thus, Table 1 presents service name and characteristic name among to UUID for a part of the positions of Figure 2.
|Service Name||UUID||Characteristic Name||UUID|
|Accelerom. service||FFA0||Accelerometer enable||FFA1|
|Accelerometer X coordinate||FFA3|
|Accelerometer Y coordinate||FFA4|
|Accelerometer Z coordinate||FFA5|
|Period of Reading accelerometer data||FFA6|
In order to access a characteristic, the
Another powerful features is
Each of the following sections contains an introduction where the basic function of the program is presented, which is then followed by the program and the results, mainly in graphical form.
2. BLE network sensors for temperature monitoring
This section presents a MATLAB application that uses three temperature sensors. CC2541 Keyfob  is a BLE compatible device that contains an accelerometer. Among its basic function, the accelerometer contains an 8-bit temperature sensor. To access the temperature sensor the accelerometer must be enabled first and then the temperature characteristic can be accessed according to Table 1. The period of reading temperature is 3 sec. according to the author publication .
At the beginning of the program a general scanning is executed and if none of the desired sensors are discovered the application is stopped through a suitable message on the screen. To do this, accessing the elements of a variable of table type,
Then, depending on the discovered number of sensors, which can be from one to any number (three in this application) the application gets temperature from them and displays it on a graphic. For this purpose, two structures,
The structure of the program is presented below.
The program runs in an endless loop and displays the last 20 values of the three temperatures in a MATLAB figure as in Figure 5. To stop the program, simply close the figure. In addition, the current date and time is displayed on the figure. One of the CC2541 Keyfob was on the outside sill of the window and therefore the resulting temperature was about 5 degree Celsius.
3. Using the accelerometer of CC2541Keyfob
This section presents a MATLAB application that accesses the accelerometer of the CC2541 Keyfob to read the 8-bit accelerations corresponding to the three axes. The program is similar to that of the previous section. There are also three callback functions, one for each axes. The period of reading data is set to 100 ms.
The program runs in an endless loop and displays the last N=200 samples of each the three axes. Figure 6 presents a screenshot during the running of the program. During this time CC2541 Keyfob was moved such as one of the three axes was on the direction of gravitational force. Thus, most of the time one of the three axes has the absolute value close to g=9.81 m/s2 while the other two are close to zero.
4. Using the movement sensor of CC2650 Sensor Tag
This section presents a MATLAB application that accesses the movement sensor of the device called CC2650 Sensor Tag. This movement sensor contains an accelerometer, a gyroscope and a magnetometer. If the accelerometer of the CC2541 Keyfob which was presented in the third section generates 8-bit data, all of the three sensors of CC2650 Sensor Tag generates 16-bit data.
The gyroscope is a three axis sensor that measures the angular rate,
Thus, Eq. (1) can be implemented by trapezoidal method by using samples of
This angle is considered in comparison with the initial position of the gyroscope which is unknown. Using the integration, it generates an error because the gyroscope has an offset. That means its output is different to zero when the gyroscope is still. Thus, by integration it follows that the angle is changed. Therefore this offset must be removed .
The magnetometer measures the magnetic field. Thus, if there are no other fields, it measures the magnetic field of the earth. When the magnetometer is placed horizontally, it can measure the angle from the north,
Regarding BLE, CC2650 Sensor Tag offers more services. The service that allows accessing the accelerometer, the gyroscope and the magnetometer has three characteristics, as shown in Table 2, where also the UUID can be seen. The first one is used to read data. The second one is used to enable the sensors. Each axis of the gyroscope and accelerometer can individually be enabled while the magnetometer can be enabled only for all axes. The third characteristic allows to establish the period of data reading. The data is presented as an 18 bytes string, where each sensor has a field of 6 bytes, two bytes for each axis, in the order: gyroscope, accelerometer and magnetometer. Actually UUID contain more digits but only the different part is presented in Table 2 .
|Service Name||UUID||Char Name||UUID|
Because in this case the MATLAB programs are much longer than previous ones only some parts of the achieved programs are presented. Mainly, such a program has three parts:
the first part when the sensor is still for a time while gyroscope data are gathered to compensate its offset; generally in this part 200 samples are acquired;
the second part when the sensor is rotated with 360 degrees in both senses around z axis while the magnetometer data are gathered to compute the calibration; in this part also 200 samples are acquired;
the last part has an indefinite duration when the sensor is moved while real-time data are displayed on the different figures.
Two programs are achieved, depending of the content of the last part. In this case there is a script and only a callback function. The period of data reading is 200 ms.
Mainly it computes:
All the time the last N=200 samples of these measurements are available.
A part of the script is presented in the following.
By running the previous program, the last 200 samples of some of the measurements obtained from the gyroscope and magnetometer are displayed in real-time.
Thus, the two waveforms of the top of Figure 7 are achieved using the gyroscope while the other two from the bottom part are achieved using the magnetometer. In each case the heading is computed. The gyroscope-based angle around z axis or heading is computed using the angular rate around the z axis, see the second waveform. The heading computed by the magnetometer presented in the third waveform is based on its x and y reading which are presented in the last waveform. It can be seen that the two waveforms that represent the heading have the same variation, except at the start. Thus the gyroscope-based heading starts from zero while magnetometer-based heading starts from about 60 degrees because it indicates the north.
By using the movement sensors, 3D orientation of a device can be computed [13, 14, 15, 16, 17]. This can be represented in three ways: quaternion, Direction Cosine Matrix (DCM) and Euler angles. The last representation means the rotational angles around the three axes, called pitch, roll and yaw but has a disadvantage because can reach in a singularity state. DCM does not have a singularity state but needs 3x3 elements. Thus the best representation is quaternion which represents a complex number having four components ,
Using the accelerometer allows only computing pitch and roll angles because a rotation around z axis does not change any of the three outputs. Thus the four elements of the quaternion, denoted
Eq. (9) can be very easily implemented in MATLAB and then the quaternion can be generated by the function
On the other hand, using the function
First the new elements of the script are presented.
The main MATLAB contributions of this chapter are:
using the new introduced MATLAB functions to access BLE devices and to implement a BLE sensors network
using the table type MATLAB to check if the desired sensors are among the discovered BLE devices
using the structure type MATLAB having a variable number of fields to handle the discovered number of BLE devices
retaining and updating the most recent samples of different measurements corresponding to BLE sensors and display them in real-time
using the quaternions to handle the 3D orientation of an object
using the new introduced MATLAB functions from Sensor Fusion and Tracking Toolbox to determine the parameters that describes 3D orientation
displaying the cube that imitates in real-time the moving of CC2650 Sensor Tag
as a future work, the MATLAB can be used to estimate the position of an object along with 3D orientation; in this way the tracking of an object can be completed.