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

Sending a CSV File over ble Gatt connection

Hi,

I need to send data from a CSV file on the nordic board(Via fatfs library). I have presently gotten a Gatt service up and running.

I have a mobile application which, can see the gatt service and write to the characteristics. From a specific write command from the mobile device I want the nordic board to send the CSV file over.

I can presently read a single line of the CSV file formatted like this: (Time stamp, data1, data 2, data3, data 4, data 5).

What I need to implement is:

  • Reading the whole file but, one or multiple lines at a time due to the size of the CSV
  • Changing the read pointer of the SD card (So I know where to continue to read from)
  • Send this data to the characteristic
  • Read this characteristic data.

My code is based upon :  https://github.com/bjornspockeli/custom_ble_service_example

My CSV file size depends on the length of time I have been storing data from. It could be upto around 1-2mbytes. I beleive I need to set up some kind of notification service. Where I read a line(or several) of the SD card, set the GATT characteristic value to this. Then change the SD card pointer. Wait till the data is received by the mobile application. Then repeat until the file is read full.y

Note I am using SDK 16 NRF52840-dk PCA 10056 S140.

This post is similar and mentions using two characteristics one from commication from the mobile app and, one for receiving the data.

https://devzone.nordicsemi.com/f/nordic-q-a/53987/nrf52840-file-transfer-via-ble

Parents
  • Hello,

    So you want to send a CSV file rom the SD card to the connected mobile phone, right?

    If I were you, I would start with the ble_app_uart example, and use ble_nus_data_send() to send your notifications. You can probably do it in the application you have started developing now. Just send the data using notifications.

    I didn't really see any questions in your post. Have you attempted this? Did you get stuck somewhere?

    Best regards,

    Edvin

  • Yes I want to send a CSV file from the SD card on the nordic board to a mobile phone. 

    Is there an example of this?

    I have not used the ble_app_uart example for this. However I do have my own custom service and Gatt connection working. My question is

    • How do I change the pointer location for the SD card?
    • How do I setup the notifications? As in how do I get them to work and change the value to be sent via this notification to the value of the CSV data?

    Also does with ble_app_uart example have a custom service? As I will need to move alot of my functionality too it.

    Also how does ble_nus_data_send work?

    Is there some guidance for how to setup notifications?

    Thanks

  • Thomas said:
    Quicky though why do you use sd_app_evt_wait(); and not idle_state_handle();?

    It was just from the top of my head. Use idle_state_handle. (the different examples use different idle handlers, depending on whether it has logging or power management enabled). Use whatever you were using. Idle_state_handle is a good one, because it processes the log (if enabled) and also does the power management handling.

    I think you should consider using an event handler for your UART input. In this case, whenever you connect, your CPU will stand on while (app_uart_get(&c) != NRF_SUCCESS), so it will never reach ide_state_handle(). Again, check how this is done in the ble_app_uart example.

    I didn't quite get your question as for why the it is advertising when you are connected. Where are the places that you call advertising start from in your project?

  • I presume I therefore, move my process_data()  inside of my uart handler. If the event is APP_UART_DATA_READY?

  • Oh, I guess you have an event handler for the UART. I thought that the app_uart_get was another function. However, I suggest you look at the uart_event_handle in ble_app_uart. You should call app_uart_get whenever you get the RX event on the UART, because you want to empty the buffer. 

    I don't know, if it works in your case, that is probably fine. It is hard to say only from the snippets.

  • Yeah it does work, I was surprised I could just move it across though with only minor tweaks but, all my functionality seems to still work fine.

    What is the best way of adapting the notification timer so that I can send the CSV file over.

  • Trial and error.

    I honestly don't know. Start at one end of the CSV file, and iterate through it. Try to send as large packets as possible. Try to increase the MTU size, data size and enable data length extension in order to be able to send packets longer than 20 bytes. If you ask me how to do it, I will refer to the ble_app_uart example again. Try it, and let me know if you are stuck.

Reply
  • Trial and error.

    I honestly don't know. Start at one end of the CSV file, and iterate through it. Try to send as large packets as possible. Try to increase the MTU size, data size and enable data length extension in order to be able to send packets longer than 20 bytes. If you ask me how to do it, I will refer to the ble_app_uart example again. Try it, and let me know if you are stuck.

