Hello,
I am a French engineere (sorry for the mistakes/poor English). My team and I are working on a project to detect the COVID thanks to breathing samples. We are testing devices to capture the volatil organic copounds. During those tests, we need to monitor the temperature and the humidity.
We are using a sensor, a SD card to store our temperature and humidity. We use a RTC ( DS1307) to indicate inside the file the time of the test.
I already have set the time of the RTC thanks to a sketch and it works perfectly when still on the computeur. The RTC has a batterie, so when the arduino is unpluged then re pluged, it doesn't need us to give it the time again. However, when using a alimentation, it only gives this :
165/165/165_45h165min85
Help please?
Here is my code, inspired by this https://www.reichelt.com/magazin/fr/mesure-de-la-temperature-carte-arduino-uno-partie-2/
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <DS1307.h>
#include <DHT.h>
DS1307 clock;
DHT dht(A0, DHT22);
byte DEGREE_SYMBOL = 0;
byte degree[8] = {
0b00000,
0b00010,
0b00101,
0b00010,
0b00000,
0b00000,
0b00000,
0b00000
};
unsigned long TOTAL_MESURE = 120000; // 2 min
unsigned long WRITE_INTERVAL = 10000; // 10000 Millisecondes
unsigned long lastWrite = 0; //dernier temps où on a écrit
unsigned long initialisation = 1; //pour n'écrire l'heure qu'une seule fois au début et après on met cette variable à 0
void setup() {
Serial.begin(9600); // Initialiser l’interface séquentielle
// Initialiser la carte SD
int selectedChip = 4;
if (!SD.begin(selectedChip)) {
Serial.println("SD-Card failed or is missing");
} else {
Serial.println("SD-Card initialized.");
}
dht.begin(); // Établir le lien avec le capteur
clock.begin(); // Établir le lien avec l’horloge temps réel
}
void loop() {
String TEMPS_DEBUT = String(getTime());
if (initialisation) {
writeToSD(TEMPS_DEBUT);
initialisation = 0;
}
while (millis()<TOTAL_MESURE) {
if (millis() - lastWrite > WRITE_INTERVAL) {
//premièrement lecture et vérification qu'on peut lire
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT");
return; // Aucune donnée --> loop() quitter à nouveau à ce stade
}
String line = String(temperature) + ";" + String(humidity);
writeToSD(line);
lastWrite = millis();
}
}
}
String getTime() {
clock.getTime(); // Interroger le temps Ă partir du microprocesseur
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 += "h";
t += String(clock.minute);
t += "min";
t += String(clock.second);
return t;
}
void writeToSD(String line) {
File dataFile = SD.open("datalog.csv", FILE_WRITE);
if (dataFile) {
dataFile.println(line); // Enregistrer sur la carte SD
dataFile.close();
Serial.println(line); // Enregistrer également au niveau de l’interface séquentielle pour résoudre les anomalies
}
else {
Serial.println("Error opening datafile");
}
}