0

So I have a program that measures temperature and humidity over time. Here isthe code I use:

#include <SD.h>
#include <SPI.h>
#include <DHT.h>
#include <Wire.h> 
#include "RTClib.h"

#define DHTPIN 2 
#define DHTTYPE 22
const int chipSelect = 10; 


DHT dht(DHTPIN, DHTTYPE);
RTC_DS3231 rtc;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("DHT22 sensor reading");

  if(! rtc.begin()) {
    Serial.println("Could not find RTC");
    while(1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}


void loop() {

  //temperature measurements
  float hum = dht.readHumidity();
  float temp = dht.readTemperature();

  if( isnan(temp) || isnan(hum )) {
    Serial.println("Failed to read from DHT22 sensor!");
    return;
  }

  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.println(" *C");

  //determining the date
  DateTime now = rtc.now();

  int dayy;
  int monthh;
  int yearr;

  int hours;
  int minutes;
  int seconds;

  String date;
  dayy = now.day();
  monthh = now.month();
  yearr = now.year();
  date = String(dayy) + String(monthh) + String(yearr);

  int seconds_passed; // how many seconds have passed since the start of the day: used for the data plot
  hours = now.hour();
  minutes = now.minute();
  seconds = now.second();
  seconds_passed = ((hours * 60) + minutes)* 60 + seconds;




  //writing to SD card

  if(SD.begin(chipSelect)) {
    Serial.println("Card is initialized! Ready to go!");
  }
  else {
    Serial.println("Error with initializing the card!");
    return;
  }
  Serial.println("Opening file...");
  String FileName;
  FileName = date + ".dat";

  File dataFile = SD.open(FileName, FILE_WRITE);

  if (dataFile) {
    dataFile.print(temp);
    dataFile.print(",");
    dataFile.print(hum);
    dataFile.print(",");
    dataFile.println(seconds_passed);
    dataFile.close();

    Serial.println("Written data to file and closed it");
    Serial.println(" ");
    Serial.print("Seconds passed: ");
    Serial.println(seconds_passed);
    Serial.println(" ");
  }
  else {
    Serial.println("Error with opening the file, trying again"); 
  }


 delay(1000);
}

It works like a charm, but for the fact that it stops running after an hour, maybe 1,5 hours.

Resetting the board seems to work, but I can't obviously sit there the whole time and reset the board. I use an Arduino Due. Did I program something wrong that something overflows or should I add some more code?

  • 3
    date = String(dayy) + String(monthh) + String(yearr);... ughhh... That poor heap... – Majenko Oct 15 '18 at 21:41
  • Does the sketch really create a new file every second? – Mikael Patel Oct 15 '18 at 21:48
  • It only creates it when it doesn't exist yet, otherwise it opens it. Majenko, what do you mean? – imRobert7 Oct 15 '18 at 22:17
  • 2
    @imRobert7 I mean read this: majenko.co.uk/blog/evils-arduino-strings – Majenko Oct 15 '18 at 22:57
  • 1
    why do you call SD.begin in loop()? it should be in setup() – Juraj Oct 16 '18 at 5:29
-1

A year ago I had the same problem, it seems there is a bug with the String library. I recomed you this:

1- Easy: use the String library on development, but for the "release" change everything to use C native strings (char name [SIZE])

2- Complex (and worse in my op.): set an interrupt every half an hour (or whenever with margin before it crashes) that activates an output connected to the rest pin of the Arduino. I've seen this on the internet but couldn't make it work.

Link to Arduino Forum's thread

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.