Read SD card column and sum it up

Hi,

on a SD card I have a csv-file which contains 3 columns.
time | date | value

Is it possible to read only the value column, sum up all values and write the result into a variable?
If yes, is it realistic to do this, if there are around 20.000 values?

How could I achieve this goal with an ESP8266 and the <SD.h> library?

Is there any example in the internet? I did not find one yet. :frowning:

If there are a fixed number of characters in each entry in the file then you can use the seek() function to move to the required data, read it, then seek() to the next instance which will be a fixed distance away in the file for each instance

If the number of characters in each entry is not fixed then you will need to read and ignore values that are not needed in each row of data using the commas to determine number of data items read

hm, I think it is a fixed number of characters. 0.00 - 9.99

is there a "seek"-example somewhere? I have no clue how to do that.

Do you really need an example ?

Write some data such as "ABCDEFGHIJKLMNOPQRSTUVWXYZ" to several rows of a file on the SD. Read and print the first character, seek() to position 30, read and print a character, add 30 to the seek() position, read and print. Continue until you have read the whole file

Can you see a pattern in the letters printed ?

Now try it with your real file

Alternatively, do not use seek() but read and discard characters in your file until you reach the required data, which will be in a fixed position, read it, save it then read and discard characters until the next instance of the required data and repeat until done

Alternatively read a whole line from your file, put it into an array of chars terminated by a '\0' then we can talk about extracting just the required data from it before repeating the process

Please post your complete sketch that creates the file, using code tags when you post the sketch

isn't a .csv file a number of fields separated by a comma and the line (row) terminated with a \r\n?

why not read a complete line (which is of varying length, use strtok() to separate each column and pick the string the desired column

It would help a lot for you to provide an example section of file.

My guess would be:

  float total = 0;
  while (file.available())
  {
    file.find('|');  // Skip over 'time' field
    file.find('|');  // Skip over 'date' field
    float value = file.parseFloat();  // parse 'value' field
    total += value;  // calculate running total
  }
  // 'total' now contains the total.
1 Like

man, that's great! that's exactly what I need. It works very well after replacing ('|') with (',') cause my file is comma separated.
Thank you so much.

Of course I know that by spending much time in Internet research and with many tries I could solve the problem by myself.
But I am a dad with two little children and I do have so less time for concentrating and doing this stuff. So I am very happy if someone can help me this way.
Really nice.
Thanks again.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.