No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:iotn:ble1
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:ble1 [2023/12/16 13:56] – [Description of the most important elements of the code] ktokarz | en:edu:zmitac:s2:iotn:ble1 [2023/12/17 19:57] (current) – [Bluetooth Low Energy for STM32 - peripheral] ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Bluetooth Low Energy for STM32 - peripheral ====== | + | ====== Bluetooth Low Energy for STM32 - peripheral |
| This laboratory shows the details of the example provided as part of the STM32duinoBLE library by Arduino. | This laboratory shows the details of the example provided as part of the STM32duinoBLE library by Arduino. | ||
| ===== Installing the library and importing the example ===== | ===== Installing the library and importing the example ===== | ||
| Line 14: | Line 14: | ||
| </ | </ | ||
| ===== Description of the most important elements of the code ===== | ===== Description of the most important elements of the code ===== | ||
| + | ==== Variables and objects definition ==== | ||
| To use the BLE device you need to create the object of the BLELocalDevice class. This object can work using the SPI-connected Bluetooth module or the built-in unit in SoC. In the case of STM32WB55, the internal unit is used, so the HCISharedMemTransportClass is chosen as the connection to the BLE module. | To use the BLE device you need to create the object of the BLELocalDevice class. This object can work using the SPI-connected Bluetooth module or the built-in unit in SoC. In the case of STM32WB55, the internal unit is used, so the HCISharedMemTransportClass is chosen as the connection to the BLE module. | ||
| < | < | ||
| Line 28: | Line 29: | ||
| BLEByteCharacteristic switchCharacteristic(" | BLEByteCharacteristic switchCharacteristic(" | ||
| </ | </ | ||
| - | The number of characteristics in one service is limited to 512 by BLE specification. You can experiment on how many characteristics can be used in Arduino implementation.\\ | + | The characteristic defined in the example allows remote devices to read or write byte values. It is also possible to define characteristics which contain the text string. |
| + | < | ||
| + | BLECharacteristic textCharacteristic(" | ||
| + | </ | ||
| + | In the BLE specification, | ||
| + | The example program allows the remote device to control the state of the LED, so there is also a declaration of the " | ||
| + | < | ||
| + | const int ledPin = LED_BUILTIN; | ||
| + | </ | ||
| + | ==== Initialization ==== | ||
| The BLE subsystem must be initialized before use. It is possible to check if initialisation is successful. This can be done once in the Arduino " | The BLE subsystem must be initialized before use. It is possible to check if initialisation is successful. This can be done once in the Arduino " | ||
| < | < | ||
| Line 63: | Line 73: | ||
| BLE.advertise(); | BLE.advertise(); | ||
| </ | </ | ||
| + | Also LED output is initialized in the setup() function. | ||
| + | < | ||
| + | // set LED pin to output mode | ||
| + | pinMode(ledPin, | ||
| + | </ | ||
| + | ==== Loop function ==== | ||
| + | As in all Arduino programs, after the initialization, | ||
| + | < | ||
| + | BLEDevice central = BLE.central(); | ||
| + | </ | ||
| + | < | ||
| + | To check if there is an active connection the state of " | ||
| + | |||
| + | < | ||
| + | // if a central is connected to peripheral: | ||
| + | if (central) { | ||
| + | Serial.print(" | ||
| + | // print the central' | ||
| + | Serial.println(central.address()); | ||
| + | |||
| + | // while the central is still connected to peripheral: | ||
| + | while (central.connected()) { | ||
| + | // if the remote device wrote to the characteristic, | ||
| + | // use the value to control the LED: | ||
| + | if (switchCharacteristic.written()) { | ||
| + | if (switchCharacteristic.value()) { // any value other than 0 | ||
| + | Serial.println(" | ||
| + | digitalWrite(ledPin, | ||
| + | } else { // a 0 value | ||
| + | Serial.println(F(" | ||
| + | digitalWrite(ledPin, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // when the central disconnects, | ||
| + | Serial.print(F(" | ||
| + | Serial.println(central.address()); | ||
| + | } | ||
| + | </ | ||
| + | < | ||
| + | ===== Tasks to do during the laboratory ===== | ||
| + | - Import the Arduino blink example. | ||
| + | - Add STM32duinoBLE library to the project. | ||
| + | - Replace the original blink code with the LED example, compile and execute. | ||
| + | - Add characteristics to existing service for controlling other LEDs on the Nucleo board. | ||
| + | - Add service and the characteristic to display texts (using serial port). | ||
| + | Reading text value can be done with memcpy() function. | ||
| + | < | ||
| + | char buffer[10]; | ||
| + | memcpy(buffer, | ||
| + | </ | ||
| + | < | ||
en/edu/zmitac/s2/iotn/ble1.1702731367.txt.gz · Last modified: by ktokarz
