Part 2. DHT22 Temperature and Humidity Sensor - initialstate/grovepi GitHub Wiki

The DHT22 is a high-precision digital humidity and temperature sensor. If you are going to capture temperature and humidity with a Grove sensor, this is the one to get because it is accurate to a few decimal places unlike its wimpy cousin, the DHT11, which has rather pathetic precision (+/- 1° Celsius). The DHT22 is the white sensor and the DHT11 is the blue one.
-
Plug your DHT22 into D4 on the GrovePi+ (you can plug it into any of the 'D' ports, but you have to match the script settings with where you plugged it into).
-
Power on your Pi and navigate to the directory of choice. When I created this tutorial, I created a grovepi directory
$ cd ~/
$ mkdir grovepi
$ cd grovepi
- Copy the GrovePi DHT Python script at https://github.com/InitialState/grovepi/blob/master/grovepi-dht.py to a new file.
$ nano dht.py
copy/paste the contents of https://github.com/InitialState/grovepi/blob/master/grovepi-dht.py into nano
- Modify the user settings at the top of this file accordingly
# --------- User Settings ---------
# The DHT_SENSOR_TYPE below may need to be changed depending on which DHT sensor you have:
# 0 - DHT11 - blue one - comes with the GrovePi+ Starter Kit
# 1 - DHT22 - white one, aka DHT Pro or AM2302
# 2 - DHT21 - black one, aka AM2301
DHT_SENSOR_TYPE = 1
# Connect the DHT sensor to one of the digital pins (i.e. 2, 3, 4, 7, or 8)
DHT_SENSOR_PIN = 4
# Initial State settings
BUCKET_NAME = ":partly_sunny: Indoor Weather"
BUCKET_KEY = "dht012715"
ACCESS_KEY = "PLACE YOUR INITIAL STATE ACCESS KEY HERE"
# Set the time between sensor reads
MINUTES_BETWEEN_READS = 1
CONVERT_TO_FAHRENHEIT = True
# ---------------------------------
Specifically, you will need to place your Initial State access key (found in your Initial State account settings) in the ACCESS_KEY variable assignment. If you do not do this, your data will not be streamed into your Initial State account, making you very sad and frustrated.
Note: You should copy/paste your Initial State ACCESS_KEY if at all possible to avoid any mistakes. Your ACCESS_KEY is case sensitive.
There is a MINUTES_BETWEEN_READS variable that you can modify if you want to take data more frequently (e.g. set this to .1 to take data every 6 seconds).
- Save and exit
CTRL+X
Y
- Run this script
$ sudo python dht.py
Note: If you get an error such as "ImportError:cannot import name grovepi", you may need to run your GrovePi scripts from within the GrovePi python directory. Try the following:
$ cd /home/pi/Desktop/GrovePi/Software/Python
$ cp ~/grovepi/dht.py .
$ nano dht.py
Modify line 1 from "from grovepi import grovepi" to "import grovepi"
$ sudo python dht.py
- Go to your Initial State account and look for a new bucket called "⛅️ Indoor Weather" (you may have to refresh the browser page to see the new bucket). Click on this bucket then click on the Tiles icon.

As you can see in the screenshot above, I ran my DHT22 for almost 5 hours. At 4:09 PM, the DHT22 decided to give me a bogus reading of 30.2° F and -1% humidity. To learn more about the many things you can do with Tiles, check out http://support.initialstate.com/knowledgebase/topics/102613-tiles.
Luckily, I can filter out this value in Waves (you can't filter this out in Tiles ... yet). Click on the Waves icon at the top to go into the Waves view. Here, you can manipulate the data a bit more. To get rid of the bogus temperature value, I first highlighted the Temperature(F) stream and applied an expression: =filterout(<50) and hit enter. For the humidity stream, I used the expression: =filterout(<0). This is the end result (much better):

To learn more about the different things you can do with Waves, check out the Waves support page.
<< Part 2: GrovePi Setup - Part 2: DHT11 Temperature and Humidity Sensor >>