Category: ARDUINO
Hits: 3364

I like to make a revision on Temperature and Humidity Monitor. After a few test, I found few flaw in the article.

First of all, the diagram. For the SD reader part, I not connect any connection to the second GND pin. Whenever, you try to power up to Arduino and monitor the output in serial terminal, arduino can not detect any SD card.

So, the correct connection is to connect all GND pins to GND.

Secondly, the output in CSV file. There is no separation between temperature and humidity reading. So, below is the correct one. I put comma between them.

I got a new project to monitor temperature and humidity 24/7. So, I set logging delay to 5 minutes, where 5 * 60 * 1000 = 300000. (1sec = 1000). At this part, i put the delay value straight to the function delay() and not the integer variable as before.

#include <SD.h>

#include "RTClib.h"

#include <Wire.h>

#include <string.h>

#include <dht11.h>

#include <stdlib.h>


RTC_DS1307 RTC;


dht11 DHT11;


#define DHT11PIN 2

File myFile;


void setup()

{

Serial.begin(9600);

Wire.begin(); //Important for RTClib.h

RTC.begin();

if (! RTC.isrunning()) {

Serial.println("RTC is NOT running!");

// following line sets the RTC to the date & time this sketch was compiled

// RTC.adjust(DateTime(__DATE__, __TIME__));

return;

}

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

// On the Ethernet Shield, CS is pin 4. It's set as an output by default.

// Note that even if it's not used as the CS pin, the hardware SS pin

// (10 on most Arduino boards, 53 on the Mega) must be left as an output

// or the SD library functions will not work.

pinMode(10, OUTPUT);


if (!SD.begin(4)) {

Serial.println("initialization failed!");

return;

}

Serial.println("initialization done.");


}


void loop()

{

openFile();

writeFile();

closeFile();

delay(300000);

}


void openFile(){

char filename[] = "00000000.CSV";


DateTime now = RTC.now();

filename[0] = (now.year()/1000)%10 + '0'; //To get 1st digit from year()

filename[1] = (now.year()/100)%10 + '0'; //To get 2nd digit from year()

filename[2] = (now.year()/10)%10 + '0'; //To get 3rd digit from year()

filename[3] = now.year()%10 + '0'; //To get 4th digit from year()

filename[4] = now.month()/10 + '0'; //To get 1st digit from month()

filename[5] = now.month()%10 + '0'; //To get 2nd digit from month()

filename[6] = now.day()/10 + '0'; //To get 1st digit from day()

filename[7] = now.day()%10 + '0'; //To get 2nd digit from day()

//Check file name exist?

if (SD.exists(filename)) {

Serial.println("exists.");

myFile = SD.open(filename, FILE_WRITE);

}

else {

Serial.println("doesn't exist.");

Serial.println("Creating new file");

Serial.println(filename);

myFile = SD.open(filename, FILE_WRITE);

}

}


void writeFile(){

DateTime now = RTC.now();

myFile.print(now.year(),DEC);

myFile.print("/");

myFile.print(now.month(),DEC);

myFile.print("/");

myFile.print(now.day(),DEC);

myFile.print(" ");

myFile.print(now.hour(),DEC);

myFile.print(":");

myFile.print(now.minute(),DEC);

myFile.print(":");

myFile.print(now.second(),DEC);

myFile.print(",");

DHT11.read(DHT11PIN);

myFile.print((float)DHT11.temperature, 2);

Serial.println((float)DHT11.temperature, 2);

myFile.print(",");

myFile.println((float)DHT11.humidity, 2);

Serial.println((float)DHT11.humidity, 2);


}


void closeFile(){

myFile.close();


}