Problème pour ajouter la date et l'heure à mes mesures sur carte SD

Bonjour à toutes et à tous

J'utilise un modèle RTC DS1302 pour essayer de dater mes mesures sur une carte SD. Voici le code, si quelqu'un pourrait m'aider à ajouter la ligne de code qu'il me manque ou à modifier mon code s'il n'est pas bon. J'ai essayé pas mal de chose, consulté quelques tutos mais rien;
Je précise que les mesures sont bien enregistrées au format txt sur ma carte SD mais il me manque l'heure.
Merci par avance pour votre aide :slight_smile:

/*
SD card datalogger

The circuit:

  • analog sensors on analog ins 0, 1, and 2
  • SD card attached to SPI bus as follows:
    ** MOSI - pin 11
    ** MISO - pin 12
    ** CLK - pin 13
    ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

*/

#include <SPI.h>
#include <SD.h>
#include <stdio.h>
#include <DS1302.h>
#include <Time.h>

const int chipSelect = 4;
const int kCePin = 5; // Chip Enable
const int kIoPin = 6; // Input/Output
const int kSclkPin = 7; // Serial Clock

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
switch (day) {
case Time::kSunday: return "Sunday";
case Time::kMonday: return "Monday";
case Time::kTuesday: return "Tuesday";
case Time::kWednesday: return "Wednesday";
case Time::kThursday: return "Thursday";
case Time::kFriday: return "Friday";
case Time::kSaturday: return "Saturday";
}
return "(unknown day)";
}
void printTime() {
// Get the current time and date from the chip.
Time t = rtc.time();

// Name the day of the week.
const String day = dayAsString(t.day);

// Format the time and date and insert into the temporary buffer.
char buf[50];
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
day.c_str(),
t.yr, t.mon, t.date,
t.hr, t.min, t.sec);
}

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);

// Initialize a new chip by turning off write protection and clearing the
// clock halt flag. These methods needn't always be called. See the DS1302
// datasheet for details.
rtc.writeProtect(false);
rtc.halt(false);

// Make a new time object to set the date and time.
// Sunday, September 22, 2013 at 01:38:50.
Time t(2017, 10, 20,19, 38, 50, Time::kThursday);

// Set the time and date on the chip.
rtc.time(t);

while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

Serial.print("Initializing SD card...");

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}

void loop() {
// make a string for assembling the data to log:
String dataString = "";

// read three sensors and append to the string:

C'est à ce niveau que je souhaite inscrire la date et l'heure mais je ne vois pas quelle syntaxe utiliser

float sensor1=analogRead(A0);
float temperatureV = ((sensor1*5)/1023);
float temp=((temperatureV-0.5)*100);
dataString += String(temp);
dataString += ",";

float sensor2=analogRead(A1);
float pressionV = ((sensor25)/1023);
float pression = pressionV
1000;
dataString += String(pression);
dataString += ",";

float sensor3=analogRead(A2);
float hygroV = ((sensor3*5)/1023);
float hygro = ((hygroV-0.826)/0.0315);
dataString += String(hygro);

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
delay(10000);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}

bonjour,
un truc comme ca par exemple

DateTime now = rtc.now();
        dataFile.print(now.year(), DEC);
        dataFile.print('/');
        dataFile.print(now.month(), DEC);
        dataFile.print('/');
        dataFile.print(now.day(), DEC);
        dataFile.print(" ");
        dataFile.print(now.hour(), DEC);
        dataFile.print(':');
        dataFile.print(now.minute(), DEC);
        dataFile.print(':');
        dataFile.print(now.second(), DEC)
dataFile.println(dataString);
dataFile.close();

Merci pour votre réponse, mais ça ne fonctionne pas, je viens de tester à l'instant.
J'ai déjà des erreurs à la compilation.

Jeffouille17:
Merci pour votre réponse, mais ça ne fonctionne pas, je viens de tester à l'instant.
J'ai déjà des erreurs à la compilation.

