This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I want to digital high a gpio pin on nrf51dk, when I receive the text say "ON" from my android smartphone.How can I accomplish that using the nrf ble app uart example.I am not sure where I can compare the incoming ble data

Toggling a gpio pin upon receiving a particular string from ble uart app

  • Hi,

    The data sent from the smartphone is handled in the function called nus_data_handler(), that you can find in main.c. In the code below, we look for the string “ON” and “OFF”, configure pin 4 as an output pin and set the pin high when we receive “ON” and set the pin low when we receive “OFF”. After that the data is sent to the UART module as normal.

    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
        static char uart_string[BLE_NUS_MAX_DATA_LEN];
        memset(uart_string,0,BLE_NUS_MAX_DATA_LEN);
        memcpy(uart_string, p_data, length);
        
        if(strcmp(uart_string,"ON") == 0)
        {
            nrf_gpio_cfg_output(4);
            nrf_gpio_pin_set(4);
        }
        
        if(strcmp(uart_string,"OFF") == 0)
        {
            nrf_gpio_cfg_output(4);
            nrf_gpio_pin_clear(4);
        }
        
        for (uint32_t i = 0; i < length; i++)
        {
            while (app_uart_put(p_data[i]) != NRF_SUCCESS);
            
        }
        while (app_uart_put('\r') != NRF_SUCCESS);
        while (app_uart_put('\n') != NRF_SUCCESS);
        
    }
    
  • It worked like magic.Thanks a lot

Related