0

Datalogger with DHT22 and LCD. help needed
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Datalogger with DHT22 and LCD. help needed

by arjanv78 on Tue Jan 10, 2017 4:31 pm

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:

Code: Select all | TOGGLE FULL SIZE
// 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();
}
Last edited by adafruit_support_mike on Thu Jan 12, 2017 5:03 am, edited 1 time in total.
Reason: added CODE tags to preserve formatting

arjanv78
 
Posts: 2
Joined: Tue Jan 10, 2017 4:20 pm

Re: Datalogger with DHT22 and LCD. help needed

by 2112 on Fri Jan 13, 2017 9:15 am

I don't know if you are still in need of help but I'll try:
The only timing you have is this line
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));

You need two timers to perform two events at different time intervals. Rather than using a delay() reference the BlinkWithout Delay example in the IDE [File>>Examples>>Digital>>BlinkWithoutDelay] to understand this concept. Have the LCD timer fire say every 2 seconds to update your LCD at a 2 second interval and the LOG timer fire less often, 5 or 10 minutes. You can use the same data read in LCD to write to log or get fresh data.

Code: Select all | TOGGLE FULL SIZE
// **Your LCD timer may look like**
if(currentLCDMillis - previousLCDMillis >= LCDinterval)
   {
    // save the last time you updated the LCD
    previousLCDMillis = currentLCDMillis;   
    // read sensor data
   // LCD.print lines here
   // maybe ECHO Serial.print lines here if desired
   }

// **Your LOG timer may look like**
if(currentLOGMillis - previousLOGMillis >= LOGinterval)
   {
    // save the last time you wrote to log file
    previousLOGMillis = currentLOGMillis;   
    // read fresh sensor data or use data read during LCD event. It's as old as the last LCD timer event
   // logfile.print lines here
  // flush to file
  logfile.flush();
   }

The millis() function will overflow in about 50 days so don't run this continuously more than 40 days or so without pressing the reset button.

I hope this helps

2112
 
Posts: 84
Joined: Tue Dec 13, 2016 11:02 am

Re: Datalogger with DHT22 and LCD. help needed

by arjanv78 on Sat Jan 14, 2017 3:55 am

Thanks, i'm trying to use this code in my script......don't know what to place where, but i'm trying.

arjanv78
 
Posts: 2
Joined: Tue Jan 10, 2017 4:20 pm

Re: Datalogger with DHT22 and LCD. help needed

by 2112 on Sat Jan 14, 2017 6:30 am

Give it a go, post what you have and we'll work you through it.

2112
 
Posts: 84
Joined: Tue Dec 13, 2016 11:02 am

Please be positive and constructive with your questions and comments.