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

How to store data in an array and send it over the array

Hi.. 

I am working nrf52832 with accelerometer sensor, I want to store the data about 100hz for 1sec in flash, I want to store it an array and send it over ble how to do that?? How to send the array of 100 over ble please let me know 

Thanks & Regards 

Bavithra k

  • Hi 

    How many bytes of data do you need for every sample of the sensor?

    Most of the send functions in the SDK take a uint8_t pointer as an argument, expecting an array of uint8_t values, but it is possible to cast a pointer to any type of array to uint8_t * before sending it. 

    Say that you have a struct used to contain your accelerometer data, you can then create an array of these structs, fill them with sensor data, and pass a pointer to the struct array to the sd_ble_gatts_hvx function (or whatever other function you plan to use to send the data).

    The critical thing is that you have to split the transmission into multiple packets if the total size of the array is larger than your ATT MTU size. 

    Best regards
    Torbjørn

  • I think 8bytes . I don't know exact size. I am sending the timestamp along with the accelerometer data x, y, z.  I tried this method but I am loosing the packets. I want to send 100 packets per second that's why I am tring  to use structure 

    Is it possible? 

  • Hi 

    I wouldn't recommend converting the data to strings before you send it. You get more predictable and efficient communication if you send the raw data, and convert it to strings on the receiving side. 

    100 packets pr second might be possible, but it depends on the connection parameters you use, and on the Bluetooth implementation in the peer device (central). For instance, if you connect to a mobile phone it varies a lot from phone to phone what kind of BLE performance you can expect. 

    You will get more reliable communication if you group multiple samples into fewer packets, rather than send each ~8 byte update as a single packet, because there is quite a bit of overhead for each packet you send. At a minimum you can send 20 bytes pr packet, but most devices you connect to will allow even larger packets that this, up to a maximum of 244 bytes. 

    If you are having issues sending 100 packets pr second I recommend you start by looking at the connection parameters, and then try to group multiple samples together in order to get more efficient communication. 

    Best regards
    Torbjørn

  • how to send the array using ble_nus_data_send function  i am trying to send the array of 100

        for(i=0;i<100;i++)
        {
          NRF_LOG_INFO ("%d.%s\n",i,p[i]);
    
          ble_nus_data_send(&m_nus,p[i],) //what length shiuld i give , is this coorect format 
          //NRF_LOG_INFO("yes");
        }

  • Hi 

    As mentioned you should send up to 244 bytes at a time to maximize throughput. 

    Assuming each element in your array is 8 bytes in size this means you can send 30 elements in each packet (30*8 = 240), but if you have to send 100 elements at a time it is easier to just send 25 elements in four equally sized packets. 

    Then you could implement your loop something like this:

    uint32_t err_code;
    for(i = 0; i < 100; i += 25)
    {
      NRF_LOG_INFO ("%d.%s\n",i,p[i]);
    
      do
      {
        err_code = ble_nus_data_send(&m_nus, (uint8_t*)&p[i], 8*25); 
      } while(err_code == NRF_ERROR_RESOURCES);
      
      // Any other error code should be handled here, at minimum by doing an assert
      APP_ERROR_CHECK(err_code);
    }

    Please note that handling the NRF_ERROR_RESOURCES error is important. The looped approach in my code is not the best solution if you are trying to send a lot of data, but if you only send 4 packets at a time it should work fine. 

    For more details on this error please give this case a read. 

    Best regards
    Torbjørn

Related