Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:practical:hardware:sut:stm32:iot_7 [2024/05/04 10:49] ktokarzen: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 of the characteristic.+**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 of the characteristic.
 \\ \\
-**Task 2.** Implement a client device, capable of subscribing to the characteristic and reading the exemplary data from a server with a notification mechanism.+**Task 2.** Implement a client device, capable of subscribing to the characteristic and reading the exemplary data from a server with a indication mechanism.
  
 ==== 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:iot-open:practical:hardware:sut:stm32:IoT_6]]. We will configure this characteristic as notify/indicate capable. It allows us to establish a connection and, if successfully connected, enable the indication or notification feature. With this feature enabled, the peripheral device initiates data transmission. It can be used for periodic measurement reading or updating information on value change.+We will pass through the lab in a few steps. We will add the second characteristic to the example from lab [[en:iot-open:practical:hardware:sut:stm32:IoT_6]]. We will configure this characteristic as capable of transmitting data with indication. It allows us to establish a connection and, if successfully connected, enable the indication feature. With this feature enabled, the peripheral device initiates data transmission. It can be used for periodic measurement reading or updating information on value change.
  
 === Step 1 === === Step 1 ===
 We start with the program written during the laboratory [[en:iot-open:practical:hardware:sut:stm32:IoT_6]] by adding 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. We start with the program written during the laboratory [[en:iot-open:practical:hardware:sut:stm32:IoT_6]] by adding 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.
 <code c> <code c>
-#define NOTIFY_CHARACTERISTIC_UUID "6e9b7b28-ca96-4774-b056-8ec5b759fd86"+#define INDICATE_CHARACTERISTIC_UUID "6e9b7b28-ca96-4774-b056-8ec5b759fd86" 
 // BLE Characteristic - custom 128-bit UUID, read and notify enabled // BLE Characteristic - custom 128-bit UUID, read and notify enabled
-BLEByteCharacteristic pNotifyCharacteristic(NOTIFY_CHARACTERISTIC_UUID, BLERead | BLEWrite | BLENotify);+BLEByteCharacteristic pIndicateCharacteristic(INDICATE_CHARACTERISTIC_UUID, BLERead | BLEWrite | BLEIndicate);
 </code> </code>
  
Line 38: Line 39:
 <code> <code>
 // 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);
 </code> </code>
  
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;
 </code> </code>
  
 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++; 
 +  } 
 +}
 </code> </code>
  
en/iot-open/practical/hardware/sut/stm32/iot_7.1714819748.txt.gz · Last modified: 2024/05/04 10:49 by ktokarz
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0