SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By arjanv
#192869
Hi,

I'm trying to make a datalogger to capture temperature and humidity with the DHT11/22 sensor, but also display realtime the temperature and humidity on a LCD.

I've did manage to combine multiple examples into one sketch.

But as i'm pretty new i asking for help how to display realtime the temperature and humidity on a LCD, because now it's a refresh as defined by this line right? define LOG_INTERVAL
The output to the csv file is okay to have it logged every 10 minutes or so.

Also want to add a borometric pressure sensor(bmp180 for example)

I hope someone will look at the code and help me out. also you guys see lots of error perhaps ;)

I've paste my script here:

// log DHT sensor readings to SD card
#include "DHT.h"
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"

#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>

Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7

RTC_PCF8523 rtc;

// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL 10000 // mills between entries (reduce to take more/faster data)

#define ECHO_TO_SERIAL 1 // echo data to serial port

#define DHTPIN 2 // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

// select 10 for adafruit shield
const int chipSelect = 10;

// create file to log to
File logfile;

void setup() {
Serial.begin(9600);


// set up the LCD's number of columns and rows:
lcd.begin(16, 2);

lcd.setBacklight(WHITE);

lcd.setCursor(0,0);
lcd.print("DataLogger");
lcd.setCursor(0,1);
lcd.print("arjan versloot");

delay( 3000 );

lcd.clear();
lcd.setCursor(0,0);
lcd.print("Init SD Card...");
lcd.setCursor(0,1);


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

// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);

if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
lcd.print("... Failed ");
// don't do anything more:
return;
}
Serial.println("card initialized.");
lcd.print("... Ready ");

delay( 2000 );

lcd.clear();




// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}

if (! logfile) {
Serial.println("could not create file");
}

Serial.print("Logging to: ");
Serial.println(filename);

// connect to RTC
Wire.begin();
if (! rtc.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}

logfile.println("millis,datetime,rh,temp_c,temp_f,sat_vapor_density,vapor_density");
#if ECHO_TO_SERIAL
Serial.println("millis,datetime,rh,temp_c,temp_f,sat_vapor_density,vapor_density");
#endif //ECHO_TO_SERIAL
dht.begin();
}

void loop() {
DateTime now = rtc.now();

delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));

String dataString = "";

// fetch the time
// now = RTC.now();
// log time
logfile.print(now.unixtime()); // seconds since 1/1/1970
logfile.print(",");
if (now.month() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.month(), DEC);
logfile.print("/");
if (now.day() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.day(), DEC);
logfile.print("/");
logfile.print(now.year(), DEC);
logfile.print(" ");
if (now.hour() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.hour(), DEC);
logfile.print(":");
if (now.minute() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.minute(), DEC);
logfile.print(":");
if (now.second() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.second(), DEC);
#if ECHO_TO_SERIAL
Serial.print(now.unixtime()); // seconds since 1/1/1970
Serial.print(",");
if (now.month() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.month(), DEC);
Serial.print("/");
if (now.day() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.day(), DEC);
Serial.print("/");
Serial.print(now.year(), DEC);
Serial.print(" ");
if (now.hour() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.hour(), DEC);
Serial.print(":");
if (now.minute() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.minute(), DEC);
Serial.print(":");
if (now.second() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.second(), DEC);
#endif //ECHO_TO_SERIAL

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity(); // relative humidity, %
float t_c = dht.readTemperature(); // air temp, degC
float t_f = t_c*9.0/5.0 + 32.0; // air temp, degF
float svd; // saturation vapor density, g/m3
float vd; // vapor density, g/m3

// set the cursor to (0,0):
lcd.setCursor(0, 0);
// print from 0 to 9:

lcd.print("Temp: ");
lcd.print(t_c);
lcd.print("C");
// set the cursor to (16,1):
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print("%");
//delay(200);


// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t_c) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
svd = 5.018 + 0.32321*t_c + 8.1847e-3*pow(t_c,2) + 3.1243e-4*pow(t_c,3);
vd = h/100*svd;
logfile.print(",");
logfile.print(h, 2);
logfile.print(",");
logfile.print(t_c, 2);
logfile.print(",");
logfile.print(t_f, 2);
logfile.print(",");
logfile.print(svd, 4);
logfile.print(",");
logfile.print(vd, 4);

#if ECHO_TO_SERIAL
Serial.print(",");
Serial.print(h, 2);
Serial.print(",");
Serial.print(t_c, 2);
Serial.print(",");
Serial.print(t_f, 2);
Serial.print(",");
Serial.print(svd, 4);
Serial.print(",");
Serial.print(vd, 4);
#endif //ECHO_TO_SERIAL
}

logfile.println();
#if ECHO_TO_SERIAL
Serial.println();
#endif // ECHO_TO_SERIAL

// flush to file
logfile.flush();
}