I wanted to measure temperature and humidity using my Raspberry Pi. I chose a DHT11 sensor because it is robust and cheap. Configuring it is also well documented but there are a number of pitfalls on the way which I would like to focus on.
The DHT11 has 4 pins. The left most is for Vcc or positive (+) pin which is connected to the Raspberry Pi 3.3V pin. The next pin across is the data pin which must be connected to a GPIO pin on the Raspberry Pi. These two pins must be connected using a 4.7K resistor.
The 3rd pin from the left is not used. The rightmost and fourth pin is the ground or negative pin which must be connected to one of the ground pins on the Raspberry Pi.
Step 1: Components Required
1. Raspberry Pi
3. 4.7k resistor
4. Various breadboard wires
Step 2: Connecting Things
Connect the Raspberry Pi and DHT11 as follows:
DHT11 (+ pin) <-> RaspberryPi (3.3V pin)
DHT11 (data pin) <-> RaspberryPi (GPIO pin - I used GPIO22)
DHT11 (3rd pin) No connection
DHT11 (- pin) ------ Raspberry Pi (gnd pin)
Step 3: Installing the Software
From the RaspberryPi command line, do the following: (NOTE, do not omit SUDO)
sudo git clone https://github.com/adafruit/Adafruit_Python_DHT.g...
You should see - Cloning into 'Adafruit_Python_DHT'... remote: Counting objects: 249, done. remote: Total 249 (delta 0), reused 0 (delta 0), pack-reused 249 Receiving objects: 100% (249/249), 77.01 KiB, done. Resolving deltas: 100% (142/142), done.
sudo apt-get update
sudo apt-get install build-essential python-dev python-openssl
You should see - Adafruit_DHT examples ez_setup.py LICENSE README.md setup.py source
You should see -Beaglebone_Black.py common.py __init__.py platform_detect.py Raspberry_Pi_2.py Raspberry_Pi.py Test.py
sudo python setup.py install
(Note, if you bypass this step then you may see an encounter error - Traceback (most recent call last):
File "./AdafruitDHT.py", line 24, in import Adafruit_DHT ImportError: No module named Adafruit_DHT)
sudo ./AdafruitDHT.py 11 22 (11 = DHT11 and 22=GPIO22 which you selected earlier)
You should see Temp=18.0* Humidity=46.0% (i.e. the temperature and humidity for your environment)
Step 4: What to To With the Output
So, as we saw, the output is "Temp=18.0* Humidity=46.0%"
There are a number of ways in which you can harness this outpu, for example using this test php file, test_dht11.php file
Firstly, I relocated the script AdafruitDHT.py to /var/www/.
To test and run the php script, change to /var/www then sudo php test_dht11.php
The output shows two numbers representing temperature and humidity. These numbers can be written to a database, or compared to warning limits and send an alert etc.
//<? php // uncomment the above line - instructables doesn't like the php start command //test_dht11.php
// executes python file to read DHT11 temperature sensor //and extracts the temperature and humidity values $temperature=0; $humidity=0; $my_pos=0; $exec_msg="sudo /var/www/AdafruitDHT.py 11 22 2>&1"; $test = shell_exec($exec_msg); //extracts temperature $my_pos = strpos($test, "Temp=",0); $temperature = substr($test, $my_pos+5, 4); echo "\n ".$temperature; //extracts humidity $my_pos = strpos($test, "Humidity=",$my_pos); $humidity = substr($test, $my_pos+9, 4); echo "\n ".$humidity; ?>