How to compare the received data over able to the specific value...

Dear team,

1. I am using the Ble_app_uart example from the SDK.

2. I had received the data successfully from the nrf_connect app.

3. Once the data was received over a ble after that I want to compare that data with string & want to perform an action accordingly.

4. if there is any example in sdk then please let me know with the above criteria.

5. please help me above senario.

Parents
  • Hello,

    The standard behavior of the example is just to forward any data received over the NUS service to the UART peripheral. This happens as part of the BLE_NUS_EVT_RX_DATA event handling in the nus_data_handler function. If you wish to modify the example to do something else with this data, or check it for some special sequence, you could implement this as part of the BLE_NUS_EVT_RX_DATA event as well / instead of the existing functionality.

    Best regards,
    Karl

  • Hi Karl,

    I just want to store the received data into the variable which is coming from the nrf_connect app

  • Hi Karl,

    Thanks for the help.

    every single time I am sending the new data from NRF connect app.

    So Could u please tell me

    And I want to wait till the new data had arrived

  • Hi Karl,

    Thanks for the help.

    every single time I am sending the new data from NRF connect app.

    So Could u please tell me

    And I want to wait till the new data not had arrived.

    and once that data had arrived I want to out of that loop.

  • How to wait till the new data had come from NRF connect app.

  • static void nus_data_handler(ble_nus_evt_t * p_evt)
    {
        int attempt = 0;
        char arr[6];
        char arr_1[6];
        
        bool flag = 0;
        
        if (p_evt->type == BLE_NUS_EVT_RX_DATA)
        {
            uint32_t err_code;
    
            NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
            NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
    
            for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
            {
                do
                {
                    err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
    
                    if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
                    {
                        NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
                        APP_ERROR_CHECK(err_code);
                    }
                }while (err_code == NRF_ERROR_BUSY);
            }
            if(p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
            {
                while (app_uart_put('\n') == NRF_ERROR_BUSY);
            }
    
            //storing the data into array which is comming from the ble_master(nrf_connect app)
            for(int i=0; i<=5; i++)
            {
              arr[i] = p_evt->params.rx_data.p_data[i];
            }
    
            if(strcmp(arr, "123456") == 0)
            {
              printf("\nPassword is correct");
            }
    
            if(strcmp(arr, "123456") != 0)
            {
              printf("\nPassword is incorrect!!! Please try again");
              printf("\nPassword : "); // after this I want to wait till the new data had arrived.
    
              //strcpy(arr_1, arr);
    
              if(strcmp(arr, "123456") == 0)
              {
                printf("\nPassword is correct");            
              }
    
            }    
        }
    }

  • hi Karl,

    I want to implement the logic as below in the normal c code in nus data handler.

    scanf provides the wait state till the user puts the data.

    in the same way, I want to put the wait state till the nrf_connect app sends the data to slave (nus_data_handler).

    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
    	char password[6];
    	
    	// 1st attempt
    	printf("\nPlease enter the password : ");
    	scanf("%s",&password); // wait state 
    	
    	
    	if(strcmp(password, "123456") == 0)
    	{
    		printf("\nPassword is correct");
    	}
    	
    	if(strcmp(password, "123456") != 0)
    	{	// 2nd attempt
    		printf("\nPassword is not correct!!! please try again");
    		printf("\nPlease enter the password : ");
    		scanf("%s",&password); // wait state 
    		
    		
    		if(strcmp(password, "123456") == 0)
    		{
    			printf("\nPassword is correct");
    		}
    		
    		if(strcmp(password, "123456") != 0)
    		{
    			// 3rd attempt
    			printf("\nPassword is not correct!!! please try again");
    			printf("\nPlease enter the password : ");
    			scanf("%s",&password); // wait state
    			
    			if(strcmp(password, "123456") == 0)
    			{
    				printf("\nPassword is correct");
    			}
    			
    			if(strcmp(password, "123456") != 0)
    			{
    				printf("\nYour all attempt had finished!! Please reset device");
    			}
    			
    		}
    	}
    	return 0;
    }

Reply
  • hi Karl,

    I want to implement the logic as below in the normal c code in nus data handler.

    scanf provides the wait state till the user puts the data.

    in the same way, I want to put the wait state till the nrf_connect app sends the data to slave (nus_data_handler).

    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
    	char password[6];
    	
    	// 1st attempt
    	printf("\nPlease enter the password : ");
    	scanf("%s",&password); // wait state 
    	
    	
    	if(strcmp(password, "123456") == 0)
    	{
    		printf("\nPassword is correct");
    	}
    	
    	if(strcmp(password, "123456") != 0)
    	{	// 2nd attempt
    		printf("\nPassword is not correct!!! please try again");
    		printf("\nPlease enter the password : ");
    		scanf("%s",&password); // wait state 
    		
    		
    		if(strcmp(password, "123456") == 0)
    		{
    			printf("\nPassword is correct");
    		}
    		
    		if(strcmp(password, "123456") != 0)
    		{
    			// 3rd attempt
    			printf("\nPassword is not correct!!! please try again");
    			printf("\nPlease enter the password : ");
    			scanf("%s",&password); // wait state
    			
    			if(strcmp(password, "123456") == 0)
    			{
    				printf("\nPassword is correct");
    			}
    			
    			if(strcmp(password, "123456") != 0)
    			{
    				printf("\nYour all attempt had finished!! Please reset device");
    			}
    			
    		}
    	}
    	return 0;
    }

Children
  • Hello ShubMane,

    The forum is not staffed during the weekend.

    ShubMane said:
    Thanks for the help.

    No problem, I am happy to help!

    ShubMane said:

    every single time I am sending the new data from NRF connect app.

    So Could u please tell me

    And I want to wait till the new data not had arrived.

    and once that data had arrived I want to out of that loop.

    I am not certain that I understand what you mean by this. You could like to wait until the new data is received? The BLE_NUS_EVT_RX_DATA event will be generated following the successful reception of a notification or write from the peer.
    You do not have to actively wait for this transfer, since the event interrupts your program.

    If you wish to wait with the connection until a certain requirement is fulfilled you may instead implement a function that sets a boolean variable that you could use as a conditional for starting advertising or scanning on either side of the link.
    I.e do not start advertising or scanning until your requirement is met.

    Keep in mind that I do not know anything about the product you are developing, but in general the security architecture you outline below might not be the most secure, since one could just brute force it by resetting the device repeatedly.
    This might not be an issue for the product you are making, but I thought I should mention it just in case.

    Best regards,
    Karl

Related