Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Need help with the ble_app_uart example

Hello Everyone,

I am a beginner and working on nRF52832 DK using SDK v15 and using keil mdk armv5.25 as a compiler.

Currently I am trying to modify an example in the SDK which is ble_app_uart. I want to modify such that whenever i send "LED1_ON" from my mobile to the DK over ble, the LED 1 on the board should turn on and vice versa, also the opposite happens when i send "LED1_OFF" from mobile. Please help me out with issue as I have tried but cant understand things properly.

  • Hi,

    Can you elaborate which part is not working. Are you not able to receive the command or to set/clear the LED? The example use the BSP module to control LED's, so you should be able to parse the command in the nus_data_handler() function and set the LED's using bsp_indication_set(). Your LED's state will change whenever another part of the application calls bsp_indication_set() as the BSP module is also used for other stuff. You can comment out the other calls (or remove the BSP module from the application altogether) if you don't want to use LED's (or buttons) for anything else.

  • I am able to properly send receive any text over uart/ble. What I am not able to do is to control the leds based on a certain string received from the phone, lets say i send LED1ON from phone, then led1 should turn on, but i dont exactly know how to accomplish that.

  • Hi,

    Essentially you need to do the following:

    1. Parse the command in the nus_data_handler() function, and decide what to do (turn on or off LED)
    2. Set or clear the GPIO directly (after removing the BSP module) or via the BSP

     

  • Yes i understood that much that modification needs to be done in nus_data_handler() but i dont know how to do it. Can you please show me?

  • Hi,

    Please find the attached diff which shows exactly what you need to do.

    diff --git a/examples/ble_peripheral/ble_app_uart/main.c b/examples/ble_peripheral/ble_app_uart/main.c
    index 16ab0a1..f01405a 100644
    --- a/examples/ble_peripheral/ble_app_uart/main.c
    +++ b/examples/ble_peripheral/ble_app_uart/main.c
    @@ -203,6 +203,17 @@ static void nus_data_handler(ble_nus_evt_t * p_evt)
             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);
     
    +       if (strcmp(p_evt->params.rx_data.p_data, "LED1_ON") == 0)
    +       {
    +           NRF_LOG_DEBUG("Turning on LED 1");
    +           nrf_gpio_pin_clear(BSP_LED_0);
    +       }
    +       else if (strcmp(p_evt->params.rx_data.p_data, "LED1_OFF") == 0)
    +       {
    +           NRF_LOG_DEBUG("Turning off LED 1");
    +           nrf_gpio_pin_set(BSP_LED_0);
    +       }
    +
             for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
             {
                 do
    @@ -311,11 +322,8 @@ static void conn_params_init(void)
      */
     static void sleep_mode_enter(void)
     {
    -    uint32_t err_code = bsp_indication_set(BSP_INDICATE_IDLE);
    -    APP_ERROR_CHECK(err_code);
    -
         // Prepare wakeup buttons.
    -    err_code = bsp_btn_ble_sleep_mode_prepare();
    +    uint32_t err_code = bsp_btn_ble_sleep_mode_prepare();
         APP_ERROR_CHECK(err_code);
     
         // Go to system-off mode (this function will not return; wakeup will cause a reset).
    @@ -337,8 +345,6 @@ static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
         switch (ble_adv_evt)
         {
             case BLE_ADV_EVT_FAST:
    -            err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
    -            APP_ERROR_CHECK(err_code);
                 break;
             case BLE_ADV_EVT_IDLE:
                 sleep_mode_enter();
    @@ -362,8 +368,6 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
         {
             case BLE_GAP_EVT_CONNECTED:
                 NRF_LOG_INFO("Connected");
    -            err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
    -            APP_ERROR_CHECK(err_code);
                 m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
                 err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
                 APP_ERROR_CHECK(err_code);
    @@ -633,12 +637,15 @@ static void buttons_leds_init(bool * p_erase_bonds)
     {
         bsp_event_t startup_event;
     
    -    uint32_t err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
    +    uint32_t err_code = bsp_init(BSP_INIT_BUTTONS, bsp_event_handler);
         APP_ERROR_CHECK(err_code);
     
         err_code = bsp_btn_ble_init(NULL, &startup_event);
         APP_ERROR_CHECK(err_code);
     
    +    // Configure GPIO's for LED's as output without using the BSP
    +    nrf_gpio_range_cfg_output(BSP_LED_0, BSP_LED_3);
    +
         *p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA);
     }
     
    

Related