Sending data array over ble nus

Hi, 

I'm trying to send an data array over ble nus. 

static void app_timer_handler(void * p_context)
{
  if(m_conn_handle != BLE_CONN_HANDLE_INVALID)
  {
    uint8_t data_array[4];

    float val0;
    float val1;
    float val2;
    float val3;

    uint8_t data0[20];
    uint8_t data1[20];
    uint8_t data2[20];
    uint8_t data3[20];

    ret_code_t err_code;

    err_code = nrfx_saadc_sample_convert(0, &m_sample);
    APP_ERROR_CHECK(err_code);

    val0 = m_sample * 3.3 / 4096;
    sprintf((char*)data0, "%.2f", val0);
    data_array[0] = data0; 

    err_code = nrfx_saadc_sample_convert(1, &m_sample);
    APP_ERROR_CHECK(err_code);

    val1 = m_sample * 3.3 / 4096;
    sprintf((char*)data1, "%.2f", val1);
    data_array[1] = data1; 

    err_code = nrfx_saadc_sample_convert(2, &m_sample);
    APP_ERROR_CHECK(err_code);

    val2 = m_sample * 3.3 / 4096;
    sprintf((char*)data2, "%.2f", val2);
    data_array[2] = data2;

    err_code = nrfx_saadc_sample_convert(3, &m_sample);
    APP_ERROR_CHECK(err_code);

    val3 = m_sample * 3.3 / 4096;
    sprintf((char*)data3, "%.2f", val3);
    data_array[3] = data3;
  
    uint16_t d_len = sizeof(data_array)-1;
    
    err_code = ble_nus_data_send(&m_nus, data_array, &d_len, m_conn_handle); 
    APP_ERROR_CHECK(err_code);
  }
}

This didn't work. 

I'm also getting this warning.

I'm doing something wrong, but I don't know what. Do I also have to change the receiving part of the central device to receive the data array?

  • Hello,

    I think you need to look at the pointer - array side of this application.

    Try to print the content of the data_array[] array in your log, or look at it in a debug session.

    What do you expect the data_array to look like? What should be in data_array[0], data_array[1], data_array[2] and data_array[3]? Hint: It only holds a single uint8_t value in each of the elements, since it is a uint8_t array.

    Then you try to set each of these uin8_t parameters equal to the address (pointer) of each of the 20 byte arrays, so what the data_array[] values actually are after e.g.

    data_array[3] = data3;

    I am not sure. data_array[3} has 8 bits, and the data3 array pointer has 32 bits. So I guess it will be either the first 8 or last 8 bits. It is not really specified in C and hence, you get the warnings that you see.

    So how to actually send the data you want to send:

    I suggest you either create one large array, data[80], and then you sprintf to the 0th, 20th, 40th and 60th element, and then you just pass the entire array through ble_nus_data_send(). Remember to set the length to 80, and not 4. 

    Best regards,

    Edvin

  • Hi,

    Thank you I will try this.

    Is it possible to send an array of size data[80] over ble_nus without any change?

  • Hi Edvin,

    I did tried in the way you suggest:

    I suggest you either create one large array, data[80], and then you sprintf to the 0th, 20th, 40th and 60th element, and then you just pass the entire array through ble_nus_data_send(). Remember to set the length to 80, and not 4. 

    but I only get once a random value see:

    This are the changes I did made

    static void app_timer_handler(void * p_context)
    {
      if(m_conn_handle != BLE_CONN_HANDLE_INVALID)
      {
        uint8_t data_array[80];
    
        float val0;
        float val1;
        float val2;
        float val3;
    
        uint8_t data0[20];
        uint8_t data1[20];
        uint8_t data2[20];
        uint8_t data3[20];
    
        ret_code_t err_code;
    
        err_code = nrfx_saadc_sample_convert(0, &m_sample);
        APP_ERROR_CHECK(err_code);
    
        val0 = m_sample * 3.3 / 4096;
        sprintf((char*)data0, "%.2f", val0);
        data_array[0] = data0; 
    
        err_code = nrfx_saadc_sample_convert(1, &m_sample);
        APP_ERROR_CHECK(err_code);
    
        val1 = m_sample * 3.3 / 4096;
        sprintf((char*)data1, "%.2f", val1);
        data_array[20] = data1; 
    
        err_code = nrfx_saadc_sample_convert(2, &m_sample);
        APP_ERROR_CHECK(err_code);
    
        val2 = m_sample * 3.3 / 4096;
        sprintf((char*)data2, "%.2f", val2);
        data_array[40] = data2;
    
        err_code = nrfx_saadc_sample_convert(3, &m_sample);
        APP_ERROR_CHECK(err_code);
    
        val3 = m_sample * 3.3 / 4096;
        sprintf((char*)data3, "%.2f", val3);
        data_array[60] = data3;
      
        uint16_t d_len = sizeof(data_array)-1;
        
        err_code = ble_nus_data_send(&m_nus, data_array, &d_len, m_conn_handle); 
        APP_ERROR_CHECK(err_code);
      }
    }

    I still don't know where it exactly goes wrong. Can you help me out?

  • Hi Ahmed,

    Edvin will be out of office for a few weeks and I will be handling this case in the meanwhile.

    Edvin said:

    Try to print the content of the data_array[] array in your log, or look at it in a debug session.

    What do you expect the data_array to look like? What should be in data_array[0], data_array[1], data_array[2] and data_array[3]? Hint: It only holds a single uint8_t value in each of the elements, since it is a uint8_t array.

    Did you try to print the contents in a log or in a debug session? Did they look as expected? If you are not too familiar with pointers and arrays in C, I would recommend you follow some online resources such as this one or similar to get some ideas on how they should work. Then as a second step, I would try to create a simple script in C where you can try to add, remove, read, change and print the arrays to see if they behave as you expected them to.

    Try the abovementioned before you add the saadc and ble_nus functionalities. If you are using nRF5 SDK, the  logging module (and this video tutorial example created by external people) might be of help with trying to examine the input of your data array.

    Kind regards,
    Andreas

Related