Reading CSV values from an SD Card

Hi there,

I've created a web attached device which collects temperature, humidity, and a value ID number and graphs the values on an HTML page hosted on an sd card.

I'm using an Arduino UNO with the Ethernet Shield R3.

Upon boot I want to use TextFinder to find the last line of values written to TEMP.CSV in the format: 0,26.68,45.67 where 0 is the ID, 26.68 is the temperature and 45.67 is the humidity. There are hundreds of values in the CSV file. I want to capture the last line of values and pull the ID number before the first comma.

Is there a way to use TextFinder to find the last line of values then find the first number before the first comma?

I'm a bit stuck! Thanks in advance!

There are hundreds of values in the CSV file.

Why? A separate file per day seems more useful, to me.

I assume you know approximately how long each CSV line is and know what the longest possible line would be. At startup you can find the length of the file, open the file and seek to a position which is a short distance from the end of the file - the distance needs to be at least the length of your longest line - and then read forward processing one line at a time until you get to the end of the file. The last complete line that you processed was the last line in the file.

Thanks, that's very helpful. I just need to figure out in code how to do that. On the note of the number of values. I would store by date but the project doesn't have a realtime clock yet. I am planning on adding one but until it does there will be just one file. Even so the number of values can be significant in one day if the resolution of data snapshots is set high.

Does anyone have any example code of how to implement the suggested method?

All the best Andrew

Hi: Processing has a lenght() instruction that will help u find the lenght of the recovered array; but that will be too complicated... If you just need the last value, then save the last value in another file, either txt or csv. Same way you save the values in the csv file you do it in another file; but you overwrite the previous value with the new one. In that way at the end you will have only the last value in that file and you just need to read one value. I don't know what you are using; but with processing use filename.print() instead of filename.println(). The lines will be overrwritten over and over and you will have your last value there. You need to create the file first of course with PrintWriter(); but if you did the csv file already I assume you know that. Hope you can do it.

Good idea! I'll give it a go later today..

Solved!

I stored a 2nd file as suggested with just the sd card loop integer in it then in setup it reads the file, adds 1 to it and starts the SD loop there so the next value is the next integer. I had a bit of a problem reading the SD card. Kept getting a value around 50 but realised I needed to parse using

sdloopno = ((myFile.parseInt()) + 1);

FYI, I tried the print approach but it adds the value to the last one on the same line. I've added in an sd.remove command to start the file fresh each time it's replaced.

Reading csv values from an external file...I feel like I'm back in my Computational Methods class

Thanks for letting me know. Honestly I haven't done it directly from Arduino to an sd card, so I don't know the details. Another way is to keep track of the value in a variable and save it in the sd card just at the end (at exit) and not constantly as you are doing. Use a counter if you just need the ID and save it at exit. Remember keep it simple whenever possible. Maybe it helps.

valuecounter=valuecounter+1; //Incremnt the counter every time a value is stored.

Good Luck.

mrkirkby:
Upon boot I want to use TextFinder to find the last line of values written to TEMP.CSV in the format: 0,26.68,45.67 where 0 is the ID, 26.68 is the temperature and 45.67 is the humidity. There are hundreds of values in the CSV file. I want to capture the last line of values and pull the ID number before the first comma.

Why do you need to do this?