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

how to read the data from peripheral and send the data over ble

hi........... nrf523

how to read the data from peripheral and send the data over ble using ble uart 

i have merged the two programs  ble_uart and twi sensor ,what changes i want to do in order to read the data from peripheral 

anyone can explain me 

see the code 

static void nus_data_handler(ble_nus_evt_t * p_evt)
{

    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
            {
                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);
 }
}

/**@snippet [Handling the data received over BLE] */


/**@brief Function for initializing services that will be used by the application.
 */
 static void services_init(void)
{
    uint32_t           err_code;
    ble_nus_init_t     nus_init;
    nrf_ble_qwr_init_t qwr_init = {0};

    // Initialize Queued Write Module.
    qwr_init.error_handler = nrf_qwr_error_handler;

    err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
    APP_ERROR_CHECK(err_code);

    // Initialize NUS.
    memset(&nus_init, 0, sizeof(nus_init));

    nus_init.data_handler = nus_data_handler;

    err_code = ble_nus_init(&m_nus, &nus_init);
    APP_ERROR_CHECK(err_code);
}
void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t       err_code;

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;

            if ((data_array[index - 1] == '\n') ||
                (data_array[index - 1] == '\r') ||
                (index >= m_ble_nus_max_data_len))
            {
                if (index > 1)
                {
                    NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                    NRF_LOG_HEXDUMP_DEBUG(data_array, index);

                    do
                    {
                        uint16_t length = (uint16_t)index;
                        err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                        if ((err_code != NRF_ERROR_INVALID_STATE) &&
                            (err_code != NRF_ERROR_RESOURCES) &&
                            (err_code != NRF_ERROR_NOT_FOUND))
                        {
                            APP_ERROR_CHECK(err_code);
                        }
                    } while (err_code == NRF_ERROR_RESOURCES);
                }

                index = 0;
            }
            break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}
/**@snippet [Handling the data received over UART] */


/**@brief  Function for initializing the UART module.
 */
/**@snippet [UART Initialization] */
static void uart_init(void)
{
    uint32_t                     err_code;
    app_uart_comm_params_t const comm_params =
    {
        .rx_pin_no    = RX_PIN_NUMBER,
        .tx_pin_no    = TX_PIN_NUMBER,
        .rts_pin_no   = RTS_PIN_NUMBER,
        .cts_pin_no   = CTS_PIN_NUMBER,
        .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
        .use_parity   = false,
#if defined (UART_PRESENT)
        .baud_rate    = NRF_UART_BAUDRATE_115200
#else
        .baud_rate    = NRF_UARTE_BAUDRATE_115200
#endif
    };

    APP_UART_FIFO_INIT(&comm_params,
                       UART_RX_BUF_SIZE,
                       UART_TX_BUF_SIZE,
                       uart_event_handle,
                       APP_IRQ_PRIORITY_LOWEST,
                       err_code);
    APP_ERROR_CHECK(err_code);
}

Parents
  • Hi Saral,

    If you want to send only the  data that you read from sensor means. keep the same code there is no change in the uploaded code. 

    you create one function. in that you read the raw data. process it and store in one variable. then do sprintf  to convert into string . then push the data using ble_nus_data_send().

    and in main function() 

    after init functions 

    in while loop you call the function with time interval of 1S delay 

    it will push the respective data. in your mobile using BLE UART app you can see the string 

    static void read_sensor_data()
    {
          m_xfer_done = false;
          uint16_t length;
          
          float f = 23.50;
          char str[80];
          
          length1 = sprintf(str, "AccX = %5d  AccY = %5d  AccZ = %5d \n", accel.x, accel.y, accel.z);
    
          rsltb = ble_nus_data_send(&m_nus, str, &length1, m_conn_handle);
          NRF_LOG_INFO("Data send...");
            NRF_LOG_FLUSH();
           printf("AccX = %5d  AccY = %5d  AccZ = %5d \r\n", accel.x, accel.y, accel.z);
            printf("Data send.. \r\n");
    
    }
    
    /**@brief Application main function.
     */
    int main(void)
    {
        bool erase_bonds;
         uint8_t sample_data[10];
    
        // Initialize.
        uart_init();
        log_init();
        timers_init();
        buttons_leds_init(&erase_bonds);
        power_management_init();
        ble_stack_init();
        gap_params_init();
        gatt_init();
        services_init();
        advertising_init();
        conn_params_init();
    
        // Start execution.
        printf("\r\nUART started.\r\n");
        NRF_LOG_INFO("BMI160 get started...");
        advertising_start();
        Acc_delay_ms(100);
    
        twi_init();
        Acc_delay_ms(50);
    
        BMI160_init();
         NRF_LOG_FLUSH();
        Acc_delay_ms(100);
    
        // Enter main loop.
        for (;;)
        {
            //idle_state_handle();
            nrf_delay_ms( 1000 ) ;
            read_sensor_data();
        }
    }

Reply
  • Hi Saral,

    If you want to send only the  data that you read from sensor means. keep the same code there is no change in the uploaded code. 

    you create one function. in that you read the raw data. process it and store in one variable. then do sprintf  to convert into string . then push the data using ble_nus_data_send().

    and in main function() 

    after init functions 

    in while loop you call the function with time interval of 1S delay 

    it will push the respective data. in your mobile using BLE UART app you can see the string 

    static void read_sensor_data()
    {
          m_xfer_done = false;
          uint16_t length;
          
          float f = 23.50;
          char str[80];
          
          length1 = sprintf(str, "AccX = %5d  AccY = %5d  AccZ = %5d \n", accel.x, accel.y, accel.z);
    
          rsltb = ble_nus_data_send(&m_nus, str, &length1, m_conn_handle);
          NRF_LOG_INFO("Data send...");
            NRF_LOG_FLUSH();
           printf("AccX = %5d  AccY = %5d  AccZ = %5d \r\n", accel.x, accel.y, accel.z);
            printf("Data send.. \r\n");
    
    }
    
    /**@brief Application main function.
     */
    int main(void)
    {
        bool erase_bonds;
         uint8_t sample_data[10];
    
        // Initialize.
        uart_init();
        log_init();
        timers_init();
        buttons_leds_init(&erase_bonds);
        power_management_init();
        ble_stack_init();
        gap_params_init();
        gatt_init();
        services_init();
        advertising_init();
        conn_params_init();
    
        // Start execution.
        printf("\r\nUART started.\r\n");
        NRF_LOG_INFO("BMI160 get started...");
        advertising_start();
        Acc_delay_ms(100);
    
        twi_init();
        Acc_delay_ms(50);
    
        BMI160_init();
         NRF_LOG_FLUSH();
        Acc_delay_ms(100);
    
        // Enter main loop.
        for (;;)
        {
            //idle_state_handle();
            nrf_delay_ms( 1000 ) ;
            read_sensor_data();
        }
    }

Children
No Data
Related