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:stm32:iot_7 [2024/05/04 10:49] – ktokarz | en:iot-open:practical:hardware:sut:stm32:iot_7 [2024/05/04 11:03] (current) – [STM_IoT_7: BLE Communication with notifications] ktokarz | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== STM_IoT_7: BLE Communication with notifications | + | ====== STM_IoT_7: BLE Communication with indications |
This scenario presents how to extend the Bluetooth Low Energy server and client devices with a notification or indication mechanism for sending data automatically. If enabled, notifications or indications are sent at any time while the data in the server is updated. A difference between them is that a notification is an unacknowledged message while an indication is an acknowledged message. While one of them is enabled by the client, the server decides on the time of the message sent. | This scenario presents how to extend the Bluetooth Low Energy server and client devices with a notification or indication mechanism for sending data automatically. If enabled, notifications or indications are sent at any time while the data in the server is updated. A difference between them is that a notification is an unacknowledged message while an indication is an acknowledged message. While one of them is enabled by the client, the server decides on the time of the message sent. | ||
Line 17: | Line 17: | ||
==== Task to be implemented ==== | ==== Task to be implemented ==== | ||
- | **Task 1.** Implement a program that operates as the BLE server which advertises itself and allows us to connect to. We should be able to subscribe to notifications | + | **Task 1.** Implement a program that operates as the BLE server which advertises itself and allows us to connect to. We should be able to subscribe to indications |
\\ | \\ | ||
- | **Task 2.** Implement a client device, capable of subscribing to the characteristic and reading the exemplary data from a server with a notification | + | **Task 2.** Implement a client device, capable of subscribing to the characteristic and reading the exemplary data from a server with a indication |
==== Start ==== | ==== Start ==== | ||
Line 25: | Line 25: | ||
==== Steps ==== | ==== Steps ==== | ||
- | We will pass through the lab in a few steps. We will add the second characteristic to the example from lab [[en: | + | We will pass through the lab in a few steps. We will add the second characteristic to the example from lab [[en: |
=== Step 1 === | === Step 1 === | ||
We start with the program written during the laboratory [[en: | We start with the program written during the laboratory [[en: | ||
<code c> | <code c> | ||
- | # | + | # |
// BLE Characteristic - custom 128-bit UUID, read and notify enabled | // BLE Characteristic - custom 128-bit UUID, read and notify enabled | ||
- | BLEByteCharacteristic | + | BLEByteCharacteristic |
</ | </ | ||
Line 38: | Line 39: | ||
< | < | ||
// add the characteristic to the service | // add the characteristic to the service | ||
- | pService.addCharacteristic(pNotifyCharacteristic); | + | pService.addCharacteristic(pIndicateCharacteristic); |
// set the initial value for the characteristic: | // set the initial value for the characteristic: | ||
- | pNotifyCharacteristic.setValue(1); | + | pIndicateCharacteristic.setValue(1); |
</ | </ | ||
Line 47: | Line 48: | ||
=== Step 2 === | === Step 2 === | ||
- | At this step, we implement periodical data sending. We add the counter variable, an integer for holding the value incremented every loop pass. | + | At this step, we implement periodical data sending. We add the counter variable, an integer for holding the value incremented every loop pass. We will increment this counter with use of millis() function to do not block the loop() with delay(); |
<code c> | <code c> | ||
int counter = 1; | int counter = 1; | ||
+ | int delay_millis; | ||
</ | </ | ||
In the main loop() we implement the incrementation of the counter and updating of the characteristic. | In the main loop() we implement the incrementation of the counter and updating of the characteristic. | ||
<code c> | <code c> | ||
- | pNotifyCharacteristic.setValue(counter); | + | void loop() { |
- | counter++; | + | // listen for BLE peripherals to connect: |
- | delay(1000); | + | BLE.central(); |
+ | if (millis() > delay_millis +1000){ | ||
+ | delay_millis = millis(); | ||
+ | pIndicateCharacteristic.setValue(counter); | ||
+ | counter++; | ||
+ | } | ||
+ | } | ||
</ | </ | ||