nRF_SDK_17.1.0 s140 pca10056 BLE_UART

Hello all,

I am trying to develop an application where the user will input some specific commands i.e hex value of 0x01 via a mobile application and then via bluetooth this command will be passed to the computer using UART.

I have compiled the ble app uart on my nrf52840 but i am struggling to modify the code for my application.

Could someone help me with this ? 

Thanks

Kleanthis

Parents
  • /**@brief Function for handling the data from the Nordic UART Service.
     *
     * @details This function will process the data received from the Nordic UART BLE Service and send
     *          it to the UART module.
     *
     * @param[in] p_evt       Nordic UART Service event.
     */
    /**@snippet [Handling the data received over BLE] */
    static void nus_data_handler(ble_nus_evt_t * p_evt)
    {    
    
     /**if (p_evt-> type == ACTIVATE_DEVICE)
    {
    uint32_t err_code;
    NRF_LOG_DEBUG("Device is now activated.");
    
    }
    */
    
          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
                {   if (p_evt_write->data[0] == 0x01)
                 
                    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);
            }
        }
    
    }

    I have also attached the code i have added on the main.c . Still is not clear to me how i shall relay the command 0x01 to be passed via BLE to UART 

Reply
  • /**@brief Function for handling the data from the Nordic UART Service.
     *
     * @details This function will process the data received from the Nordic UART BLE Service and send
     *          it to the UART module.
     *
     * @param[in] p_evt       Nordic UART Service event.
     */
    /**@snippet [Handling the data received over BLE] */
    static void nus_data_handler(ble_nus_evt_t * p_evt)
    {    
    
     /**if (p_evt-> type == ACTIVATE_DEVICE)
    {
    uint32_t err_code;
    NRF_LOG_DEBUG("Device is now activated.");
    
    }
    */
    
          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
                {   if (p_evt_write->data[0] == 0x01)
                 
                    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);
            }
        }
    
    }

    I have also attached the code i have added on the main.c . Still is not clear to me how i shall relay the command 0x01 to be passed via BLE to UART 

Children
  • kleanthise said:
    Ideally yes i would like to use the unmodified BLE NUS service, check the contents of incoming packets and se the DEVICE_ACTIVATED variable in case the 0x01 command is received.

    Great, thank you for confirming this.
    In this case, I recommend that you just add this as a check in before the app_uart_put like I mentioned earlier. In your case I suppose it would look something like this:

    ..
    #define COMMAND_ACTIVATION 0x01
    ..
        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);
            
            if(p_evt->params.rx_data.p_data[0] == COMMAND_ACTIVATION)
            {
                m_device_state = 1;
            }
            
            if(m_device_state)
            {
                ..    
            }
            else
            {
                NRF_LOG_INFO("Device not activated.");
            }
    ..


    However, you should also make sure that your phone application is not sending any unexpected ASCII control characters, so that you can be sure that these specific command bytes will only ever be sent intentionally.

    kleanthise said:
    I have no uncommented the original code and i am back on the defaul ble_nus.c and h files. In the real case scenario i would like to be able to keep the original BLE NUS Service functionality alongside the activate command since i would be receiving commands from a pc using the uART protocol ( those will be an array of 0 and 1) and  i will just showing them on a mobile application

    Please take care to make sure that the NUS service ble_nus.c and ble_nus.h files are reverted to their original state. If you at any point later notice some unexpected behavior with the NUS service I would download a new SDK instance and replace these files with the unmodified ones, just to make sure that there are no unexpected changes.

    Best regards,
    Karl

Related