Saving all data from DHT Temp/Humidity Sensor

Hi everyone,

I am very new to using Arduino and have managed (after some effort) to put together my first useable sketch for automating some LED lights for my bonsai.

I have am using a DHT 11 sensor for temp and humidity, TEMT light sensor and trying to log it all the data to an SD card and display on LCD.

I have managed to do pretty much all of this, however I can only log a single piece of information from the DHT sensor onto the SD card. I would like to log all of this information and the info from the TEMT Light sensor as well.

I have tried trying to create separate folders on the SD card for each piece of info, but that didn't work rather seemed to corrupt the SD card. What I want to do is have the info appear comma delimited so I can export to excel and get some graphs going.

Could anyone point me in the right direction? This is what i have so far - I am sure its hilariously inefficient and poorly written, but as I say this is probably my first real sketch that actually works.

Thanks to anyone who take the time to read and reply.

:slight_smile:

#include <SPI.h> 
#include <SD.h> 

#include "DHT.h"
#define DHTPIN 2     Pin 2
#define DHTTYPE DHT11   
DHT dht(DHTPIN, DHTTYPE); 

#include <Wire.h>  

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  

int lightSensor = 0; 
int lightValue = 0;  
int LEDS= 3;                   
const int chipSelect = 10; 

unsigned long second= 1000;
unsigned long minute= 60000;
unsigned long previousMillis=0; 
the loop


void setup()
     

{
  pinMode(10, OUTPUT);
  pinMode (LEDS, OUTPUT);      
  Serial.begin(9600);          
  lcd.begin(16,2);          
  lcd.setCursor(0,0);
  

  lcd.print("Hello, world!");   
  delay(1000);
  lcd.clear();

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

  }
 delay (1000);
 lcd.clear(); 
 
lcd.setCursor(0,0); //Start at character 4 on line 0
  lcd.print("Initializing SD card..."); 
 SD.remove("datalog.txt");

 delay (1000);

lcd.clear(); 


}

void loop()

{             
   // Following code initialise sensor and checks it it working    
  float h = dht.readHumidity();  // Read temperature as Celsius
  float t = dht.readTemperature();  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);   
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) 
  
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
    lcd.setCursor(0,0);     
  lcd.print("Failed to read from DHT sensor!");
  }


//The print on LCD code
  lcd.setCursor(0,0); 
  lcd.print("H");
  lcd.setCursor(2,0);
  lcd.print(h);
  lcd.setCursor(7,0);
  lcd.print("%");
  
  
  lcd.setCursor(0,1); 
  lcd.print("T");
  lcd.setCursor(2,1);
  lcd.print(t); // prints temperature in Celcius
  lcd.setCursor(7,1); 
  lcd.print("c");
  lcd.setCursor(9,1);
  lcd.print(f);  // prints temperature in Faherenheit
  lcd.setCursor(14,1);
  lcd.print("f");    
  
  
   
  
  // Following code create minute delay between reading light sensor and recording data
  lightValue = analogRead(lightSensor);  
  unsigned long currentMillis = millis(); 
        if ((unsigned long)(currentMillis - previousMillis) >= 60000)  
      
            {    
                
                lcd.setCursor(9,0);
                lcd.print("L     "); 
                lcd.setCursor(11,0); 
                lcd.print(lightValue); 



                
                              File dataFile = SD.open("datalog.txt", FILE_WRITE); // HERE I NEED TO WRITE ALL THE DATA RECEIVED FROM THE SENSORS I CAN ONLY WRITE TEMP at the moment 't'
                               if (dataFile) {
                               dataFile.println(t);
                                dataFile.close();
            
                                              }
                              // if the file isn't open, pop up an error:
                              else       {
                               lcd.setCursor(11,0); 
                                lcd.print("FILE ERROR"); 
                                      }
                                           
                              
                  previousMillis = currentMillis;                             
                  if (lightValue >= 20)
                        {digitalWrite (LEDS,HIGH);}
                    else  
                        {digitalWrite (LEDS,LOW);}
            }
  


    
    

  
 
}
File dataFile = SD.open("datalog.txt", FILE_WRITE); // HERE I NEED TO WRITE ALL THE DATA RECEIVED FROM THE SENSORS
if (dataFile) {
  dataFile.println(t);
  dataFile.print(',');
  dataFile.print(h);
  dataFile.print(',');
  dataFile.print(lightValue);
  dataFile.println();
  dataFile.close();
}

The code is not too bad. What is bad is the comments. The comments here seem to be all wrong:

 float h = dht.readHumidity();  // Read temperature as Celsius
  float t = dht.readTemperature();  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);

When you have a comment after a line of code (the // notation) that must refer to the line that it's on, not the line following.

Hi

What I think you want to do is possibly exactly what I have done in my Arduino web server application. The application is at http:///www.2wg.co.nz and you can browse maybe 90% of the functionality. If you have a look within the BACKUPS directory/folder on the SD Card you will find hourly backups of my application's data - it includes temperature and humidity data from DHT11 and DHT22 sensors.

The full source code for my application can be downloaded from the PUBLIC directory of the application's SD card.

Download the Overview.pdf file and have a look at section 4.15. Is that what you are interested in?

I use date (year, month, day) and time (hour and minute) for directory and filesnames for backup and log files. Everyday or every month the files are written into new directories. "I" never create the new directories on the SD Card - the application does it using the SD.mkdir() procedure.

Because my application web server can also facilitate file downloads (it also does uploads) I can download backup and log files from the system's SD Card to my local PC - that includes analysis of data in Excel.

Have a look at the MemoryBackup() procedure in my application. That is what writes out the system's DHT11 and DHT22 data to date and time based backup SD card files. The data is tab delimited so it can easily be imported into Excel for analysis.

Obviously to generate file and directory names based on year, month, day, hour and minute values you need a clock implementation and functionality to extract each to the date components for re-use in building your date and time directory and filenames.

Hope this helps.

Cheers

Catweazle NZ

Hi

I have copied the most recent system tab delimited backup file to the public directory so you have a current working example to look at. Browse to the file or use this URL to access it:

http://www.2wg.co.nz/PUBLIC/03291300.TXT/

The data at the top of the file with lines starting with 60MN is hourly DHT11/22 data for the previous 24 hours.

The data near the top of the file with lines starting with DAY are daily DHT11/22 minimum and maximum temperature and humidity readings.

Most recent data is at the end of each line. NULL occurs where the system was powered off and no backup was written at the top of the hour. (I was working on the system at this time yesterday.)

Cheers

Catweazle NZ.