#include <DHT.h>                  // Code that knows how to run the DHT22 thing.
#include <LiquidCrystal_I2C.h>    // Code the knows how to run the LCD display.
#include <timeObj.h>              // Code that gives you a simple egg timer.
#include <blinker.h>              // Code that automatically give you a "blinking" pin. (LED)

#define DHTPIN 2        // The Pin number the DHT22 will be connected to.
#define DHTTYPE DHT22   // We need to tell it what type its going to be as well.. DHT 22

// Create all the exiting global things we'll need for this.
DHT               dht(DHTPIN, DHTTYPE);     // A DHT22 code object to control the thing.
timeObj           DHTTimer(100);            // A timeObj to slow down reads without blocking.
float             temperature;              // A variable for the temp readings.
float             humidity;                 // A variable for the heumidityy readings.
float             lastTemp;                 // We want to remember what we already showed you.
float             lastHumid;                // And that would be without blinking? So, these two.
LiquidCrystal_I2C lcd(0x27,16,2);           // A 16 char, 2 line I2C display address = 0x27.
char              blankStr[] = {"       "}; // Handy blank line for erasing.. Stuff.
blinker           heartBeat;                // A blinking LED (#13) to show its always running.

// Do the initial setup stuff.
void setup() {
  
  lastTemp = 1000;          // Wakky flag value.
  lastHumid = 1000;         // Me too.
  lcd.init();               // initialize the lcd
  lcd.backlight();          // Do the backlight thing.
  dht.begin();              // Fire up the DHT thing.
  heartBeat.setOnOff(true); // Fire up the heartbeat blinker.
}

// Take care of reading the sensor and passing back true for success and false for.. Issues.
bool readDHT(void) {

  temperature   = dht.readTemperature();        // Ask the DHT22 code object to read the temp.
  humidity  = dht.readHumidity();               // Ask the DHT22 code object to read the Humidity.
  if (isnan(temperature)||isnan(humidity)) {    // If we got back any bad values..
    return false;                               // We return false, we failed.
  }                                             //
  return true;                                  // Let 'em know we have new values!
}


// Blank out this value on the display.
void blankValue(int lineNum) {
  
  lcd.setCursor(9,lineNum);
  lcd.print(blankStr);
}


// Show either success, the values, or fail, error messages, on the display
void showValues(bool success) {

  if (lastTemp!=temperature
    ||lastHumid!=humidity) {      // If either value has changed..
    if (success) {                // If we have fresh new readings..
      blankValue(0);              // Blank out the first line.
      lcd.setCursor(0,0);         // Set to start of first line.
      if (temperature<0) {        // If the temp is negitive..
        lcd.print(" Temp :");     // Leave a little extra for the minus sign.
      } else {                    // Else its positive..
        lcd.print(" Temp : ");    // Leave a space where the minus sign would have been.
      }                           //
      lcd.print(temperature,2);   // Print out the temp. 2 decimal places.
      lcd.print(" C");            // Show the units.
      blankValue(1);              // Blank out second line.
      lcd.setCursor(0,1);         // Move to the start of the second line.
      lcd.print("Humid : ");      // Print the humidity label.
      lcd.print(humidity,2);      // And the humidity value.
      lcd.print(" %");            // Show the uinits.
    } else {                      // Else the llast reading failed..
      blankValue(0);              // Blank out the first line.
      lcd.setCursor(0,0);         // Set to start of the first line.
      lcd.print(" Temp : ---");   // Print out temp error.
      blankValue(1);              // Blank out the next line.
      lcd.setCursor(0,1);         // Set to start of the next line.
      lcd.print("Humid : ---");   // Print out humidity error.
    }                             //
    lastTemp = temperature;       // Save off the "seen" value.
    lastHumid = humidity;         // This one too.
  }
}


// And here we loop forever.
void loop() {

  idle();                   // Run the behind the scenes magic. (blinker)
  if (DHTTimer.ding()) {    // If our timer expires..
    showValues(readDHT());  // Display the results.
    DHTTimer.start();       // restart the timer.
  }
}
Click the DHT22
to change its values.