GETTING TEMPERATURE AND HUMIDITY

GETTING TEMPERATURE AND HUMIDITY

Run sudo apt-get update and sudo apt-get upgrade -y to ensure your system is up to date.
The temperature and humidity refers to a time of the day. For the data to be correct, your timezone must be set correctly. When you execute date you should see the local time. If not, then you can set the correct timezone in sudo raspi-config.
To communicate with the sensor, the Adafruit_Python_DHT library is used. It talks to the GPIO pins without using the Wiring Pi library. To install it, get the dependencies with
sudo apt-get install -y build-essential python-dev git and then download and install the library with

mkdir -p /home/pi/sources  
cd /home/pi/sources  
git clone https://github.com/adafruit/Adafruit_Python_DHT.git  
cd Adafruit_Python_DHT  
sudo python setup.py install  

Now you can already get the temperature and humidity with the command
sudo /home/pi/sources/Adafruit_Python_DHT/examples/AdafruitDHT.py 2302 4. The first argument is the sensor type, it can be 11 or 22 or 2302. The second argument is the RPi GPIO pin which is connected to the sensor data pin.Actually the sensor can be queried from any Python script with just two lines of code. You can try this by starting Python interactive with sudo python then in the interactive mode type

import Adafruit_DHT  
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 4)  
temperature  
humidity  

Press Ctrl-D to quit Python interactive mode.