1- mettre le code entre balise code </> du 1er post
2- qu'est ce qui ne fonctionne pas?
3- on doit deviner les erreurs de compil?

déjà tu n'appelle pas printTime() ; dans le loop, donc ca ne risque pas de t'afficher l'heure etc... dans le loop

if (dataFile) {
printTime() ;
dataFile.print(buf); // pas testé sur mon pc
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
    delay(10000);
  }

Voici l'erreur affichée après compilation du dernier code que tu m'as envoyé.

D:\Arduino\programmes\Datalogger_sondes___horloge\Datalogger_sondes___horloge.ino: In function 'void loop()':

Datalogger_sondes___horloge:130: error: 'buf' was not declared in this scope

dataFile.print(buf);

^

exit status 1
'buf' was not declared in this scope

Je débute :confused:

Personne pour me donner un petit coup de pouce???

:confused:

Mets le code du premier message entre balise en éditant ton post....

/*
SD card datalogger

The circuit:

  • analog sensors on analog ins 0, 1, and 2
  • SD card attached to SPI bus as follows:
    ** MOSI - pin 11
    ** MISO - pin 12
    ** CLK - pin 13
    ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

*/

#include <SPI.h>
#include <SD.h>
#include <stdio.h>
#include <DS1302.h>
#include <Time.h>

const int chipSelect = 4;
const int kCePin = 5; // Chip Enable
const int kIoPin = 6; // Input/Output
const int kSclkPin = 7; // Serial Clock

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
switch (day) {
case Time::kSunday: return "Sunday";
case Time::kMonday: return "Monday";
case Time::kTuesday: return "Tuesday";
case Time::kWednesday: return "Wednesday";
case Time::kThursday: return "Thursday";
case Time::kFriday: return "Friday";
case Time::kSaturday: return "Saturday";
}
return "(unknown day)";
}
void printTime() {
// Get the current time and date from the chip.
Time t = rtc.time();

// Name the day of the week.
const String day = dayAsString(t.day);

// Format the time and date and insert into the temporary buffer.
char buf[50];
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
day.c_str(),
t.yr, t.mon, t.date,
t.hr, t.min, t.sec);
}

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);

// Initialize a new chip by turning off write protection and clearing the
// clock halt flag. These methods needn't always be called. See the DS1302
// datasheet for details.
rtc.writeProtect(false);
rtc.halt(false);

// Make a new time object to set the date and time.
// Sunday, September 22, 2013 at 01:38:50.
Time t(2017, 10, 20,19, 38, 50, Time::kThursday);

// Set the time and date on the chip.
rtc.time(t);

while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

Serial.print("Initializing SD card...");

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}

void loop() {
// make a string for assembling the data to log:
String dataString = "";

// read three sensors and append to the string:

C'est à ce niveau que je souhaite inscrire la date et l'heure mais je ne vois pas quelle syntaxe utiliser

float sensor1=analogRead(A0);
float temperatureV = ((sensor1*5)/1023);
float temp=((temperatureV-0.5)*100);
dataString += String(temp);
dataString += ",";

float sensor2=analogRead(A1);
float pressionV = ((sensor25)/1023);
float pression = pressionV
1000;
dataString += String(pression);
dataString += ",";

float sensor3=analogRead(A2);
float hygroV = ((sensor3*5)/1023);
float hygro = ((hygroV-0.826)/0.0315);
dataString += String(hygro);

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
delay(10000);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}

Merci pour votre aide :slight_smile:

/*
  SD card datalogger

 
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 
 
 */

#include <SPI.h>
#include <SD.h>
#include <stdio.h>
#include <DS1302.h>
#include <Time.h>