Children
  • How do I increase the size of what is sent via the notification? Each datum I have is 51 bytes. Therefore, I would like to be at least this large.

  • Please refer to the ble_app_uart example. look at where it calls ble_nus_data_send, and what this function does.

    Are you able to send an array of 51 bytes? If not, what is it that prevents you from doing so? what does sd_ble_gatts_hvx() return?

  • I have not tried to send more than 8 bytes. And only with using the notification timer as well. As the parameter that gets passed into is uint8.  How do I change this so I can use a larger size than 51bytes?

    I have looked at the ble_app_uart and, its a bit confusing. I do not need its uart functionality. Also I do not need blcm_link_ctx_get. Below is my update code.

    uint32_t ble_cus_custom_value_update(ble_cus_t * p_cus, uint8_t custom_value)
    {
        NRF_LOG_INFO("In ble_cus_custom_value_update. \r\n"); 
        if (p_cus == NULL)
        {
            return NRF_ERROR_NULL;
        }
    
        uint32_t err_code = NRF_SUCCESS;
        ble_gatts_value_t gatts_value;
    
        // Initialize value struct.
        memset(&gatts_value, 0, sizeof(gatts_value));
    
        gatts_value.len     = sizeof(uint8_t);
        gatts_value.offset  = 0;
        gatts_value.p_value = &custom_value;
    
        // Update database.
        err_code = sd_ble_gatts_value_set(p_cus->conn_handle,
                                          p_cus->custom_value_handles.value_handle,
                                          &gatts_value);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    
        // Send value if connected and notifying.
        if ((p_cus->conn_handle != BLE_CONN_HANDLE_INVALID)) 
        {
            ble_gatts_hvx_params_t hvx_params;
    
            memset(&hvx_params, 0, sizeof(hvx_params));
    
            hvx_params.handle = p_cus->custom_value_handles.value_handle;
            hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
            hvx_params.offset = gatts_value.offset;
            hvx_params.p_len  = &gatts_value.len;
            hvx_params.p_data = gatts_value.p_value;
    
            err_code = sd_ble_gatts_hvx(p_cus->conn_handle, &hvx_params);
            NRF_LOG_INFO("sd_ble_gatts_hvx result: %x. \r\n", err_code); 
        }
        else
        {
            err_code = NRF_ERROR_INVALID_STATE;
            NRF_LOG_INFO("sd_ble_gatts_hvx result: NRF_ERROR_INVALID_STATE. \r\n"); 
        }
    
    
        return err_code;
    }
    

  • uint8_t isn't 8 bytes. It is one byte.

    I know that you don't need UART, but what I meant that you could have a look at is how ble_nus_data_send() uses an array of uint8_t.

    Please check it out and try to implement something similar.

    Also, you don't need to call your ble_cus_custom_value_update() or ble_nus_data_send() from the timeout. But you need to call it from somewhere. Probably, in your case, you want to call it with an array of data that you have just read out from your SD-card.

    So (pseudocode) : 

    static void sd_card_update(void)
    {
        custom_value_update();
    }

    Please, please look at ble_app_uart. This characteristic has a value that is several bytes long, instead of only one. You need to set this in services_init() Look at how it is done in nus_init() in ble_app_uart.

  • I have a function in my main thats called from a command via gatt called SEND_CSV. It crashes with sd_ble_gatts_hvx result: C. This comes from my custom function ble_cus_send_csv. Which I made by looking at the ble_app_uart and my previous update function. Do you know what i could be doing wrong. Also my data i am sending is 51bytes. It is a char array though which I presume is the same as a uint8_t. 

    {
        static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
        static uint8_t index = 0;
        ret_code_t err_code;
        SD_CARD_Read_Line();//Gets a line of 51bytes of data
        NRF_LOG_INFO("D1: %s\r\n",ble_csv_data);
        
        uint16_t data_length =0;
        data_length =sizeof(ble_csv_data);
     
        err_code = ble_cus_send_csv(&m_cus, ble_csv_data,&data_length, m_conn_handle);//Causes error
               
        APP_ERROR_CHECK(err_code);
         
    }
    uint32_t ble_cus_send_csv(ble_cus_t * p_cus, uint8_t   * p_data, uint16_t  * p_length, uint16_t    conn_handle)
    {
        ble_gatts_hvx_params_t hvx_params;
        NRF_LOG_INFO("Sending CSV.\r\n"); 
        if (p_cus == NULL)
        {
            return NRF_ERROR_NULL;
        }
    
        uint32_t err_code = NRF_SUCCESS;
    
        // Send value if connected and notifying.
        if ((p_cus->conn_handle != BLE_CONN_HANDLE_INVALID)) 
        {
            
            memset(&hvx_params, 0, sizeof(hvx_params));
    
            hvx_params.handle = p_cus->custom_value_handles.value_handle;
            hvx_params.p_data = p_data;
            hvx_params.p_len  = p_length;
            hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
    
            err_code = sd_ble_gatts_hvx(p_cus->conn_handle, &hvx_params);
            NRF_LOG_INFO("sd_ble_gatts_hvx result: %x. \r\n", err_code); 
        }
        else
        {
            err_code = NRF_ERROR_INVALID_STATE;
            NRF_LOG_INFO("sd_ble_gatts_hvx result: NRF_ERROR_INVALID_STATE. \r\n"); 
        }
        return err_code;
    }

Related