No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:iotn:ble2
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:edu:zmitac:s2:iotn:ble2 [2023/12/17 20:19] – ktokarz | en:edu:zmitac:s2:iotn:ble2 [2023/12/17 20:57] (current) – [Tasks to do during the laboratory] ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Bluetooth Low Energy for STM32 - peripheral read ====== | ====== Bluetooth Low Energy for STM32 - peripheral read ====== | ||
| In the previous example, a simple peripheral device program has been shown. The program allowed the central device to write data to the chosen characteristic and control the LEDs. In this part, we will read the characteristic data. The example available for STM32WB55 in Arduino in addition to controlling LED, allows also to read the state of the button.\\ | In the previous example, a simple peripheral device program has been shown. The program allowed the central device to write data to the chosen characteristic and control the LEDs. In this part, we will read the characteristic data. The example available for STM32WB55 in Arduino in addition to controlling LED, allows also to read the state of the button.\\ | ||
| - | The simplest method to get the information from a connected peripheral device is to read the characteristic. BLE specification defines also the notification and indication mechanisms to enable automatic updating of the information in which the central device is interested. | + | The simplest method to get the information from a connected peripheral device is to read the characteristic. BLE specification defines also the notification and indication mechanisms to enable automatic updating of the information in which the central device is interested. |
| * Notification is controlled with bit 0 (0-disable, 1-enable). | * Notification is controlled with bit 0 (0-disable, 1-enable). | ||
| * Indication is controlled with bit 1 (0-disable, 1-enable). | * Indication is controlled with bit 1 (0-disable, 1-enable). | ||
| + | ===== ButtonLED example - reading the characteristic data ===== | ||
| + | If the characteristic enables data read, the central device can retrieve information from the peripheral. in the ButtonLED example the " | ||
| + | < | ||
| + | // create button characteristic and allow remote device to get notifications | ||
| + | BLEByteCharacteristic buttonCharacteristic(" | ||
| + | </ | ||
| + | Let's consider reading first. The program does not differ much from the " | ||
| + | < | ||
| + | pinMode(buttonPin, | ||
| + | </ | ||
| + | The program uses two characteristics, | ||
| + | < | ||
| + | // add the characteristics to the service | ||
| + | ledService.addCharacteristic(ledCharacteristic); | ||
| + | ledService.addCharacteristic(buttonCharacteristic); | ||
| + | |||
| + | // add the service | ||
| + | BLE.addService(ledService); | ||
| + | </ | ||
| + | In the main " | ||
| + | < | ||
| + | // read the current button pin state | ||
| + | char buttonValue = digitalRead(buttonPin); | ||
| + | |||
| + | // has the value changed since the last read | ||
| + | bool buttonChanged = (buttonCharacteristic.value() != buttonValue); | ||
| + | |||
| + | if (buttonChanged) { | ||
| + | // button state changed, update characteristics | ||
| + | ledCharacteristic.writeValue(buttonValue); | ||
| + | buttonCharacteristic.writeValue(buttonValue); | ||
| + | } | ||
| + | </ | ||
| + | While the notification is enabled by the central device, the updated value will be sent to the central automatically. The same occurs if the central device enables indication. This can be done if the characteristic property is properly set. | ||
| + | < | ||
| + | BLEByteCharacteristic buttonCharacteristic(" | ||
| + | </ | ||
| + | ===== Tasks to do during the laboratory ===== | ||
| + | - Import LEDbutton example, compile and upload to Nucleo board. | ||
| + | - Observe the behaviour of the notification mechanism. | ||
| + | - Modify the button characteristic' | ||
| + | - Add more characteristics, | ||
en/edu/zmitac/s2/iotn/ble2.1702840744.txt.gz · Last modified: by ktokarz
