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

  • Also I found out an issue of myflag1 never runs in the main however, this is due to my code in my main loop. I have a function called process data that works like this in the main loop.

    for(;;)
        {
            Process_data();
            if(myflag1 == true)//Does this run?
            {
                NRF_LOG_INFO("Go to reading CSV file in main code");
                myflag1 = false;
                //SEND_CSV();//Run the function to send the CSV file
            }
            sd_app_evt_wait();//Processing for SD CARD
        }

    If I un-comment my process_data function the myflag1 works. So why would my function(below stop the main execution?)

    void Process_data(void)
    {
        if(GATT_CONNECTED == false)
        {
            // Get next gyte from FIFO
            while (app_uart_get(&c) != NRF_SUCCESS);
            string[pos] = c;
    
            // Assume that all strings end with \n. If end, print and start over.
            if (string[pos] == '\n')
            {
                string[pos+1] = '\0';
                sscanf(string,  "%*d , %d , %*d , %d , %*d , %d , %*d , %d , %*d , %f",&T1_data,&T2_data,&T3_data,&T4_data,&Pdif_data);
    
                if(Print_log_enabled == 1)
                {
                    NRF_LOG_RAW_INFO("Received string: \"%s\"", string);//Print the received string
                    NRF_LOG_RAW_INFO("T1 is:\"%d\"", T1_data);//T1 data string
                    NRF_LOG_RAW_INFO("T2 is:\"%d\"", T2_data);//T2 data string
                    NRF_LOG_RAW_INFO("T3 is:\"%d\"", T3_data);//T3 data string
                    NRF_LOG_RAW_INFO("T4 is:\"%d\"", T4_data);//T4 data string
                    NRF_LOG_RAW_INFO("Pdiff" NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(Pdif_data));//Pdif data string
                }
                pos = 0;
                //Storage
                if((Unix_Time != Last_Unix_Time))//Time instance has changed and logging is enabled.
                {
                    Last_Unix_Time = Unix_Time;
                    counter++;
                    advertising_mod(counter,T1_data,T2_data,T3_data,T4_data,Pdif_data);
                    if(Log_to_sd_card == 1)
                    {
                        SD_CARD_T1[SD_Card_Data_Pointer] = T1_data;//From data string t1
                        SD_CARD_T2[SD_Card_Data_Pointer] = T2_data;//From data string t2
                        SD_CARD_T3[SD_Card_Data_Pointer] = T3_data;//From data string t3
                        SD_CARD_T4[SD_Card_Data_Pointer] = T4_data;//From data string t4
                        SD_CARD_Pdiff[SD_Card_Data_Pointer] = Pdif_data;   //From pressure sensor
                        SD_CARD_UNIX_TIME  [SD_Card_Data_Pointer] = Unix_Time;//Unix time stamp
                        SD_Card_Data_Pointer = SD_Card_Data_Pointer + 1;//Increment the SD_Card_Data_Pointer by 1
                        if(SD_Card_Data_Pointer == MAXIMUM_BUFFER_STORAGE)//Buffer is full
                        {
                            Write_block_to_sd_card();//Write a block of data to the SD card
                            SD_Card_Data_Pointer = 0;//Reset the SD_Card_Data_Pointer to 0
                        }
                    }
                }
            }
            else
            {
                pos++;
            }
            
        }
        else//Gatt connected
        {
            
        }
        NRF_LOG_INFO("Processing main loop");//Runs if gatt is not connected
        
    }

    Edit: GATT_CONNECTED is a volatile bool set inside ble_evt_handler given if the gatt is connected or not.

    Edit: I've debugged it. And when process data is un-commented the board crashes to my advertising mod function. Which should not be running as the gatt is connected. I do not know why my loop is running when the gatt is connected.

  • 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.

Reply Children
  • 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.

  • 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.

Related