Project tutorial
Temperature and Humidity Logger Per Hour

Temperature and Humidity Logger Per Hour © GPL3+

In this project, we're going to use the DHT11 humidity and temperature sensor to calculate the average temperature and humidity every hour.

  • 1,558 views
  • 0 comments
  • 10 respects

Components and supplies

Apps and online services

About this project

The project is about temperature and humidity logging....

I am a newbie with Arduino so if I have some mistakes help me to fix them.

I have bought it for about 1 month and every a while when I have time I experimenting with it...

I wanted to make something to count how the temperature changes inside my house as I am at work..

It simply works by taking samples every 15 minutes and add them then it divides them by the numper of the saples it got over time.

For example when you start the arduino by pluging it in the socket it takes the reading of the dht and displays it in the first row until the first 15 minutes has come then it displays the first averege temperature and humidity.

At the first row the first 2 digits are the second counting toward the minute. After the seconds the humidity is displayed and besides the temperature. At the second row if the seconds are between 0 and 29 it displays the time it is running else if the seconds are between 30 and 59 it displays the hours it have logged!

Code

Humidity and temperature loggerC/C++
/*  Humidity and Temperature Logger
 * 
 * Logging the humidity and temperature  per 1 hour
 *  while taking samples every 15 minutes and
 *  get the averege hunidity and temperature by dividing
 *  them with the count of samples it got.
 *  
 *  The first reading is from 
 *  the start up and gets refreshed
 *  every 15 minutes.
 * 
 * 
 *  Using the timeLib we count how many hours 
 *  it runs. 
 *  If we add an rtc we can have the real
 *  time of the time the variable logged.
 *  
 * 
 * The sd  pin out conection:
 * SD SCK : PIN 13
 * SD MISO : PIN 12
 * SD MOSI : PIN 11
 * SD CS : PIN 10
 * 
 * 
 * The lcd conection:
 * RS : PIN 9
 * ENABLE : PIN 8
 * LCD D4 : 7
 * LCD D5 : 6
 * LCD D6 : 5
 * LCD D7 : 4
 * 
 * The DHT conection:
 * DHT : PIN 2
 * 
 * 
 * Created 19/2/2019
 * by Lefteris X.
 */
///////////////////////TIME//////////////////////////////////////

#include "TimeLib.h"

unsigned long lastnow=0; // LAST TIME WRITED IN SD CARD

unsigned long lastSample=0; // LAST SAMPLE TAKEN

////////////////////////////////////////////////////////////////
  
///////////////////////////SD CARD/////////////////////////

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

  const int chipSelect = 10;

  // make a string for assembling the data to log:
  String dataString = "";  
  String time = "";
  
  int lastWrite=0; // COUNT THE HOURS LOGGED
  
  float averegeT, averegeH; // STORE THE AVEREGE OF DATA

///////////////////////////////////////////////////////////

///////////////////////DHT/////////////////////////////////

#include "DHT.h"

#define DHTPIN 2 // the digital pin of sensor

#define DHTTYPE DHT11 // define the type of sensor


DHT dht(DHTPIN,DHTTYPE); // initialize DHT sensor

// THE ARAY FOR STORING AND CALCULATING THE AVEREGE
  float TempSample[4]= {0,0,0,0};
  float HumSample[4]= {0,0,0,0};
  
  int counter=1; // COUNT THE SAMPLES

  
////////////////////////////////////////////////////////////

/////////////////////////////LCD////////////////////////////
 #include <LiquidCrystal.h>

 LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
/////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(9600); // SERIAL INITIALIZE

  dht.begin();  // DHT INITIALIZE

  // GIVE THE FIRST VALUES UNTIL THE FIRST AVEREGE CHECK
    averegeT=dht.readTemperature();
    averegeH=dht.readHumidity();
  

  lcd.begin(16,2);  // LCD 16x2 INITIALIZE
  

  setTime(00,00,00,19,2,19); // SET THE TIME AS ZERO TO COUNT THE TIME IT RUNS


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

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    lcd.print("Card Failed");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
  lcd.clear();
  lcd.home();
  lcd.print("card initialized.");
  delay(1000);
  lcd.clear();
  
}

void loop() {
  

  samples();


  loging();

 
  lcd_print();


 

}//loop closing
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void lcd_print()
{
/*
 * a FUCTION TO PRINT EVERY 30 SECONDS THE HUMIDITY 
 * AND TEMPRATURE AND PRINT THE TIME IS RUNNING FOR 
 * 27 SECOND EVERY TIME.
 */
    if (second()==1 || second()==30 )
  {
    lcd.setCursor(03,00);
  lcd.print(averegeH);
  lcd.print(" ");
  lcd.print(averegeT);
  lcd.print("   ");
  }

  lcd.home();
  lcd.setCursor(00,00);
  lcd.print(second());
  if (second()<=9)
  {
    lcd.setCursor(01,00);
    lcd.print("  ");
  }

  while (second()>0 && second()<29)
  {
    
    lcd.setCursor(00,01);
    lcd.print("Time : ");
    lcd.print(hour());
    lcd.print(":");
    lcd.print(minute());
    lcd.print(":");
    lcd.print(second());
    lcd.print("    ");
    break;
  }
  while (second()<59 && second()>29)
  {
    
    lcd.setCursor(00,01);
    lcd.print("Loged Hours ");
    lcd.print(lastWrite);
    lcd.print("   ");
  
  }
 
 
}




