User Tools

Site Tools


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.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:edu:zmitac:s2:iotn:esp32ble4 [2024/04/01 19:32] – ktokarzen:edu:zmitac:s2:iotn:esp32ble4 [2024/04/01 20:08] (current) – [FAQ] ktokarz
Line 109: Line 109:
   pBLEScan->setActiveScan(true);   pBLEScan->setActiveScan(true);
      
-  // Start the scan for 5 seconds +  // Start the scan for 30 seconds 
-  pBLEScan->start(5, false);+  pBLEScan->start(30, false);
 } }
 </code> </code>
 +<note> 
 +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. 
 +</note>
 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 "doConnect" flag. If it's true we can connect to this server. Once we are connected the "connected" flag is set to be true in the connectToServer() function. We will show this function in a while. 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 "doConnect" flag. If it's true we can connect to this server. Once we are connected the "connected" flag is set to be true in the connectToServer() function. We will show this function in a while.
 <code> <code>
Line 130: Line 132:
 </code> </code>
  
-<note> +The connection to the server is executed in some steps: 
-Any characteristic can be configured as notify or indicate enabled. We can enable this mechanism for more than one characteristic. +  - Creation of the client object. 
-</note>+  - 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 for the characteristic and setting the notify callback function. 
 +  - Informing the main program with the "connected" flag.
  
-After these modifications, it would be possible to observe an additional characteristic in the service with the possibility of reading, writing, and enabling notification and indication.+<code> 
 +bool connectToServer() { 
 +    BLEClient*  pClient  = BLEDevice::createClient(); 
 +    pClient->setClientCallbacks(new MyClientCallback());
  
 +    // Connect to the remote BLE Server.
 +    pClient->connect(myDevice);  
 +    
 +    pClient->setMTU(517); 
 +  
 +    // Obtain a reference to the service we are after in the remote BLE server.
 +    BLERemoteService* pRemoteService = pClient->getService(BLEUUID(SERVICE_UUID));
 +    
 +    // Obtain a reference to the characteristic in the service of the remote BLE server.
 +    pRemoteCharacteristic = pRemoteService->getCharacteristic(BLEUUID(REMOTE_CHARACTERISTIC_UUID));
 +    
 +    if(pRemoteCharacteristic->canNotify())
 +      pRemoteCharacteristic->registerForNotify(notifyCallback);
  
-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/notification packet. +    connected = true; 
-<code c> +    return true; 
-int int_value=0; +}
-char text_value[] = "000000";+
 </code> </code>
-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. 
-<code> 
-void loop(){ 
-    if (!deviceConnected && !advStarted) { 
-        pServer->startAdvertising(); // restart advertising 
-        advStarted = true; 
-    } 
  
-    if (deviceConnected){ +<note> 
-        advStarted = false; +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's operation. 
-        itoa(int_value,text_value,10); +</note>
-        pNotifyCharacteristic->setValue(text_value); +
-        pNotifyCharacteristic->notify(); +
-        int_value++; +
-    }+
  
-    delay(500); 
-}; 
-</code> 
 ==== 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 the notification we will observe periodic incrementation of the value sent with data packets. +You should be able to find the server device in the BLE neighbourhood, connect to it, subscribe to the notification and receive the incoming data. We should be able to observe periodic increments of the value received with data packets from 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, the peripheral device won't send another message for this characteristic.+**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/indication mechanism similar to interrupts?**: You are right. They work in a similar manner as interrupts in the microprocessor. They allow to sending of the data asynchronously, and the peripheral decides about the time of the message sent like the peripheral decides on the time of signalling interrupt. Data transmission when the central device sends the packet with a characteristic read command is similar to the polling method used in microprocessors.+**May I use the data in another way than printing it on the console?**: Of course. You can for example change the LED colour on the central device depending on the temperature readings from the peripheral device.
  
 <WRAP noprint> <WRAP noprint>
en/edu/zmitac/s2/iotn/esp32ble4.1711992720.txt.gz · Last modified: by ktokarz