This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Using BLE NUS SERVICE without UART

Hello

I would like to send my sensor data to a mobile application using BLE SDK 15. Someone suggested me to go through the BLE NUS SERVICE example which was a good one. I understood a lot of things but now my sensor values are coming on analog pins and not on UART pins. How do i send those values? Would i have to create a whole new service for doing this or could i somehow modify the same example to work according to my needs. Thanks!  

Parents
  • Hello,

    What type of sensor do you use? Do they communicate using the UART protocol? If they do, then you can only change the uart_init() function in main.c to use the pins that you use. If they use some other protocol, then you have to read using the correct protocol, and then pass them on to the UART service. 

     

    You don't need to create anything else.

    Whatever is passed on into ble_nus_data_send() will be sent to the connected device, as long as notifications is enabled.

     

    Best regards,

    Edvin

  • Hello Edvin, Thank you for your response. I have a couple of more doubts  on UART service example before closing out on this string. I would highly appreciate if you could help me out with those. My sensors give values on analog pins using SAADC without using any other serial interface protocol. Now, i do not need to keep uart_init() function in my program and inside uart_event_handle(app_uart_evt_t * p_event) function, instead of using app_uart_get function i would call my custom function to fetch sensor values and use ble_nus_data_send() function to send that value to my connected mobile application . is that right or am i missing something here?

    Lastly, In this example my nrf52840 board is a peer and application is nordic uart mobile application. So, when i send data from my application to the board, then void nus_data_handler function is called?? and when i send data from board to mobile application then uart_event_handle() function is called???is that right?? or is it vice-versa.  I am sorry, i tried to put breakpoints to analyze this behavior but was not able to crack it.

    It would be great if i could get answers these questions. 

    Thanks and Regards,

  • Yes. That seems correct. Remove the uart_init() function, and use your own function, which you can copy parts of from the SAADC example found in SDK\examples\peripheral\saadc.

    Then, instead of printing the values in the log, you can pass them on to ble_nus_data_send() function.

     

    The behavior of the ble_app_uart example is as follows:

    When you write something on the physical UART, which either ends with '\n' (newline) or the length is greater than the buffer size, then it will uart_event_handle() is triggered, and will send the data to the mobile using the ble_nus_data_send() function.

    When you send something from the phone to the nRF, then the nus_data_handler() function is called with the p_evt->type == BLE_NUS_EVT_RX_DATA.

     

    This function will typically print the received message on the physical UART, but you can remove this. The message is stored in p_evt->params.rx_data.p_data[i], with the length of p_evt->params.rx_data.length.

    This means that the message is stored byte by byte like this:

    p_evt->params.rx_data.p_data[0];
    p_evt->params.rx_data.p_data[1];
    p_evt->params.rx_data.p_data[...];
    p_evt->params.rx_data.p_data[ p_evt->params.rx_data.length -1 ];
    p_evt->params.rx_data.p_data[ p_evt->params.rx_data.length ];

     

    Best regards,

    Edvin

Reply
  • Yes. That seems correct. Remove the uart_init() function, and use your own function, which you can copy parts of from the SAADC example found in SDK\examples\peripheral\saadc.

    Then, instead of printing the values in the log, you can pass them on to ble_nus_data_send() function.

     

    The behavior of the ble_app_uart example is as follows:

    When you write something on the physical UART, which either ends with '\n' (newline) or the length is greater than the buffer size, then it will uart_event_handle() is triggered, and will send the data to the mobile using the ble_nus_data_send() function.

    When you send something from the phone to the nRF, then the nus_data_handler() function is called with the p_evt->type == BLE_NUS_EVT_RX_DATA.

     

    This function will typically print the received message on the physical UART, but you can remove this. The message is stored in p_evt->params.rx_data.p_data[i], with the length of p_evt->params.rx_data.length.

    This means that the message is stored byte by byte like this:

    p_evt->params.rx_data.p_data[0];
    p_evt->params.rx_data.p_data[1];
    p_evt->params.rx_data.p_data[...];
    p_evt->params.rx_data.p_data[ p_evt->params.rx_data.length -1 ];
    p_evt->params.rx_data.p_data[ p_evt->params.rx_data.length ];

     

    Best regards,

    Edvin

Children
Related