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

  • Hello,

    Could you please elaborate on what your issue is? Is the example not functioning as expected on your nRF52840? How does it behave, and how is it different from what you had expected? Are there any errors returned, or any sudden resets of the device?
    Or does the message you have sent to the nRF52840 over BLE simply not appear on the serial monitor that you have on your computer?

    Keep in mind that the serial terminal you are using might be printing your hex values as though they were ASCII characters. If you are sending values around 0x01 they might therefore not show up, since these are flow control characters in the ASCII table.
    In the case that your phone is sending ASCII characters directly, this should not then be an issue.

    Looking forward to resolving this issue together!

    Best regards,
    Karl

  • Hello Karl,

    Yes i can elaborate on what my issue is. 

    I would like to develop a firmware that as soon as the user enters the command 0x01 (hex) in a mobile application that will be connected with bluetooth to enable a function called ACTIVATE_DEVICE and that will send in UART the following command. That command will be written in the function on_write and called from the main script.

    I have started with the example of ble_app_uart and modified that accordingly. Please see attached the ble_nus.c , ble_nus.h and the main script where i call the function

     

    Could you please have a look and let me know your thoughts on this ? 

    Thanks 

    Kleanthis

  • Hello Kleanthis,

    Thank you for elaborating, this makes your issue a lot easier to understand.
    From the provided description I do not immediately see the need to modify the BLE NUS service directly to achieve the activation command.
    Would it be an option to just use the unmodified BLE NUS service, check the contents of incoming packets, and set the DEVICE_ACTIVATED variable in case the 0x01 command is received?
    In essence, you would do this as part of the NUS event handler in main.c, before the received data is passed to app_uart_put.

    In the shared screenshots I notice that you have commented out swaths of code from the ble_nus.c file. Do you intend to keep the original BLE NUS service functionality alongside the ACTIVATE command, or are you primarily concerned with forwarding these hex commands?
    If you do not intend to keep the original NUS functionality I would recommend that you go through the Service Tutorial instead. The tutorial goes through the theoretical and practical aspects of how to set up a custom service and characteristic - which might be what you are after, in this case.

    On a general note I would also recommend that you share code here in the forum using the Insert -> Code option, instead of screenshots. This makes the code a lot easier to read and understand for others.

    Best regards,
    Karl

  • Hello Karl,

    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.

    Could you help me to write this function in the main instead ? 

    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

    Looking forward for your assistance on this.

    Kind regardss,

    Kleanthis

  • /**@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 

Related