This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:practical:hardware:sut:esp32:iot_9 [2024/05/02 14:03] – [IoT_9: BLE Communication with notify/indicate] ktokarz | en:iot-open:practical:hardware:sut:esp32:iot_9 [2024/05/04 12:20] (current) – ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== IoT9: BLE Communication with notify/ | ||
| + | This scenario presents how to extend the Bluetooth Low Energy server and client devices with a notification/ | ||
| + | ===== Prerequisites ===== | ||
| + | It is necessary to understand the principles of the Bluetooth Low Energy protocol with concepts of services, characteristics and descriptors. Notification and indication methods of data transmission should be known. We will use in this scenario the knowledge of the services and characteristics so making the [[en: | ||
| + | ===== Suggested Readings and Knowledge Resources ===== | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | * [[en: | ||
| + | |||
| + | |||
| + | ===== Hands-on Lab Scenario ===== | ||
| + | This scenario is intended to be implemented using two BLE laboratory nodes. One of them is a server, while the second is a client. Here we will present the extension of the scenario implemented in [[en: | ||
| + | ==== Task to be implemented ==== | ||
| + | **Task 1.** Implement a program that operates as the BLE server which advertises itself and allows us to connect to. After a successful connection, it handles the notify descriptor modification and sends the data automatically if notifications are enabled. | ||
| + | \\ | ||
| + | **Task 2.** Implement a client device, capable of connecting to the server, enabling notifications and displaying the exemplary data coming from a server as notification packets. | ||
| + | |||
| + | ==== Start ==== | ||
| + | You can use the simple client and server programs from [[en: | ||
| + | |||
| + | ==== Steps ==== | ||
| + | We will pass through the lab in a few steps. We will add the second characteristic to the example from lab [[en: | ||
| + | |||
| + | === Step 1 === | ||
| + | We start with the program written during the laboratory [[en: | ||
| + | ^ Descriptor UUID ^ Bits 1 and 0 ^ CCCD value ^ Function | ||
| + | | 0x2902 | ||
| + | | ::: | 01 | 1 | enable notification | ||
| + | | ::: | 10 | 2 | enable indication | ||
| + | < | ||
| + | #include " | ||
| + | </ | ||
| + | Next, we add another characteristic to the service and variable required to hold the pointer to the characteristic class. We need to define the UUID for this characteristic. | ||
| + | < | ||
| + | BLECharacteristic | ||
| + | #define NOTIFY_CHARACTERISTIC_UUID " | ||
| + | </ | ||
| + | We create an additional characteristic with reading, writing, notify and indicate enabled, and the initial text " | ||
| + | < | ||
| + | // Create a BLE Characteristic | ||
| + | pNotifyCharacteristic = pService-> | ||
| + | NOTIFY_CHARACTERISTIC_UUID, | ||
| + | BLECharacteristic:: | ||
| + | BLECharacteristic:: | ||
| + | BLECharacteristic:: | ||
| + | BLECharacteristic:: | ||
| + | ); | ||
| + | pNotifyCharacteristic-> | ||
| + | // Create a BLE Descriptor | ||
| + | pNotifyCharacteristic-> | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | Any characteristic can be configured as notify or indicate enabled. We can enable this mechanism for more than one characteristic. | ||
| + | </ | ||
| + | |||
| + | After these modifications, | ||
| + | |||
| + | === Step 2 === | ||
| + | At this step, we implement periodical data sending. We add two variables, an integer for holding the value incremented every loop pass and a text value converted from an integer to be sent within the indication/ | ||
| + | <code c> | ||
| + | int int_value=0; | ||
| + | char text_value[] = " | ||
| + | </ | ||
| + | In the loop() function we modify the part executed if the connection was established. We'll add conversion from integer to text with atoi(), an update of characteristic value, and an incrementation of integer value. | ||
| + | < | ||
| + | void loop(){ | ||
| + | if (!deviceConnected && !advStarted) { | ||
| + | pServer-> | ||
| + | advStarted = true; | ||
| + | } | ||
| + | |||
| + | if (deviceConnected){ | ||
| + | advStarted = false; | ||
| + | itoa(int_value, | ||
| + | pNotifyCharacteristic-> | ||
| + | pNotifyCharacteristic-> | ||
| + | int_value++; | ||
| + | } | ||
| + | |||
| + | delay(500); | ||
| + | }; | ||
| + | </ | ||
| + | |||
| + | === Step 3 === | ||
| + | In this step, we'll analyse the communication between the server and the client. The client software is much more complex than the server. Some parts of the client software are implemented as callback functions because they handle reactions on the data coming asynchronously from the server. The diagram presents the algorithm of the client and data coming from the server. | ||
| + | {{ en: | ||
| + | |||
| + | === Step 4 === | ||
| + | We have to extend the client software from scenario [[en: | ||
| + | <code c> | ||
| + | #define REMOTE_NOTIFY_CHARACTERISTIC_UUID " | ||
| + | </ | ||
| + | |||
| + | In the function for connecting to the server, we have to add another characteristic and register the callback function. It is also good to clear the LCD after establishing a connection. | ||
| + | |||
| + | <code c> | ||
| + | // Obtain a reference to the notify characteristic of the chosen service. | ||
| + | pRemoteNotifyCharacteristic = pRemoteService-> | ||
| + | |||
| + | if(pRemoteNotifyCharacteristic-> | ||
| + | pRemoteNotifyCharacteristic-> | ||
| + | | ||
| + | lcd.clear(); | ||
| + | </ | ||
| + | |||
| + | Next, we add the callback function which will be called every time the server sends a new notification packet. | ||
| + | <code c> | ||
| + | static void notifyCallback( | ||
| + | BLERemoteCharacteristic* pBLERemoteNotifyCharacteristic, | ||
| + | uint8_t* pData, | ||
| + | size_t length, | ||
| + | bool isNotify) | ||
| + | { | ||
| + | lcd.setCursor(0, | ||
| + | pData[length]=0; | ||
| + | lcd.print((char*)pData); | ||
| + | } | ||
| + | </ | ||
| + | < | ||
| + | You can leave a periodical reading of the second characteristic in the loop(), but you will observe that sometimes its value appears together with the notification in the first line of the LCD. This is because notification callback is called as the interrupt handler, and can be executed between setting the cursor and displaying the characteristic value. The only way to avoid it is to move displaying of incoming notifications to the mail loop. | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Result validation ==== | ||
| + | You should be able to establish a connection and read the non-notification characteristic data. After enabling the notification we will observe periodic incrementation of the value sent with notify data packets. | ||
| + | < | ||
| + | You can observe that sometimes devices do not connect. This can happen because if you upload the new version of the program to one of the devices the second one remains in a connected state. In such a situation you need to restart both devices. | ||
| + | </ | ||
| + | |||
| + | ===== FAQ ===== | ||
| + | |||
| + | **What is the difference between notification and indication? | ||
| + | |||
| + | **Is the notification/ | ||
| + | |||
| + | <WRAP noprint> | ||
| + | ===== Project information ===== | ||
| + | {{: | ||
| + | This Intellectual Output was implemented under the Erasmus+ KA2.\\ | ||
| + | Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, | ||
| + | Project number: 2022-1-PL01-KA220-HED-000085090. | ||
| + | |||
| + | **__Erasmus+ Disclaimer__**\\ | ||
| + | This project has been funded with support from the European Commission. \\ | ||
| + | This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein. | ||
| + | |||
| + | **__Copyright Notice__**\\ | ||
| + | This content was created by the IOT-OPEN.EU Reloaded consortium, 2022, | ||
| + | The content is Copyrighted and distributed under CC BY-NC [[https:// | ||
| + | <figure label> | ||
| + | {{: | ||
| + | </ | ||
| + | </ | ||