lefteris1993
Published © GPL3+

Temperature and Humidity Logger Per Hour

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

BeginnerFull instructions provided9,677
Temperature and Humidity Logger Per Hour

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Pmod SD
Digilent Pmod SD
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Single Turn Potentiometer- 10k ohms
Single Turn Potentiometer- 10k ohms
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Humidity and temperature fritzing

Code

Humidity and temperature logger

C/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);
  }


}

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

Credits

lefteris1993

lefteris1993

0 projects • 5 followers

Comments