No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:iotn:esp32ble4
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:esp32ble4 [2024/04/01 19:32] – ktokarz | en:edu:zmitac:s2:iotn:esp32ble4 [2024/04/01 20:08] (current) – [FAQ] ktokarz | ||
|---|---|---|---|
| Line 109: | Line 109: | ||
| pBLEScan-> | pBLEScan-> | ||
| | | ||
| - | // Start the scan for 5 seconds | + | // Start the scan for 30 seconds |
| - | pBLEScan-> | + | pBLEScan-> |
| } | } | ||
| </ | </ | ||
| + | < | ||
| + | In the original example, the scan time is 5 seconds only. It can be too short to connect to a remote device, especially if there are other devices in the neighbourhood. | ||
| + | </ | ||
| In the loop function, we wait for the information that the server with the service UUID we were interested in was found. It is signalled with the use of " | In the loop function, we wait for the information that the server with the service UUID we were interested in was found. It is signalled with the use of " | ||
| < | < | ||
| Line 130: | Line 132: | ||
| </ | </ | ||
| - | < | + | The connection to the server is executed in some steps: |
| - | Any characteristic | + | - Creation of the client object. |
| - | </ | + | - Setting the callback class for handling disconnection. |
| + | - Connection to the remote device. | ||
| + | - Setting parameters of the connection. | ||
| + | - Getting the reference to the service. | ||
| + | - Getting the reference to the characteristic. | ||
| + | - Registering notification | ||
| + | - Informing the main program with the " | ||
| - | After these modifications, | + | < |
| + | bool connectToServer() { | ||
| + | BLEClient* | ||
| + | pClient-> | ||
| + | // Connect to the remote BLE Server. | ||
| + | pClient-> | ||
| + | | ||
| + | pClient-> | ||
| + | | ||
| + | // Obtain a reference to the service we are after in the remote BLE server. | ||
| + | BLERemoteService* pRemoteService = pClient-> | ||
| + | | ||
| + | // Obtain a reference to the characteristic in the service of the remote BLE server. | ||
| + | pRemoteCharacteristic = pRemoteService-> | ||
| + | | ||
| + | if(pRemoteCharacteristic-> | ||
| + | pRemoteCharacteristic-> | ||
| - | 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/ | + | connected |
| - | <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){ | + | < |
| - | | + | Because this example is quite complex, it's good to have messages sent by the program. The original example contains many messages printed at almost every step of the program' |
| - | itoa(int_value,text_value, | + | </note> |
| - | | + | |
| - | pNotifyCharacteristic-> | + | |
| - | int_value++; | + | |
| - | } | + | |
| - | delay(500); | ||
| - | }; | ||
| - | </ | ||
| ==== Result validation ==== | ==== Result validation ==== | ||
| - | You should be able to find the device in the BLE neighbourhood. It should allow us to establish a connection, read the characteristic data and write a new value. After enabling | + | You should be able to find the server |
| ===== FAQ ===== | ===== FAQ ===== | ||
| - | **What is the difference between notification and indication?**: As it was mentioned in the beginning the notification is an unacknowledged message while an indication is an acknowledged message. It means that if the indication message remains unacknowledged, | + | **Can we connect to the same peripheral with more than one central device?**: No, it's not possible. The BLE standard allows only one connection for the peripheral device. It was defined to ensure that BLE will work on constrained devices with small memory. |
| - | **Is the notification/ | + | **May I use the data in another way than printing it on the console?**: Of course. |
| <WRAP noprint> | <WRAP noprint> | ||
en/edu/zmitac/s2/iotn/esp32ble4.1711992720.txt.gz · Last modified: by ktokarz
