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

Problem sending data array using BLE_NUS_DATA_SEND

Hi,

I am simply trying to send a sample of data from a TWI sensor to smartphone via BLE with a 2 second delay between each send.

I've adapted the ble_app_uart and twi_sensor examples, and everything is working properly except one thing. The data is not received properly.

The BLE_NUS_DATA_SEND is sending and working correctly because I do see the "value" title appear in the NRF Connect app after enabling notifications, but there is just no value there (just empty space).

To check the data, I printed/logged the sensor value in the data_send function. M_sample prints correctly, but when I try to print the data array, it doesn't show. So I believe this problem is caused when m_sample is put into the data array. This tells me that m_sample is of the wrong type or form.

I have 2 questions:

1. What type/size/form does my data need to be in to be printed/logged, and why can't I print the data_array but can with the m_sample?

2. What changes need to be applied to send my number sample (m_sample) via BLE_NUS_DATA_SEND?

 

I'm sure these questions are obvious to someone. I may have missed some basic things in the documentation, but I'd really appreciate if someone could point out my problem.

SDK15.3, SES

void data_send(uint8_t m_sample)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t       err_code;

    data_array[BLE_NUS_MAX_DATA_LEN - 1] = m_sample;


NRF_LOG_INFO("check msample: %d Celsius degrees.", m_sample);

       // NRF_LOG_INFO("Check array.", data_array); 
          NRF_LOG_INFO("Check array2.", m_sample); 
          
                    //NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                    //NRF_LOG_HEXDUMP_DEBUG(data_array, index);

                   
                        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) &&// This part is right (saw another user do it).
                            (err_code != NRF_ERROR_RESOURCES) &&
                            (err_code != NRF_ERROR_NOT_FOUND))//different
                        {
                            APP_ERROR_CHECK(err_code);
                      }

}

Parents
  • What do you expect this code to do?

       data_array[BLE_NUS_MAX_DATA_LEN - 1] = m_sample;

    loads your sample value into the last element of the array.

                            uint16_t length = (uint16_t)index;
                            err_code = ble_nus_data_send(&m_nus, &data_array, &length, m_conn_handle);

    index is set to zero at the start of the function, and you haven't shown anything that ever updates it - so you are always specifying a length of zero!

  • I expected the m_sample to be inserted into the array with a length and size that matches the requirement of ble_nus_data_send.

    In that case, why would the value of data_array be such a large number? I printed it, and it's showing a value of 536884548. What could this number represent then? 

  • Well. This is C programming, so there is no magic here, unfortunately. 

    ble_nus_data_send takes a data array (in your case the variable data_array[]). Now, it doesn't send the entire array, but from element 0 to element p_length (3rd element, e.g. &length). If this value is 0, then it will send 0 bytes, so if you want to send one byte, which it looks like, based on that you take in one (!) byte, m_sample, you should try to set index to 1. However, I suspect that you are actually trying to send more than one byte. Look at how it is done in the ble_app_uart example.

    Now. It returns 5, e.g. NRF_ERROR_NOT_FOUND. This simply means that you aren't connected to anything. If you try to connect, you will probably get the return value NRF_ERROR_INVALID_STATE (== 8). This means that notifications aren't enabled. 

    Unless ble_nus_data_send() returns 0 the data array that you try to send have not been queued. 

    However, to send data from a TWI sensor, you should look at the ble_app_uart example, and take some inspiration from the uart_event_handle() in main.c.

    BR,

    Edvin

  • This is C programming

    Absolutely!

    I expected the m_sample to be inserted into the array with a length and size that matches the requirement of ble_nus_data_send.

    That's not what your code does - as described above.

    why would the value of data_array be such a large number?

    Again, this is standard 'C' programming - nothing specifically to do with Nordic or even embedded microcontrollers!

    In 'C', the name of an array - such as "data_array" -  gives a pointer to the first element of the array; or, in other words, the address of the first element of the array.

    So, if you print that, you will very likely get a large number!

    showing a value of 536884548

    So that's 0x20003544 - which is, indeed, an address in the nRF52 RAM:

    I think you might need to brush-up on your 'C' - so here are some 'C' learning & reference materials:

    http://blog.antronics.co.uk/2011/08/08/so-youre-thinking-of-starting-with-c/

Reply Children
No Data
Related