Read out CSV File with Processing from SD-Board connected to Arduino

Hello everybody,

I didn’t find any solution to my problem, so I decided to register here and start a new topic. Here is, what it’s all about:

I built up kind of a measuring station with an Arduino, RTC, Melexis IR Sensor and a sensor for temperature and humidity. They are all connected via I²C to the Arduino, while the microSD breakout board is connected via SPI. Every five minutes the average values of the past five minutes are written down in a csv file. I will hang up the station in a tree to measure leaf surface temperatures during the summer season.

To read put the data once a week, I want to connect the Arduino via the USB I/O to the notebook and read out the data with a processing sketch. But everything I found was, how to read out data immediately while measuring and not as a post process. My suggestion is, to write a code for the Arduino to read out the csv and a sketch running on the notebook which reads the serial port.

This code would read the file on the SD-Card on the Arduino:

#include <SPI.h>
#include <SD.h>

const char* fileName = "LOGGER08.CSV";
String fileName2 = "LOGGER08.CSV";
File logfile;
const int chipSelect = 10;

void setup(){
  Serial.begin(9600);

  Serial.println("Initializing SD card...");
  pinMode(10, OUTPUT);

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

 
  logfile = SD.open(fileName);
  if (logfile) {
    Serial.println("Content of Logfile "+ fileName2+":\n");

    while (logfile.available()) {
      Serial.write(logfile.read());
    }
    logfile.close();
    } 
  else {
    Serial.println("error opening "+fileName2);
  }
}

void loop(){
}

This is the output on the serial monitor:

I think the code for the processing sketch should look like this:

import processing.serial.*;

Serial myPort;  
String val;

void setup() {
   String portName = Serial.list()[1];
   myPort = new Serial(this, portName, 9600);
}

void draw() {
  if ( myPort.available() > 0) {  
    val = myPort.readStringUntil('\n');        
  } 
println(val); 
}

This is an extract of the processing console:

Why are there these “null” entries?
And how can I save it as a csv file without all these “null” entries?

I hope I was clear enough in my explanation.
Thanks a lot,
banff

Arduino output.JPG

Processing Output.JPG

Unlike on the Arduino, draw() is not called over and over again, AS FAST AS POSSIBLE.

So, you should never do serial input in draw().

Use myPort.bufferUntil() to define what character triggers a call to serialEvent(), and define a serialEvent() method that does the serial input.

PaulS: Unlike on the Arduino, draw() is not called over and over again, AS FAST AS POSSIBLE.

So, you should never do serial input in draw().

Use myPort.bufferUntil() to define what character triggers a call to serialEvent(), and define a serialEvent() method that does the serial input.

Thanks for your reply. I made a new sketch:

import processing.serial.*;
PFont t;
PrintWriter output;
Serial myPort;
String result = null;
int lf = 10;

void setup() {
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  myPort.clear();
  myPort.bufferUntil(lf);
  output = createWriter("C:\\Users\\...\\data2.csv");
  size(600,200);
  textAlign(CENTER, CENTER);
  fill(153);
  t = createFont("Calibri", 16, true);
} 
  
  
void draw() {
  background(0);
  textFont(t, 16);
  fill(255);
  text("Read Loggerfile from SD Card", 300, 20);
  text("Millis,Date,Time,ObjectT,AmbientT,AirT,Humidity,RTCtemp",300,70);
  text("To save and exit press any key.", 300, 140);  
}

void serialEvent(Serial p) {
  if (result != null) {
    result = p.readString();
    output.print(result);
    output.flush();
    text(result, 300, 100);
  }
}

void keyPressed() {
  output.close();
  exit();
}

Is this what you meant with the serialEvent? Is it better now?

Thanks again. banff

Is this what you meant with the serialEvent?

Yes.

Is it better now?

I'll be home a week from today. I'll try your code then, and let you know.

Okay, somehow the code above is not working.. The program starts and there is no error popping up, but the Arduino is not ridden out. The created file is empty. I don't know why. Any suggestions?

Thanks a lot, banff

but the Arduino is not ridden out.

You lost me. Perhaps you forgot to saddle it up.

Any suggestions?

Use print() to see what was actually received, of anything.

PaulS:
You lost me. Perhaps you forgot to saddle it up.

Sorry for that… I meant read out.

PaulS:
Use print() to see what was actually received, of anything.

Well, nothing.

But if I omit the if-statement (if (result != null)), it works.

Thank you,
banff

But if I omit the if-statement (if (result != null)), it works.

Well, yeah.

  if (result != null) {
    result = p.readString();

You should be testing result against null AFTER reading the string, not before reading anything.