const int chipSelect = 4;
const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 7;  // Serial Clock

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}
  void printTime() {
  // Get the current time and date from the chip.
  Time t = rtc.time();

  // Name the day of the week.
  const String day = dayAsString(t.day);

  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           day.c_str(),
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);
  }

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
 
  // Initialize a new chip by turning off write protection and clearing the
  // clock halt flag. These methods needn't always be called. See the DS1302
  // datasheet for details.
  rtc.writeProtect(false);
  rtc.halt(false);

   // Make a new time object to set the date and time.
  // Sunday, September 22, 2013 at 01:38:50.
  Time t(2017, 10, 20,19, 38, 50, Time::kThursday);

  // Set the time and date on the chip.
  rtc.time(t);
 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
 
C'est à ce niveau que je souhaite inscrire la date et l'heure mais je ne vois pas quelle syntaxe utiliser   
 
    float sensor1=analogRead(A0);
    float temperatureV = ((sensor1*5)/1023);
    float temp=((temperatureV-0.5)*100);
    dataString += String(temp);
    dataString += ",";
   
    float sensor2=analogRead(A1);
    float pressionV = ((sensor2*5)/1023);
    float pression = pressionV*1000;
    dataString += String(pression);
    dataString += ",";
   
    float sensor3=analogRead(A2);
    float hygroV = ((sensor3*5)/1023);
    float hygro = ((hygroV-0.826)/0.0315);
    dataString += String(hygro);
   


  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
    delay(10000);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

Merci pour les indications :slight_smile:

Bonsoir, personne pour me filer un coup de pouce concernant l'ajout de la date et l'heure à mes mesures?
J'ai déjà essayé pas mal de chose, les données sont bien enregistrées sur ma carte SD mais il me manque l'horodatage. Voici mon code si quelqu'un peut m'aiguiller merci.

/*
  SD card datalogger

 
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 
 
 */

#include <SPI.h>
#include <SD.h>
#include <stdio.h>
#include <DS1302.h>
#include <Time.h>

const int chipSelect = 4;
const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 7;  // Serial Clock

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}
  void printTime() {
  // Get the current time and date from the chip.
  Time t = rtc.time();

  // Name the day of the week.
  const String day = dayAsString(t.day);

  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           day.c_str(),
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);
  }

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
 
  // Initialize a new chip by turning off write protection and clearing the
  // clock halt flag. These methods needn't always be called. See the DS1302
  // datasheet for details.
  rtc.writeProtect(false);
  rtc.halt(false);

   // Make a new time object to set the date and time.
  // Sunday, September 22, 2013 at 01:38:50.
  Time t(2017, 10, 20,19, 38, 50, Time::kThursday);

  // Set the time and date on the chip.
  rtc.time(t);
 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
 
C'est à ce niveau que je souhaite inscrire la date et l'heure mais je ne vois pas quelle syntaxe utiliser   
 
    float sensor1=analogRead(A0);
    float temperatureV = ((sensor1*5)/1023);
    float temp=((temperatureV-0.5)*100);
    dataString += String(temp);
    dataString += ",";
   
    float sensor2=analogRead(A1);
    float pressionV = ((sensor2*5)/1023);
    float pression = pressionV*1000;
    dataString += String(pression);
    dataString += ",";
   
    float sensor3=analogRead(A2);
    float hygroV = ((sensor3*5)/1023);
    float hygro = ((hygroV-0.826)/0.0315);
    dataString += String(hygro);
   


  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
    delay(10000);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

:confused:

comme indiqué dans le post 4, tu n'appelles pas printTime dans le loop

Bonsoir voici un code que j aplique sur mon arduino au format csv.

sprintf (moisfile,"mois%02d%02d.csv",annee,mois);
totamois = SD.open(moisfile, FILE_WRITE);

totamois.print(jour); totamois.print(";");
totamois.print(mois); totamois.print(";");
totamois.print(annee); totamois.print(";");
totamois.print(heure); totamois.print(";");
totamois.print(minut); myFile.print(";");
totamois.println(seconde);
totamois.close();

Bonne reception.