cancel
Showing results for 
Search instead for 
Did you mean: 

Reading I2C sensor data over bluetooth on mobile applications.

alitastan
Associate II

Hello, I have recently started coding and learning C language. I implemented VL53L1X drivers to my NUCLEO-WB15CC and I am able to get measurements over USART communication. However, I would like to get these measurement results over Bluetooth and I would like to start and/or cancel the measurements from my phone by using some characteristic values. For example, when I write 0x1, the measurement should start and when I write 0x0 the measurement should stop. I am trying to build this behavior on top of P2PServer example provided by ST in STM32Cube_FW_WB_V1.13.0. I am not really sure where should I start implementing this behavior. I have started reading AN5289 document along with the P2PServer codes and I am kind of lost. If you have any tips or recommendations for me please help me, I will be so grateful. Thanks.

Edit: Here is my main.c, I want to implement the characteristic value scenario to my if statement at line 8. Then, I want to control the start of the measurements without a button and by writing to a characteristic.

  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	  //B2 pin is connected via internal pull up resistor
	buttonisPressed = HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin);
	if(!(buttonisPressed == 1)){
		HAL_Delay(200);
		vl53l1x_get_measurement();
 
	}
 
 
  }
  /* USER CODE END 3 */

2 REPLIES 2
Remy ISSALYS
ST Employee

Hello,

I think the simplest solution is to start from BLE_p2pServer application.

In p2p_server_app.c file, you can start or stop your measurement using the LED characteristic, see the following code:

void P2PS_STM_App_Notification(P2PS_STM_App_Notification_evt_t *pNotification)
{
/* USER CODE BEGIN P2PS_STM_App_Notification_1 */
 
/* USER CODE END P2PS_STM_App_Notification_1 */
  switch(pNotification->P2P_Evt_Opcode)
  {
...
     case P2PS_STM_WRITE_EVT:
       /* USER CODE BEGIN P2PS_STM_WRITE_EVT */
       if(pNotification->DataTransfered.pPayload[0] == 0x00){ /* ALL Deviceselected - may be 
         necessary as LB Routeur informs all connection */
         if(pNotification->DataTransfered.pPayload[1] == 0x01)
         {
           /* Start your measurement here */
         }
         if(pNotification->DataTransfered.pPayload[1] == 0x00)
         {
           /* Stop your measurement here */
         }
       }
...
  }
}

With P2P Server example, a notification is sent when SW1 is pressed, of course notification should be enabled before, so in your case you can start by send your sensor value through a notification each time SW1 is pressed. In order to do this, you can look P2PS_Send_Notification function in p2p_server_app.c file and replace button status by your sensor value, maybe you have to change characteristic value size.

After these changes, you can uses ST BLE Toolbox smartphone application, connect to your device, enable the notification, send 0x01 value in order to start your measurement and then press SW1 to send the sensor value to your smartphone.

Best Regards

alitastan
Associate II

Hello,

Thanks for the fast response. I am able to start my sensor measurement by using write to characteristic function. I also created a read characteristic and I am passing the return value of my function there.

Best Regards