writing in an old file with dynamic filename

Hi,

I’m working on a datalogger for tempreture and humidity. Arduino opens a new file with a dynamic filename (date,time). Then Arduino creats a heading table and inserts the first values.

I want the Arduino to get values for tempreture and humidity every minute. My Problem ist that Arduino creats every minute a new file.

I gues its a result of the dynamic filename. But I don’t know how to stop ist from creating new files instead of writing in the old one.

I’m not a pro. Started doing this one week ago.

Here is my Code (some notes are in german, sorry for that :wink: ):

// Einbindung der Bibliotheken
#include “DHT.h”
#include <Wire.h>
#include <DS1307.h>
#include <SD.h>
#include <SPI.h>

// Temperatur und Luftfeuchtigkeitssensor einbinden
DHT dht;

// Echtzeituhr einbinden
DS1307 clock;

//Festlegung von Intervalen
unsigned long WRITE_INTERVAL = 60000; // 1 Minuten
unsigned long lastWrite = WRITE_INTERVAL;

int SELECTED_CHIP = 4;
int count = 0;

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

// SD Karte inizialisieren
int selectedChip = 4;
if (!SD.begin(selectedChip)) {
Serial.println(“SD-Card failed or is missing”);
} else {
Serial.println(“SD-Card initialized.”);
}

// Verbindung zu DHT 22 auf Pin 2 herstellen
dht.setup(2);

// Aktivierung der Echtzeituhr
clock.begin();

}

// Schreiben der Daten in eine erstellte .csv Datei
void writeToSD(String lineContent) {

// Erstellung eines dynamischen Filename (1LOGMMDDHHMM),
// beginnend mit Ziffer n=[1;5] für die jeweilige Messstation (1, 2, 3, 4, 5)
// folgend mit MM (Monat) da nur 8"."+3 (csv) Charakter zur Verfügung stehen um File zu benennen.
// Jahr und Sekunde trotzdem definiert um für andere Messungen anpassen zu können.
int y = clock.year;
int m = clock.month;
int d = clock.dayOfMonth;
int h = clock.hour;
int mi = clock.minute;
int s = clock.second;
char filename[13] = {’\0’}; // Datum+Zeit hat maximal 12 Zeichen

sprintf(filename,“1%d%d%d%d.csv”, m, d, h, mi);

String lineHeadTable = “Time;Temperature;Humidity”;

File dataFile = SD.open(filename, FILE_WRITE);
if (dataFile) {
Serial.println (filename);
dataFile.println (lineHeadTable);
dataFile.println(lineContent); // Auf die SD-Karte schreiben
dataFile.close();
Serial.println(lineHeadTable);
Serial.println(lineContent); // Zusätzlich auf serielle Schnittstelle schreiben zum Debuggen
}
else {
Serial.println(“Error opening datafile”);
}

}

void loop() {

delay(dht.getMinimumSamplingPeriod());

// Variblenzuweisung
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();

// Überprüfung der Werte
if (isnan(temperature) || isnan(humidity)) {
Serial.println(“Fehler beim lesen von DHT”);
return;
}
if (temperature > 37) {
Serial.println(“Wert ungültig”);
return;
}
if (temperature < -20) {
Serial.println(“Wert ungültig”);
return;
}
if (humidity > 100) {
Serial.println(“Wert ungültig”);
return;
}
if (humidity < 0) {
Serial.println(“Wert ungültig”);
return;
}

// delay zur Auslesung der Werte von DHT22 und Echtzeituhr
if (millis() - lastWrite > WRITE_INTERVAL) {
String line = String(getTime()) + “;” + String(temperature) + “;” + String(humidity);
writeToSD(line);
lastWrite = millis();

}
}

// Abfragen der Zeit und Datum
String getTime() {
clock.getTime(); // Zeit vom Chip abfragen
String t = String(clock.dayOfMonth, DEC);
t += String(".");
t += String(clock.month);
t += String(".");
t += String(clock.year);
t += String(" ");
t += String(clock.hour);
t += “:”;
t += String(clock.minute);
t += “:”;
t += String(clock.second);
return t;

}

I gues its a result of the dynamic filename.

It is not. Look at the documentation for the open() method. The second argument is important. It controls whether the file is opened for read or write. If opened for write, it controls whether the file is appended to, or truncated and then written to.

Thank you Paul.

Well, I tried FILE_APPEND "SD.open(filename, FILE_APPEND)" and also without a mode "SD.open(filename)".

With FILE_Append I don't get any results. Arduino gives me just an error on serialmonitor. Without a mode I got still the same Problem.

Would you give me a hint what I should change in my code.

Try FILE_WRITE | FILE_APPEND as the mode.

SD.open(filename, FILE_WRITE | FILE_APPEND);