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

  • I have merged my code back together and I seem to have the same problem it is reading 51 bytes now however. 

    In order to get my SD card line to read the line for the csv I need to read 51 bytes otherwise:

     D1: 1584969092,1881,1843,1862,1862,-0.010000 

    Does not output the right length of data.

    But when I output this via my gatt connection it only takes 30 bytes.

    Why could this be?

  • Thomas said:
    When you state I have included my headers wrong. Where was this?

     It seems you have included the header files like you included your .c files. You must include header files by adding their location to this list:

     

    Thomas said:
    I have merged my code back together and I seem to have the same problem it is reading 51 bytes now however.

     What problem would that be?

     

    Thomas said:

    In order to get my SD card line to read the line for the csv I need to read 51 bytes otherwise:

     D1: 1584969092,1881,1843,1862,1862,-0.010000 

    Does not output the right length of data.

     Ok.

     

    Thomas said:
    But when I output this via my gatt connection it only takes 30 bytes.

     What do you mean by "output this via my gatt"?

  • Sorry for my late response.

    It does print out 51 bytes via the gatt (setting the characteristic value) However for me to read the data correctly via my NRF log it needs 51 bytes. But, when I inspect the data itself its 40 bytes long. So how can I chop off the excess?

    When I read this data on my mobile application I am developing it sees all the data and the stuff I dont want aka the full 51 I need a way of chopping this off. I am thinking of adding a termination character to the CSV file such as X as it wont be used in the CSV. Then when I see X it is the end of a new piece of data. Is this a good idea?

  • Do you mean if one line in the CSV file is shorter than 51 bytes? Doesn't that already have the linefeed termination on each line?

    You can find this before you send it as well. If each line is terminated by a linefeed you could say that each linefeed is the end, and set the length to that position, instead of 51. Another way is to just send all the data, 51 bytes at the time (or even more), and reassemble the file on the other side of the connection.

  • Ok I think I want to send a single line at a time which in memory is 40 bytes. How do I only send 40 bytes as opposed to the whole 51? I presume I need to index it similar to the app_uart example and if the characters \n or \r are found stop adding it to the new data to send. Something like that?

    Then send the next line of data.

Reply Children
  • Thomas said:
    I presume I need to index it similar to the app_uart example and if the characters \n or \r are found stop adding it to the new data to send. Something like that?

     Yes, indeed, and then set the p_length to a pointer to a uint16_t containing the length, just like in ble_app_uart.

  • Just a quick one: 

    I left my system running and got this,

    ERROR 1 [NRF_ERROR_SVC_HANDLER_MISSING] at D:\Segger projects\Segger Projects\SDKFOLDER\examples\ble_peripheral\gatt_issue\custom_files\Uart_comms.c:27
    00> PC at: 0x0003F671.

    This relates to this line of code:

    APP_ERROR_HANDLER(p_event->data.error_communication);

    Why does this error occur?

  • For the UART event handler, the suggestion that the error handler comes with (in this case NRF_ERROR_SVC_HANDLER_MISSING) is not correct. The err_code is 1. You must look at the event description. What sort of type is p_event->data.error_communication? See if you can find the declaration of the error_communication in a header file, and see if you can see what it means when this is set to 1.

    Hint: What type is p_event? Right click and click go to definition.

  • The type is app_uart_evt_t* is is an APP_UART_COMMUNICATION_ERROR.

    I disable the uart in my gatt connection when a connection is made. Then I restart it when I disconnect. This error comes some time after i disconnect.

  • I tried to point to the app_uart_evt_t->data.error_communication, which is an uint32_t, but I wanted you to check the description of this event in app_uart.h line 117.

    Does it say anything about the value of the error_communication?

    From app_uart.h:

    uint32_t error_communication; /**< Field used if evt_type is: APP_UART_COMMUNICATION_ERROR. This field contains the value in the ERRORSRC register for the UART peripheral. The UART_ERRORSRC_x defines from nrf5x_bitfields.h can be used to parse the error code. See also the \nRFXX Series Reference Manual for specification. */

    You are using the nRF52840, so I suggest you check the ERRORSRC register for the UART on the nRF52840:
    https://infocenter.nordicsemi.com/topic/ps_nrf52840/uart.html?cp=4_0_0_5_32_9#topic

    A value of 1 means OVERRUN. Perhaps this is to be expected when you disable and/or enable the UART when there is UART data traffic ongoing? Have you tried to ignore the APP_UART_COMMUNICATION_ERROR when you are enabling and disabling the UART?

Related