Arduino Asked by imRobert7 on October 22, 2020
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?
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 reset pin of the Arduino. I've seen this on the internet but couldn't make it work.
Answered by Sanjo on October 22, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP