Home‎ > ‎Home Alarm System project‎ > ‎

Temperature and Humidity using the DHT22 sensor



This tutorial you will build one or more digital temperature gauges linked to your  Raspberry Pi that can be viewed on the WWW or your smart phone. 

At the end of the project you will have a WWW dashboard that looks similar to this:


 Multiple Sensors


 Display a temperature history graph per sensor




What you need:
  • Raspberry Pi Model A or B
  • the following parts are available through the PrivateEyePi Store
    • DHT22 sensor(s) (TS02) , one per temperature reading you want on the dashboard
    • 10k resistor (one per sensor) 
    • Jumper wire cables and breadboard or wire, soldering iron and solder (JW01, BB01)
 
For support, queries or suggestions please email support@privateeyepi.com or leave comments in our blog.
 
 
Step 1: Build and test the Electronics:

Parts can be purchased from our store: http://www.privateeyepi.com/store
  • Purchase a DHT22 digital thermometer and humidity sensor
  • Purchase a 4.7k (ohm) resistor
  • As per Figure 1 put the resistor between pins 1 and 2 as in the diagram
  • Connect Pin 1 of the sensor to 5V GPIO (Pin 2)
  • Connect Pin 2 of the sensor to and free GPIO pin e.g. GPIO4 (Pin 7)
  • Ignore pin 3
  • Connect Pin 4 of the temperature gauge to ground (Pin 6)
  
 
 
Figure 1. DHT22 temperature and humidity digital sensor circuit diagram


Figure 2. Picture of circuit from Figure 1 on a bread board

Multiple sensors
If you want to add multiple sensors unfortunately you cannot daisy chain them to the same GPIO (as you can with the DS18B20). You are restricted by the amount of free GPIO pins available on the Raspberry Pi. However you can connect multiple sensors each with their own GPIO pin connection to the Raspberry Pi. Another option for unlimited multiple temperature sensors is to go wireless.


Configure the dashboard 

Step 1: Register user and password
 
If you have not yet registered a user and password with PrivateEyePi follow the following steps, otherwise proceed to Step 3. Note this is a completely free service and has no trial period.
 
  • Go to www.privateeyepi.com
  • Click on "New User" option top right of the screen.
  • Enter your the details on the screen and click update.
 
Step 2: Configure GPIO 4
 
This configuration tells the system that you have connected a temperature gauge to GPIO 4 and to display it on your dashboard.
  • As per Figure 3 click on the GPIO menu option 
  • Click "Add"
  • Type 7 in the "number" field
  • Give it a Description of "GPIO 4", or anything else you like
 

Figure 3. PrivateEyePi setting for a new sensor

 

  • Click "Update"
  • As per Figure 4 click the "Location" menu item 
  • Enter a Description, e.g. "Temperature Gauge"
  • Select GPIO Port 4
  • Select Type "Temperature"
  • Click "Update"
 

Figure 4. PrivateEyePi location settings
 

If you have multiple sensors then repeat this step and add other GPIO and Locations settings for each sensor.

Step 3: Download the Python program that will poll the sensor(s) - dht22.py

This Python program will run on your RaspberryPi and retrieve the temperature settings for 1 sensor and send it to the dashboard. If you have multiple DHT22 sensors then you need to run multiple instances of DHT22 (ie rename the file to another filename).

Follow the following link to install the PrivateEyePi software:


As per Figure 7 at the command prompt: pi@raspberrypi / $

Type

  cd /home
  sudo apt-get update
  sudo apt-get install git
  sudo git clone https://github.com/adafruit/Adafruit_Python_DHT.git 
  cd Adafruit_Python_DHT
  sudo apt-get install build-essential python-dev  
  sudo python setup.py install 

Step 4: Register user and password
 
If you have not yet registered a user and password with PrivateEyePi follow the following steps, otherwise proceed to Step 3. Note this is a completely free service and has no trial period.
 
  • Go to www.privateeyepi.com
  • Click on "New User" option top right of the screen.
  • Enter your the details on the screen and click update.
Step 5: Configure the program

  • Follow the following link to enter your username and password in the globals.py file
  • We have assumed you installed the PrivateEyePi software in the /home directory otherwise you should replace /home in the following steps with your directory.
  • Edit the globals.py file again:
  cd /home
  sudo nano globals.py

Set the PrintToScreen setting to "True". This will print all output to the screen while you are testing your setup. You can switch this off later once everything is working.

Set the Farenheit variable to True if you want your temperature display in Fahrenheit on your dashboard. As per bottom of Figure 9 update the dht22_gpio and dht22_pin_no settings with the GPIO number and it's associated Raspberry Pi pin number (they are not the same!) to which your sensor is connected. In this tutorial it is GPIO 4 (or pin 7 on the Raspberry Pi).

Figure 9 - Set email settings, Fahrenheit option and DHT22 GPIO & Pin Number


Press CTRL-X
Then press Y to save the file
Press ENTER
 
Step 4: Run dht22.py program
 
 
At the command prompt type:

  cd /home
  sudo python dht22.py
 
Step 5: View your dashboard
 
  • Go to www.privateeyepi.com
  • Log in, or click on the "Dashboard" link on top left
  • You should now see the temperature and humidity (red circle in Figure 10) displayed


Figure 10 - Temperature and humidity displayed on the PrivateEyePi dashboard