void samples()
{
/*
 * THE FUCTION THAT CALCULATES AND PRINTS BY SERIAL 
 * THE VARIABLE OF THE ARAYS AND THE AVEREGE 
 * EVERY 15 MINUTES
 * 
 */
  unsigned long curentmills=millis();
  if  (curentmills-lastSample>=900000)
  {
    lastSample=curentmills;
    float t=dht.readTemperature();
    float h = dht.readHumidity(); 

    switch (counter)
    {
      case 1:
      {
        
        TempSample[0]=t;
        HumSample[0]=h;
        break;
      }
      case 2:
      {
        
        TempSample[1]=t;
        HumSample[1]=h;
        break;
      }
      case 3:
      {
        
        TempSample[2]=t;
        HumSample[2]=h;
        break;
      }
      case 4:
      {
        
        TempSample[3]=t;
        HumSample[3]=h;
        break;
      }
    }
    
    Serial.println("===================================");
    
    for (int i=0; i<4; i++)
    {
    Serial.print("TempSample[");
    Serial.print(i);
    Serial.print("] = ");
    Serial.println(TempSample[i]);

    
    }
    
    Serial.println("===================================");

    for (int j=0; j<4; j++)
    {
      
    Serial.print("HumSample[");
    Serial.print(j);
    Serial.print("] = ");
    Serial.println(HumSample[j]);
    
    }
    
    Serial.println("===================================");


     float HumSampleMo= ( HumSample[0] +  HumSample[1] + HumSample[2] + HumSample[3]) /counter;
    Serial.print("HumSampleMo = ");
    Serial.println(HumSampleMo);
    
     float TempSampleMo= ( TempSample[0] +  TempSample[1] + TempSample[2] + TempSample[3]) /counter;
    Serial.print("TempSampleMo = ");
    Serial.println(TempSampleMo);
    
    Serial.println("===================================");
    
    averegeT = TempSampleMo;
    
    averegeH = HumSampleMo;
    
      

    counter++;
    if (counter>4)
    {
      counter=1;
      for (int i=0; i<4; i++)
      {

        
      TempSample[i] = 0;
      
      HumSample[i] = 0;
        
      }
    
    }
  }




  
}






void loging() 
{
  /*
   * THE FUCTION THAT STORING TO THE SD CARD THE 
   * AVEREGE OF HUMIDITY AND TEMPERATURE EVERY 1 HOUR
   * STARTS FROM 00:59:00
   */
 float h = dht.readHumidity(); // save humidity to memory

 float t = dht.readTemperature(); // save temperature in C to memory
 
 unsigned long curentnow=millis();

  
  if ( isnan(h) || isnan(t))
  {
    Serial.println("Failed to read from DHT sensor");
    lcd.clear();
    lcd.print("DHT MISING");
    
  }

 // if (second()==1)
  //if (minute()==59 && second()==0)
   if(curentnow-lastnow>=3600000)
  {
  lastnow=curentnow;


  dataString = " ";
  time = " ";
  
dataString += "Temperature Averege = ";
dataString += String(averegeT);
dataString += "\t";
dataString += "Humidity Averege = ";
dataString += String(averegeH);
dataString += "\n";
dataString += "Hours logged : ";
dataString += String(lastWrite);

time +="Time = ";
time +=String(hour());
time +=":";
time +=String(minute());
time +=":";
time +=String(second());
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("templog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(time);
    dataFile.println(dataString);
    dataFile.flush();
    dataFile.close();
    // print to the serial port too:
    Serial.println(time);
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
  lastWrite++;
  Serial.println(lastWrite);
  }


}

///////////////////////////////////////////////////////////////////////////////////////////////////////////

Schematics

Humidity and temperature fritzing
humidity_temperature_3JCJr6PVCN.fzz

Comments

Similar projects you might like

Temperature and Humidity Data Logger

Project tutorial by Wimpie van den Berg

  • 25,026 views
  • 2 comments
  • 25 respects

Temperature and Humidity Logger (Using Arduino)

Project showcase by lmsousa

  • 8,321 views
  • 7 comments
  • 35 respects

DHT11 Humidity + Temperature Sensor with 16x2 LCD display

Project showcase by onatto22

  • 10,015 views
  • 2 comments
  • 20 respects

Temperature + Humidity on LCD

Project showcase by interpeo

  • 20,710 views
  • 11 comments
  • 49 respects

Arduino Temperature - Humidity - Rain Sensor

Project showcase by Rick_Findus

  • 10,390 views
  • 6 comments
  • 17 respects

Temperature and Humidity Data logger - Breadboard

Project tutorial by Jed Hodson

  • 7,857 views
  • 2 comments
  • 10 respects
Add projectSign up / Login