The objective of this tutorial is to develop a program interacting with DHT22 Sensor.

Prerequisites

  • Components < 70 EUR:
Component Site Price
Raspberry PI 3 Farnell 37.69 EUR
Power supply Micro USB 5V 2500mA Amazon 8.99 EUR
Micro SD Card (16 Go class 10) Amazon 9.99 EUR
DHT22 Sensor ebay 5.60 EUR
Breadboard ebay 3.30 EUR
10 x Cables male/female ebay 1.00 EUR
10 x Cables male/male ebay 1.00 EUR
10 x Resistors 10k ebay 1.10 EUR
Total:   68.67 EUR

Note: This is an example as a guide. You can buy all components in others sites and maybe with better prices.

Electronic wiring

DHT22 Overview
DHT22 Overview

Install Adafruit

Adafruit is a Python library to read the DHT series of humidity and temperature sensors on a Raspberry Pi.

Download the latest version

wget https://github.com/adafruit/Adafruit_Python_DHT/archive/master.zip

Unzip the package

unzip master.zip

Installation

cd Adafruit_Python_DHT-master/
sudo python setup.py install

See more here

or with a virtual env

virtualenv -p /usr/bin/python2.7 ~/workspace/venv2.7/
source ~/workspace/venv2.7/bin/activate
pip install adafruit_python_dht

See how to create a virtual environment here

Get Series of data

Get Series of Temperature and Humdity from DHT22

Create new script

touch getDHT22Series.py

Edit and add this code below

vim getDHT22Series.py
#!/usr/bin/python
import Adafruit_DHT

def getData_func():
    humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4)
    if humidity is not None and temperature is not None:
        print('Temperature={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
        return  { '@type':'DHT22', 'temperature': temperature, 'humidity': humidity }
    else:
        print('Failed to get reading. Try again!')
        return

if __name__ == "__main__": getData_func()

See source here

Execute the code

python getDHT22Series.py

Output:

Temperature=23.8*C  Humidity=36.3%