Full text
Bachelor’s Thesis Bachelor’s degree in Industrial Technology Engineering Bluetooth Low Energy based on the nRF52840 USB dongle REPORT Author: Jordi Ribas Sobreviela Director: Manuel Moreno Eguílaz Call: September 2019 Escola Tècnica Superior d’Enginyeria Industrial de Barcelona
Bluetooth Low Energy based on the nRF52840 USB dongle Page 3 Abstract The objective of this report is to explain the process of creating a system that receives data from a sensor, which is connected to a power source, and sends it to a computer without using wires. The technologies chosen to carry out the project have been the wireless protocol Bluetooth Low Energy and the nRF52840 USB Dongle from Nordic Semiconductor. Bluetooth Low Energy, shortened BLE, has the advantage of having very low energy consumption. For this reason, devices using BLE can run on simple batteries for very long periods of time. At the end of this project, the Dongle, that will be attached to the sensor and powered by a power bank, will send the data coming from the sensor to a Raspberry Pi using BLE. To do this, two applications have been created and installed into the Dongle. The first application allows the Dongle to collect the data coming from the sensor using an analogical to digital converter. Then, this data is sent in packets and the Raspberry Pi displays it in a screen. The second application is an improvement of the first one. It allows the Dongle to perform multitasking and do some calculations on the data. The Raspberry displays the results and writes them in text files for their analysis. The Dongle is programmed using C language and with the help of the Segger Embeded Studio software and the nRF Connect for Desktop application. The applications that the Dongle runs are a modification of one of the examples that are included in the nRF5 Software Development Kit, which can be downloaded directly from Nordic Semiconductor’s webpage. This example is a heart rate application that sends data via BLE. The programming on the Raspberry has been made using Python. To test the applications, simulated data has been used in order to control if the data that the Dongle sent was the same that the Raspberry Pi received. When the applications passed the tests with the simulated data, the Dongle was connected to a power source that created a wave whose parameters were known. Using the analogical to digital converter, the Dongle acquired data from the wave and calculated its parameters. The results that the Raspberry Pi showed were not exactly the same as the expected ones, but the error was small enough to consider them a good approximation.
Page 4 Jordi Ribas Sobreviela Summary ABSTRACT ___________________________________________________ 3 SUMMARY ___________________________________________________ 4 1. GLOSSARY ______________________________________________ 7 2. INTRODUCTION ___________________________________________ 9 2.1. Objectives of the project ................................................................................ 9 2.2. Scope of the project ....................................................................................... 9 3. BLUETOOTH LOW ENERGY _______________________________ 11 3.1. History .......................................................................................................... 11 3.2. Compatibility with Bluetooth Classic ............................................................ 12 3.3. Technical specifications ............................................................................... 13 3.4. Architecture .................................................................................................. 14 3.4.1. Physical Layer (PHY) ....................................................................................... 14 3.4.2. Link Layer ........................................................................................................ 15 3.4.3. Generic Access Profile (GAP) .......................................................................... 16 3.4.4. Generic Attribute Profile (GATT) ...................................................................... 17 4. HARDWARE SETUP ______________________________________ 20 4.1. nRF52840 USB Dongle ............................................................................... 21 4.2. nRF52840 DK .............................................................................................. 22 4.3. Raspberry Pi 2B ........................................................................................... 23 5. GENERIC PROCESS FOR CREATING A BLE APPLICATION _____ 26 5.1. nRF5 SDK .................................................................................................... 26 5.2. Segger Embedded Studio (SES) ................................................................. 28 5.3. nRF Connect for Desktop ............................................................................ 30 5.3.1. Bluetooth Low Energy application .................................................................... 30 5.3.2. Programmer application ................................................................................... 32 6. THE BLUETOOTH LOW ENERGY APPLICATIONS _____________ 35 6.1. First application: sending raw data............................................................... 36 6.1.1. Analogical to digital conversion ........................................................................ 37 6.1.2. Software development ..................................................................................... 38 6.1.3. Results ............................................................................................................. 41 6.2. Second application: parameter calculation .................................................. 43 6.2.1. Calculated parameters ..................................................................................... 43
Bluetooth Low Energy based on the nRF52840 USB dongle Page 5 6.2.2. Software development .................................................................................... 44 6.2.3. Results ............................................................................................................ 46 7. ECONOMIC STUDY _______________________________________ 52 7.1. Cost of the equipment .................................................................................. 52 7.2. Cost of the development time ....................................................................... 54 8. ENVIRONMENTAL IMPACT ________________________________ 55 8.1. Impact on people’s health ............................................................................ 55 8.2. Impact on the planet ..................................................................................... 55 CONCLUSIONS ______________________________________________ 57 ACKNOWLEDGEMENTS _______________________________________ 59 BIBLIOGRAPHY ______________________________________________ 60 Bibliographic references ........................................................................................ 60 Consulted bibliography .......................................................................................... 63
Bluetooth Low Energy based on the nRF52840 USB dongle Page 7 1. Glossary - API: Application Programming Interface - BC: Bluetooth Classic - BLE: Bluetooth Low Energy - Bluetooth SIG: Bluetooth Special Interest Group is the standards organisation that oversees the development of Bluetooth standards and the licensing of the Bluetooth technologies and trademarks to manufacturers [1]. - Bootloader: A bootloader is a type of program that loads and starts the boot time tasks and processes of an operating system. It enables loading the operating system within the device memory when a device is started or booted up [2]. - DFU: The Device Firmware Upgrade is a vendor and device independent mechanism for upgrading the firmware of USB devices with improved versions provided by their manufacturers [3]. - GPIO: General Purpose Input/Output - IDE: Integrated Development Environment - IEEE: Institute of Electrical and Electronics Engineers - IoT: Internet of Things - MBR: The Master Boot Record provides an interface to allow in-system updates of the application, the SoftDevice, and bootloader firmware [4]. - SDK: Software Development Kit - SoftDevice: A SoftDevice is a precompiled and linked binary software implementing a wireless protocol developed by Nordic Semiconductor [5]. - SWD: Serial Wire Debug
Page 8 Report - UUID: The Universally Unique Identifier is a 128-bit number that is used to identify information, in the case of this project it identifies an Attribute type. The Attributes that are adopted by the Bluetooth SIG share all but 16 bits of a special 128-bit Base UUID. On the other hand, UUIDs that are defined by the developer can be any 128-bit numbers.
Bluetooth Low Energy based on the nRF52840 USB dongle Page 9 2. Introduction The motivation for doing this project is the lack of a system to transfer the data from a sensor to a computer wireless with a low power consumption. In order to solve this problem, this project has consisted of the creation of several Bluetooth Low Energy applications that allow this data transmission. 2.1. Objectives of the project • Develop a way to collect data continuously from the sensor using the nRF52840 USB Dongle device from Nordic Semiconductor. • Be able to send the data acquired by the Dongle using BLE to a Raspberry Pi. This process has to be continually repeated at the maximum frequency possible in order to get a good approximation of the data from the sensor. • Receive the data coming from the Dongle using a Raspberry Pi and display it on a screen. This data must also be saved in text files. • Include the calculation of parameters in the information that is sent from the Dongle to the Raspberry Pi. • Implement a method for allowing the Dongle to do the data acquisition process at the same time it performs the parameters calculation and sends the information. 2.2. Scope of the project This project intends to create to a system to make the data reading of a sensor easier for its further analysis. To do this, two software applications have been created using various development tools. The functionality of these applications is specifically adapted to the device nRF52840 USB Dongle. The code that is incorporated in the Raspberry Pi works only for the two applications developed. As soon as the Dongle is powered, the developed application will start, but the connection between the Dongle and the Raspberry Pi will not happen until the Raspberry Pi accepts it. The applications will periodically send raw data or parameters that will
Page 16 Jordi Ribas Sobreviela Bluetooth Device Address The Bluetooth Device Address is an identifier for Bluetooth devices (similar to a MAC address) formed by a 48-bit (6-byte) number. There are two types of addresses: - Public Address: It is factory-programmed and must be registered with the IEEE Registration Authority. This type of address is permanent; thus, it will not change during the device lifetime. - Random Address: It can be pre-programmed on the device or generated during its lifetime. It is the most common type of address because it does not need to be registered with the IEEE. 3.4.3. Generic Access Profile (GAP) The Generic Access Profile (GAP) defines how BLE devices interact with each other. It is key to determine the way two or more devices exchange information and communicate. GAP covers the following aspects: modes and roles of BLE devices, advertisements, connection establishment and security. In this report only the aspect of the modes and roles of BLE devices will be covered because it gives enough information to understand the project. Further information about this topic can be found on the complementary bibliography section of this document. Roles - Broadcaster: The Broadcaster uses the Advertising state from the Link Layer to send advertising packets without establishing a connection with any devices. The data is available for all the devices that are listening. An example of a Broadcaster would be a public thermometer that informs about the temperature to all the interested devices. - Observer: The Observer uses the Scanning state from the Link Layer to look for advertising data coming from a Broadcaster. This role does not establish a connection with other devices and does not send data. A device with a display is a common example of this role.
Bluetooth Low Energy based on the nRF52840 USB dongle Page 17 Figure 3.5. Broadcaster/Observer network topology [11]. - Peripheral: The Peripheral sends advertising packets in order to establish a connection with another device. It uses the Connected state from the Link Layer and it is a Slave. Peripherals are optimized to consume the least amount of processing power and energy. For this reason, they are low-cost gadgets. The Dongle will use this role in the applications developed in this project. - Central: The Central is the Master of the Connected state from the Link Layer. Central devices look for advertising packets from Peripherals and initiate a connection with the ones they choose. A Central can be connected to multiple devices creating a single network. The Central role is usually performed by devices with powerful CPUs. Figure 3.6. Peripheral/Central network topology [11]. 3.4.4. Generic Attribute Profile (GATT) The Generic Attribute Profile defines the way data is organized and exchanged during a Bluetooth Low Energy connection. To fully understand GATT, it is first needed to learn about the Attribute Protocol (ATT) since they share the same roles. These two roles are the Server and the Client:
Page 18 Jordi Ribas Sobreviela - Server: The Server is responsible for containing the data that is going to be exchanged. It receives requests from the Client and answers sending responses back. The data that the Server has is organized in the form of Attributes, which have the structure represented below: - Client: The Client sends requests to the Server in order to read its data or control its behaviour. Firstly, the Client does not know about the Attributes located in the Server, for this reason it performs a service discovery before sending requests. With the service discovery the Client is able to find out about the presence and nature of the Server’s Attributes. The GATT defines a hierarchy for organizing Attributes. These Attributes are organized as GATT Server Profiles, which at the same time are formed by Services, which contain Characteristics, which contain Attributes. A Characteristic’s Attribute can be a Declaration, a Value or a Descriptor. Figure 3.8. Hierarchy for organizing Attributes [13]. Figure 3.7. Attribute structure [12].
Bluetooth Low Energy based on the nRF52840 USB dongle Page 19 The GATT Profile that has been modified in this project to adapt it to the objective of each application is the Heart Rate Service, which is a public Service defined by the Bluetooth SIG. The structure and hierarchy of this Profile will be used as an example to clarify the concepts recently explained. Figure 3.9. Structure and hierarchy of the GATT Heart Rate Service [13]. It can be observed that the Service contains eight Attribute data structures (underlined in red) and three Characteristics (circled in green). The "Heart Rate Measurement" Characteristic contains three Attributes, including a Descriptor Attribute. This Characteristic is a Serverinitiated update characteristic, which means that the value will be sent to the Client whenever the value Attribute is updated.
Page 20 Jordi Ribas Sobreviela 4. Hardware setup In this section, it is described an explanation about the hardware devices that have been used to program the applications on the Dongle and the way these devices connect and interact with each other. The most important devices in this setup are the nRF52840 USB dongle, the Raspberry Pi 2 and a computer. The Dongle is connected to a laptop via USB, which is its energy source. In the laboratory the energy source will be a battery with a USB port, but the computer is used because it allows to program the Dongle. Soldered to the castellated edges of the Dongle there will be the pins, which will make the connection between the Dongle and the sensors available in the laboratory. The Raspberry Pi is connected to a keyboard and a mouse through USB and to a display via HDMI. It may be needed to use a VGA to HDMI adapter cable to make the display connection. The power source of the Raspberry will also be the laptop and they will be connected using a micro-USB to USB cable. If it is needed to have Internet on the Raspberry, an Ethernet cable will also be required, which will be connecting the Raspberry with the laptop. Finally, to communicate the Raspberry with the Dongle via Bluetooth Low Energy, a Bluetooth 4.0 USB adapter is necessary. It is important to remember that the Raspberry needs a micro SD card to store all the data. In this project the micro SD card that has been employed has a capacity of 16 GB, but an 8 GB card should be enough to handle the developed applications or similar ones. To test the code written for the applications and be able to perform debugging there is another gadget that has been used. This device is the nRF52840 DK from Nordic Semiconductor. This board is more sophisticated than the Dongle, but it is also bigger and more expensive; that’s the reason why it will only be used to make the coding tasks easier. First the applications will be programmed on the nRF52840 DK and then, once it is checked that they work properly, they will be programmed on the Dongle. The nRF52840 DK replaces the Dongle in the setup, but its connection with the computer is made via a micro-USB to USB cable instead of connecting it directly to the computer USB port.
Bluetooth Low Energy based on the nRF52840 USB dongle Page 21 Figure 4.1. Hardware setup diagram [14] [15] [16] [17] [18] [19] [20]. 4.1. nRF52840 USB Dongle The nRF52840 USB Dongle (board number PCA10059) is a device from Nordic Semiconductor that has the advantages of being small and low-cost while being able to use most of the short-range wireless standards including Bluetooth Low Energy. It can be used for product and software development with the help of the nRF5 SDK and the application nRF Connect for Desktop, which is why it is a great tool to develop wireless applications. The Dongle features a green LED (LD1), a multicolour RGB LED (LD2), a user configurable button (SW1), and a reset button (SW2). It also has a USB-A-type connector printed on the circuit board and 15 GPIOs in addition to the ground, power and SWD connections along the castellated edges [21].
Page 22 Jordi Ribas Sobreviela Figure 4.2. nRF52840 Dongle’s buttons and LEDs drawing (front view) [21]. The Dongle can be powered using the USB interface or using an external regulated 1.8V – 3.6V source connected to the VDD OUT point. As it has been explained, in this project the power source will be the USB interface. The reset button (SW2) is used to put the Dongle in the DFU mode, which allows the user to program it. LED 2 will be pulsing red to indicate that the device has entered the DFU mode. Figure 4.3. The USB Dongle with soldered pins that has been used. Source: own. 4.2. nRF52840 DK The nRF52840 DK (board number PCA10056) will be used as a development platform for testing Bluetooth Low Energy applications. One its main benefits is that it features an onboard programming and debugging solution. This is a sophisticated board which includes a lot of features and offers a wide variety of options for developers. However, in this report only the required information for understanding the procedure of debugging the project will be explained. The device has four buttons and four LEDs and can be powered using various methods, but in this case its energy source will be the computer. The board is linked with the laptop using the USB connector (J2). When connecting the board to the computer it is important to verify that the power switch (SW8) is set to ON, the nRF power source switch (SW9) is set to VDD and the nRF only switch (SW6) is set to DEFAULT. Reset button (SW2)
Bluetooth Low Energy based on the nRF52840 USB dongle Page 23 Figure 4.4. The nRF52840 DK board that has been used. Source: own. Figure 4.5. nRF52840 DK drawing (front view) [22]. 4.3. Raspberry Pi 2B A Raspberry Pi is a single board computer which allows the user to connect peripherals such as a keyboard, a mouse or a display and it is capable of doing the tasks a normal desktop computer can do. Due to its small size and reduced price it is very used in projects where some computational power is needed to carry out simple tasks. In this project its function will be the reception and representation of information coming from the USB Dongle or the nRF52840DK. SW6 SW9 SW8 J2
Page 24 Jordi Ribas Sobreviela In order to be able to use the Raspberry Pi, the first thing that has to be done is to download Raspbian [23], which is the operating system that runs on the Raspberry Pi. This system software can be easily downloaded from the official page of the Foundation: https://www.raspberrypi.org/downloads/raspbian/ There are three different versions of Raspbian, depending on the amount of features and software that they include. The chosen version for this project has been the “Raspbian Stretch with desktop and recommended software”, which is the most sophisticated one. Nowadays, this version cannot be found on the website mentioned before because a new version of the operating system (Raspbian Buster) has been released. The next step is to format the micro SD card and then write the Raspbian image file into it. The micro SD card has been formatted using Windows 10, but for the image writing the software Win 32 Disk Imager has been needed. Once this is done, the Raspberry Pi can be started by just plugging the card into the Raspberry Pi and connecting it to a power source (in this case the laptop). With this done, the device is ready to be configured and be used. But without some peripherals it will not be possible to interact with the Raspberry Pi, so the next step is connecting the external devices. At first, the Raspberry Pi connection was intended to be made using the laptop screen, keyboard and mouse. This way less devices would be required and the working environment would be more organized. This configuration is called headless setup and there is plenty of information about it on the Internet. The headless setup consists in creating a file named “ssh”, that has no extension and placing it onto the boot partition of the micro SD card. This way SSH is enabled and this allows the remote access of the Raspberry Pi from a computer. Then, after enabling other devices to connect to the computer’s Internet on the Windows networks configuration, the Internet can be shared with the Raspberry Pi using the Ethernet cable. Once this is done, using the software Putty it will be possible to access to the command prompt of the Raspberry Pi. But, in order to be able to use the desktop and all its functionalities, it will be necessary to enable VNC (which will have to be downloaded) in the Raspberry Pi’s configuration app. The described steps were carefully followed and initially it was possible to use the Raspberry Pi with only the laptop, but when the Raspberry Pi shut down and then turned on there were problems with recognizing the Raspberry Pi’s IP address. Before spending a lot of time finding and solving the problem it was decided that the easiest option would be to use the standard
Bluetooth Low Energy based on the nRF52840 USB dongle Page 25 connection that uses an external display, keyboard and mouse. Figure 4.6. Raspberry Pi setup. Source: own.
Page 32 Jordi Ribas Sobreviela At this point there are a lot of things that can be done, but to test the connection, a new service with a characteristic was created. The characteristic had the Read and Notify properties and it was given an initial value. Then a descriptor called Client Characteristic Configuration was added to the characteristic. With the connection established, the characteristic’s value was displayed on the phone’s app and if the value was changed in the nRF Connect, the value also changed in the phone. 5.3.2. Programmer application nRF Connect Programmer can be used to program firmware to Nordic devices. The application allows the user to see the memory layout for both J-Link and Nordic USB devices. It also allows the user to display content for the HEX files and write it to devices [31]. In this project this application has been used to program the applications on the Dongle. The process of connecting the Dongle to the application is the same as in the Bluetooth Low Energy application. When the device has been selected the screen shows the device with the contents of its memory represented. In Figure 6.6. the names referring to the different memory Figure 5.5. Battery Level characteristic on the nRF Connect for Desktop (left) and value read on the LighBlue phone app (right). Source: own.
Bluetooth Low Energy based on the nRF52840 USB dongle Page 33 areas can be appreciated. The application has also a log section, where the messages about the processes that have been carried out appear, and the right section has the tools to write files into the device and also erase them. Figure 5.6. Screenshot of the Programmer application with the memory parts of the Dongle. Source: own. To flash the Dongle, two HEX files need to be loaded: - Application HEX file: This file is created when a SES Project is built correctly. The file is located in the SDK, in a folder called hex. The path to the file is: C:\ . . . \examples\ble_peripheral\ble_app_hrs\pca10059\s140\ses\Output\Release\Exe - Soft Device HEX file: This file can be found in the downloaded SDK; it is not created afterwards. Its path is: C:\ . . . \components\softdevice\s140\hex To load the files, the menu on the right of the nRF Connect screen will be used (see Figure 5.7). Clicking on the Add HEX file tab it will be able to browse the HEX files and select them. When a HEX file is selected, it will appear in the File Memory Layout. The Soft Device HEX file includes the blue region and the orange one. The blue region corresponds to the soft device and the orange region corresponds to the MBR. When the Application HEX file is also added, a green region appears, which is the application.
Page 34 Jordi Ribas Sobreviela To write the files into the Dongle the only thing that needs to be done is to click on the Write tab on the right menu. Then, the Dongle flashing process will start showing various messages in the log section. When this process finishes, the Dongle will automatically exit DFU mode, it will be disconnected from the program as it will not appear in the device list, and it will start the application that has just been written on it. Figure 5.7. Programmer application screenshot showing the tools menu on the right and the File Memory Layout with the loaded HEX files on the left. Source: own. It is important to remark that the Programmer application is not used when working with the nRF52840 DK because the application is automatically flashed into the device clicking on the option “Download name of the application” in the “Target” tab in SES. Before doing this, the Project has to be built.
Bluetooth Low Energy based on the nRF52840 USB dongle Page 35 6. The Bluetooth Low Energy applications This section consists in the explanation of which BLE applications have been developed and their use, how they have been written and which the result of their performance is when they carry out their tasks. The objective of the applications is to transmit information using BLE, from the Dongle to the Raspberry Pi. The data that is transferred is a measurement from a sensor that is connected with the Dongle. For this reason, the transmission needs to have certain characteristics because otherwise the information received in the Raspberry Pi will not be useful. The sensor gathers data from a power source that has a frequency of 50 Hz, this is the same as saying that its period is 0,02 seconds or 20 milliseconds. In order to be able to reconstruct the original signal with the data that is sent to the Raspberry Pi using BLE, it is crucial that the process of sampling respects the Nyquist–Shannon sampling theorem. This theorem states that the sampling frequency has to be at least two times the frequency of the signal that is being sampled. To maximize the precision of the signal reconstruction, the sampling frequency has been set to 1 kHz, which means that the sampling period is 1ms. The design of these applications is based on an example from the SDK called ble_app_hrs. This example consists of a heart rate sensor that sends out data including the battery level, the detection of the sensor and the heart rate measurement. The code creates simulators to obtain the data and uses timers to send it via BLE. When a timer expires, the code sends the data and the timer is restarted. This example was chosen because it has support for both the Dongle and the nRF52840 DK and its function is similar to the purpose of the applications that will be created. The process of coding and testing the applications has been possible with the help of the setup that has been described. The first task of the process is the modification of the code using SES. Then, once the SES Project is built, the application is flashed to the Dongle using the nRF Connect for Desktop. The Dongle sends the data to the Raspberry Pi and using a Python code the data can be represented on the monitor. If the sent data is correct and the connection characteristics between the Dongle and the Raspberry Pi are the expected ones, the application is ready. If this is not the case, then the code has to be modified again and repeat the whole process. To make the coding task easier, the nRF52840 DK is used to be able to debug when using SES. This board can also transmit data to the Raspberry Pi using BLE.
Page 36 Jordi Ribas Sobreviela At the end of the project, the Dongle will acquire the data from the sensor using an analogical to digital converter (ADC), but to test the applications easily it uses simulated data. This data is generated using an Excel file that emulates the signal from the power source in the laboratory. To generate the data, the Excel file takes the frequency, the amplitude and the offset of the signal. With these parameters and the step size of the time, a sine wave function is created. The step size is defined as 1 millisecond because it is the sampling period of the applications. This way the Excel file generates the value of the sine wave every 1 millisecond. Figure 6.1.Excel file used to generate simulated data. Source: own. 6.1. First application: sending raw data The purpose of this application is to obtain the data from the sensor every 1 millisecond using an analogical to digital converter (ADC), and to send BLE packets with this data to the Raspberry Pi. At first it was thought that sending a packet of 20 values every 20 milliseconds would be the best option because that is the signal period. But in practice this generated problems because it exceeded the maximum length value of 20 bytes that a BLE packet can have. For this reason, the application was programmed to send 10 values every 10 milliseconds (half of a period). 0 0,5 1 1,5 2 1 11 21 31 41 51 61 71 81 91 101 111 Voltage (V) Time (ms)
Bluetooth Low Energy based on the nRF52840 USB dongle Page 37 6.1.1. Analogical to digital conversion The first and most important modification that had to be made to the hrs example was to add an ADC to replace the simulators in the data collection. The ADC needs to define a point where the data will be collected. This point has to be one of the connection points in the castellated edges, where the pins have been soldered. To determine which pin is the ideal, the Dongle’s connections schematic has been consulted [32]. The connection points in the schematic that have the nomenclature “AIN#”, where the character “#” is a number, are the points that allow an analogical input. Among them, the ones that have a square at the end of their line in the schematic, are the ones that allow a connection. The chosen pin has been the P0.02, whose connection point is the AIN0. Figure 6.2. Part of the Dongle's schematic showing the chosen pin for the ADC connection. [32] The ADC default configuration has a resolution of 10 bits; this involves that the Dongle will be able to send 2^10 = 1024 different values. The voltage range accepted by the Dongle is 3.6 V, therefore, the voltage resolution of the ADC is 3.6/1024 = 0.00352 V. The data that the ADC collects is stored in 16 bits (2 bytes), meaning that it will only be possible to send 10 values (20 bytes) in each BLE packet.
Page 38 Jordi Ribas Sobreviela 6.1.2. Software development In this section there is the explanation of how does the original hrs example works and the modifications that have been made to it in order to accomplish the desired behaviour. Not all the details about the example will be analysed, only the important parts that allow the comprehension of this project. How the Raspberry Pi receives the data from the Dongle and represents it using a Python code is also explained here. This is the part that has required more effort due to the fact that SES uses C language to develop the applications, so a new programming language had to be learnt. The hrs example from the SDK acts as a sensor that is continually sending values of heart rate measurements to the device it is connected with. When the application is initialized it automatically starts advertising so that other BLE devices can discover the sensor. When a device decides to establish a connection with the sensor, the application starts sending heart rate measurements to the device every 1 second. The sensor continues sending BLE packets until the connection between the two devices is stopped. To do this, the application uses a heart rate timer, which is originally set to 1000 milliseconds (1 second). Every time this timer expires, a function is called and the timer is restarted. The called function creates the measurement with a simulator and then calls another function that encodes the data and sends it to the client. The measurement is a 16-bit value, but the data that is sent is an array of 8-bit values. Now that the original functioning of the application is known, let’s explain the modifications. To implement the analogical to digital conversion in the code some drivers need to be enabled and the file containing the ADC configuration code needs to be loaded to the SES project. The CMSIS Configuration Wizard can be used to enable the corresponding drivers. Writing some code, the ADC is configured to collect data from the input point AIN0 and it substitutes the simulator in the data acquisition process. Now the heart rate measurements that the application sends are actually the values that the ADC receives and then stores in a variable called adc0. As the sending channel uses 8-bit data and the values from the ADC have 16 bits, the adc0 values are split in two 8-bit values and stored in an array. When the length of the array is 20 (it takes 10 milliseconds because every time the timer expires two values are added to the array), the sending function is called. This function copies the previous array to the array that transmits
Bluetooth Low Energy based on the nRF52840 USB dongle Page 39 the data, which is called encoded_hrm and sends it via BLE to the Raspberry Pi. Figure 6.3. Diagram for schematizing the developed code. Source: own. The Raspberry Pi also needs to be programmed to establish the BLE connection with the Dongle and to receive and interpret the data. The programming language used for this task has been Python. The Python program has to respond to the advertising notifications that the BLE is sending and accept the data that is being sent. Once the data is received, the Raspberry Pi has to regroup the values that were previously separated, convert them to Volts units and print them. To make the data analysis easier, a text file is generated with all the data that is also printed in the terminal. This Python code is the result of changing an already existing code and adapting it to the application objective. The original code is called bluepy and it is available on GitHub (https://github.com/IanHarvey/bluepy). Bluepy provides an application programming interface (API) to handle BLE devices using Python. As bluepy is a generic code, some adaptions have been performed to make the code recognize the Dongle and receive the notifications from the heart rate example. To recognize the Attributes from the heart rate application, the code uses their UUIDs. These UUIDs can be found in the official Bluetooth SIG website. The next table summarizes the
Page 40 Jordi Ribas Sobreviela UUIDs that have been used and their corresponding Attribute. UUID Attribute 0x180D Heart Rate Service 0x2A37 Heart Rate Measurement Characteristic 0x2902 Client Characteristic Configuration Table 6.1. UUIDs used and their corresponding Attribute. Source: own. With the Heart Rate Service UUID the data from the application is exposed and ready to be accepted. The Heart Rate Measurement Characteristic controls the transmission of data from heart rate measurements and it has a notifying property and a Descriptor. This Descriptor is the Client Characteristic Configuration. Using these UUIDs the code is able to continuously accept the data coming from the Dongle. Now the Raspberry Pi has to display the data in a way that it is easily understandable. To do this, a function for handling notifications includes a series of orders to organize the data. Firstly, the function opens an empty text file where the data will be added. Then, it regroups the separated data contained in the received array and uses a conversion factor to transform the values to Volts units. This conversion factor consists in multiplying the value by the accepted voltage range of the Dongle (3.6V) and dividing it for 1023 (base 2 exponential to the number of bits of the ADC minus 1). After this, the function prints the converted data in the terminal and writes it in the text file. When all the data from the received array has been written to the text file, the function closes the file. The data is organized in a single column in the text file. To run the Python program in the Raspberry it is necessary to access the directory where the file containing the program is placed using the terminal. Then, the program has to be called including the Device Address of the Dongle and its type. The Dongle address can be found using various tools, but maybe the nRF Connect for Desktop application is the easiest one. When the Dongle is connected to the application the address appears under its name (see Figure 5.4). The address type is random.
Bluetooth Low Energy based on the nRF52840 USB dongle Page 41 Figure 6.4. Command line with the orders to connect the Dongle with the Raspberry Pi. Source: own. 6.1.3. Results The firsts tests to see if the Dongle was able to send data were done using the nRF Connect application to establish a connection between the phone and the Dongle. Once the phone was able to receive notifications, the tests were done using the Raspberry Pi setup. To be able to run tests without a power source, the Excel file with simulated data has been used. In this file a new column is added with the ADC values (from 0 to 1023) obtained when converting the simulated ones. In the C code from SES, the data collection from the ADC is replaced with the array of these simulated values. This involves that every time the ADC used to take a sample and store it in adc0, now adc0 takes a value from the array. With this method, if the application works properly the text file and the terminal will show the same values from the Excel file. Once the application passes the test with the simulated data, the final test with the power source can be performed. The final test has been done in the laboratory because a power source and an oscilloscope are needed. An analogical waveform with a frequency of 50 Hz and a range of Volts anywhere between 0 and 3,6 is set using the power source. The oscilloscope represents the waveform and displays the maximum and minimum value, the average value and the rms value. Figure 6.5. Oscilloscope screen showing a wave used for testing. Source: own.
Page 48 Jordi Ribas Sobreviela Figure 6.13. Waveform with bigger amplitude. Source: own. Figure 6.14. Evolution of the average value through time. Source: own. 1,26 1,28 1,3 1,32 1,34 1,36 1,38 1,4 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 Voltage (V) Period number Evolution of the average value Calculated avg Oscilloscope avg
Bluetooth Low Energy based on the nRF52840 USB dongle Page 49 Figure 6.15. Evolution of the rms through time. Source: own. Figure 6.14 and Figure 6.15 show the results from representing the data obtained when the waveform from Figure 6.13 is connected to the Dongle. Both graphics represent a good approximation of the parameters as the error in the average and the rms value is in the order of 20 mV (millivolts). Figure 6.16. Waveform with a different offset. Source: own. 1,52 1,54 1,56 1,58 1,6 1,62 1,64 1,66 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 Voltage (V) Period number Evolution of the rms value Calculated rms Oscilloscope rms
Page 50 Jordi Ribas Sobreviela Figure 6.17. Evolution of the average value through time. Source: own. Figure 6.18. Evolution of the rms value through time. Source: own. 2,68 2,7 2,72 2,74 2,76 2,78 2,8 2,82 1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 Voltage (V) Period number Evolution of the average value Calculated avg Oscilloscope avg 2,7 2,72 2,74 2,76 2,78 2,8 2,82 1 6 11 16 21 26 31 36 41 46 51 56 61 Voltage (V) Period number Evolution of the rms value Calculated rms Oscilloscope rms
Bluetooth Low Energy based on the nRF52840 USB dongle Page 51 Figure 6.17 and Figure 6.18 show the results obtained when the waveform from Figure 6.16 is generated using the power source. As it can be seen, the results are very similar to the previous ones. Analysing all the figures, it can be observed that the value given by the oscilloscope is usually a few millivolts greater than the calculated value, but in the case of Figure 6.14 it is smaller. Despite this, it is risky to claim that the calculated value is always inferior than the one given by the oscilloscope. An error this small can be caused by the existent noise in the laboratory or the little variations of the waveform values.
Page 52 Jordi Ribas Sobreviela 7. Economic study In this section there is the analysis of the budget needed to carry out this project. This budget includes the cost of the materials, tools, software and hardware devices that have been used, as well as the cost of the working hours that have been necessary to finish the project. 7.1. Cost of the equipment The equipment required to do this project includes: the wires used to connect the devices, the software licenses, the documentation used to write this report, all the devices that have been bought during the passing of the project and the amortization of the instruments that have been used. The software tools that have been used did not represent any cost, as they were all license free and could be easily downloaded from their brand’s official website. The book that has been used to understand all the BLE basic theory and the first steps of the functioning of the nRF5 Nordic devices, is available on the author’s website (https://www.novelbits.io/bluetooth-5-developers-e-book/#ebook-packages) for a price of 99$. This value has been converted to euros for this analysis. All the hardware devices needed for this project can be ordered online, so it is easy to estimate their price. Both the BLE book and the hardware devices used in this project did not represent an expense for the author because all the material was lent by the supervisor of the project. During the course of the project two nRF52840 DK boards were used because one did not work properly and had to be replaced. The following tables summarize all the equipment cost for this project and the cost of amortization due to their usage. To calculate the cost of amortization of the equipment, the next information is needed: lifetime of the equipment, purchase value and amount of the equipment’s working hours during this project. Dividing the purchase value by the lifetime, the depreciation fee is obtained. Then the fee is multiplied for the hours of functioning and the cost of amortization is obtained. The amortization cost of the oscilloscope and the power source hasn’t been calculated because their usage time has been very small. The only objects that have been considered for the amortization cost are the laptop and the Raspberry Pi because they are the devices that have been working for more hours. Despite this, observing Table 7.2, it is obvious that this cost is insignificant compared with the acquisition price of the equipment.
Bluetooth Low Energy based on the nRF52840 USB dongle Page 53 Equipment Unit price (€) Quantity Total price (€) BLE theory book 89,20 1 89,20 Raspberry Pi 2B 42,37 1 42,37 nRF52840 USB Dongle 9,40 1 9,40 nRF52840 DK 43,29 2 86,58 Bluetooth 4.0 USB adapter 15,99 1 15,99 USB – microUSB wire 1,70 2 3,40 VGA – HDMI adapter 8,99 1 8,99 Ethernet wire 2,95 1 2,95 16 GB microSD card 6,99 1 6,99 TOTAL 265,87 Table 7.1. Cost of the equipment used in this project. Source: own. Equipment Purchase value (€) Estimated lifetime (years) Estimated working time (days) Amortization cost (€) Laptop 800 7 10 3,13 Raspberry Pi 2B 42,37 7 5 0,08 TOTAL 3,21 Table 7.2. Cost of amortization of the equipment used in this project. Source: own.
Page 54 Jordi Ribas Sobreviela 7.2. Cost of the development time The cost of the time that has been spent working on this project is showed in this section. The working has been divided in three sections: - Research and previous study: contains the hours spent learning the basics of C programming, learning what is BLE and how it works, which are the characteristics and capabilities of the Dongle and how do the different software function. - Software developing: time spent programming the two applications on the Dongle or the nRF52840 DK using SES and the examples from the SDK. The programming tasks that have been done using Python to allow the Raspberry Pi to receive and display data are also considered here. - Testing and writing the report: contains the time dedicated to test the application, whether it is with the power source or the simulated data. This section also contains the hours committed to writing this report. Task Time spent (hours) Cost (€/hour) Total cost (€) Research and previous study 50 40 2000 Software developing 200 40 8000 Testing and writing the report 50 40 2000 TOTAL 12000 Table 7.3. Summary of the cost of the working hours. Source: own.
Bluetooth Low Energy based on the nRF52840 USB dongle Page 55 8. Environmental impact The environmental impact of this project is small because most of it consists in software development. Therefore, the only part of the project that will make an impact on the environment will be the hardware devices. It is also important to consider that these devices communicate with each other using radio frequencies and they contain electrical and electronic components that generate electromagnetic waves. 8.1. Impact on people’s health The radiofrequency of 2.4 GHz, which is the one BLE and other wireless protocols use such as Wi-Fi use, has been subject of numerous studies because is present in most of people’s everyday life. These studies have concluded that a prolonged exposure to these electromagnetic waves do not have any effect to people’s health, not even to children. The hardware setup in the laboratory also should not represent any danger for the people working there. The equipment uses very low voltages, so there is not any risk of electrocution. 8.2. Impact on the planet The part of the project that creates a bigger impact on the planet comes when the lifetime of the devices ends. When this happens, they need to be properly recycled because these devices contain a wide variety of materials and some of them are toxic. For this reason, the recycling process of this kind of products is complicated. During their use, the devices consume a certain amount of energy. Even though this energy consumption is low because that is just one of the BLE technology goals, it has to be considered. When the Dongle is powered using the USB interface, it only consumes 5 V and some of its functions can work with only 3 V. As for the Raspberry Pi, it uses an average of 295 mA when working. Its maximum consumption is 400 mA, which means 2.1 W approximately; this value is reached when it uses the 400% of its CPU, so probably it will not be reached when working with the Dongle.
Bluetooth Low Energy based on the nRF52840 USB dongle Page 57 Conclusions The goal of the project was to develop BLE applications with the USB Dongle that could allow the wireless transmission of the data coming from a sensor. It has been proved using different tests that the applications here presented can create a BLE connection with another device and send data. The first application enables the data collection using an ADC and then successfully transmits this data in BLE packets. The tests have shown that the same data that has been collected by the Dongle is then received and displayed using the Raspberry Pi. The second application is an improvement of the first one. It makes possible for the Dongle to continue with the data acquisition while performing other tasks. It also incorporates the calculation of four parameters from the signal that is being sampled. Using the data from the text files that are generated when the parameters are sent via BLE, it is possible to study the time evolution of these parameters. To enhance the performance of the Dongle, a good idea would be to create another application to put the Dongle in a sleep mode. This way, if data needed to be collected, the Dongle would run the application, but otherwise it would remain in sleep mode. With this application the energy consumption would be reduced even more. To conclude, this project has proved that the nRF52850 USB Dongle from Nordic Semiconductor is a great option for running simple BLE applications due its low cost. The applications that have been created in this project using the Dongle have achieved the desired results. This project has also demonstrated that Bluetooth Low Energy is a technology with a lot of possibilities in the actual era. Its characteristics make it an ideal solution to establish simple wireless communications between devices and it allows the creation of applications that bring new functionalities to the devices, as in the case of this project.