IoT Temperature & Humidity to Initial State Using a Raspberry Pi & DHT22

Screenshot

Picture of IoT Temperature & Humidity Sensor DHT22 AM2302

IoT Temperature & Humidity to Initial State Using a Raspberry Pi & DHT22

I have a Raspberry Pi 3 running OpenHab located on my desk next to my iMac. Since I spend most of day here, I thought it would be good to get some Temperature & Humidity data reading. Right now I am sending that data to Initial State as they have some really cool dashboards! I also plan to send this data to OpenHab but havent figured that out yet(I will update this post when I do). In this tutorial, I will go over the steps so you can do the same or similar. I haven’t tested but assume a DHT11 would work as well.

*Disclaimer: Most if not all links on this page will be Affiliate Links. This helps pay for things like website hosting and more bits & gadgets to write about.

DHT22/AM2302 Digital Temperature and Humidity Measurement Sensor

These come in a few versions, some bare with four connectors and some mounted to a board with only 2 connection (VCC, DATA & Ground). If you’re using the bare version, you’ll need to use a resistor. It seems like most of the mounted AM2302 versions have the resistor built in. These AM2302 versions are a little pricey on Amazon at $9.99 a piece, but if you’re “Prime Spoiled” and need it in two days, that’s the price you pay! I was able to find a pack of 4 on Ebay for about $20 shipped.

Initial State

Initial State allows you to send data from various devices such as Raspberry Pis and ESP8266s to a beautifully visualized dashboard viewable in a web browser. Their free account is fairly generous and allows 25,000 events per month, but you can only view the last 24 hours of data. Fork over $5/month and you’ll remove those limits. The Pro account at $25/month adds Email/SMS triggers.

To get started with Initial State on my Raspberry Pi 3, I followed their post on Github “Create a System Health Dashboard for your Raspberry Pi”.  If you get an error in Step 2 when installing psutil like I did, you’ll need to run:

$ sudo apt-get install gcc python-dev
$ sudo pip install psutil 

Once you get that up and running, hopefully you won’t find that your Pi has a memory leak like I did!

 

Screenshot from Initial State Raspberry Pi3 System Health

Now they you have Initial State up and running, it’s time to get the DHT22/AM2302 connected to your Raspberry Pi.

The DHT22/2302 has 3 connections: 3V+, Data Out, and Ground

  • Connect 3V+ to Pin 1 on my RPi
  • Connect Data Out to GPIO 4(Pin 7)
  • Connect Ground to Pin 6 GND

Wiring Diagram Frtizing IoT Temperature & Humidity to Initial State Using a Raspberry Pi & DHT22

Now that you have it wired it’s time to install some more code!

I followed the Adafruit install instruction here: https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/software-install-updated

pi@OpenHAB:~ $ pwd
/home/pi
pi@OpenHAB:~ $ git clone https://github.com/adafruit/Adafruit_Python_DHT.git
Cloning into 'Adafruit_Python_DHT'...
remote: Counting objects: 223, done.
remote: Total 223 (delta 0), reused 0 (delta 0), pack-reused 223
Receiving objects: 100% (223/223), 71.72 KiB | 0 bytes/s, done.
Resolving deltas: 100% (125/125), done.
Checking connectivity... done.
pi@OpenHAB:~ $ cd Adafruit_Python_DHT/
pi@OpenHAB:~/Adafruit_Python_DHT $ sudo python setup.py install
running install
running bdist_egg
running egg_info
creating Adafruit_DHT.egg-info
writing Adafruit_DHT.egg-info/PKG-INFO
writing top-level names to Adafruit_DHT.egg-info/top_level.txt
writing dependency_links to Adafruit_DHT.egg-info/dependency_links.txt
writing manifest file 'Adafruit_DHT.egg-info/SOURCES.txt'
reading manifest file 'Adafruit_DHT.egg-info/SOURCES.txt'
writing manifest file 'Adafruit_DHT.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-armv7l/egg
running install_lib
running build_py
creating build
creating build/lib.linux-armv7l-2.7
creating build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/__init__.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/Test.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/Raspberry_Pi.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/Raspberry_Pi_2.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/Beaglebone_Black.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/platform_detect.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/common.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
running build_ext
building 'Adafruit_DHT.Raspberry_Pi_2_Driver' extension
creating build/temp.linux-armv7l-2.7
creating build/temp.linux-armv7l-2.7/source
creating build/temp.linux-armv7l-2.7/source/Raspberry_Pi_2
arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c source/_Raspberry_Pi_2_Driver.c -o build/temp.linux-armv7l-2.7/source/_Raspberry_Pi_2_Driver.o -std=gnu99
arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c source/common_dht_read.c -o build/temp.linux-armv7l-2.7/source/common_dht_read.o -std=gnu99
arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c source/Raspberry_Pi_2/pi_2_dht_read.c -o build/temp.linux-armv7l-2.7/source/Raspberry_Pi_2/pi_2_dht_read.o -std=gnu99
arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c source/Raspberry_Pi_2/pi_2_mmio.c -o build/temp.linux-armv7l-2.7/source/Raspberry_Pi_2/pi_2_mmio.o -std=gnu99
arm-linux-gnueabihf-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-z,relro -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-armv7l-2.7/source/_Raspberry_Pi_2_Driver.o build/temp.linux-armv7l-2.7/source/common_dht_read.o build/temp.linux-armv7l-2.7/source/Raspberry_Pi_2/pi_2_dht_read.o build/temp.linux-armv7l-2.7/source/Raspberry_Pi_2/pi_2_mmio.o -lrt -o build/lib.linux-armv7l-2.7/Adafruit_DHT/Raspberry_Pi_2_Driver.so
creating build/bdist.linux-armv7l
creating build/bdist.linux-armv7l/egg
creating build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/__init__.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/Test.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/Raspberry_Pi_2_Driver.so -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/Raspberry_Pi.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/Raspberry_Pi_2.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/Beaglebone_Black.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/platform_detect.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/common.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/Test.py to Test.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi.py to Raspberry_Pi.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2.py to Raspberry_Pi_2.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/Beaglebone_Black.py to Beaglebone_Black.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/platform_detect.py to platform_detect.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py to common.pyc
creating stub loader for Adafruit_DHT/Raspberry_Pi_2_Driver.so
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2_Driver.py to Raspberry_Pi_2_Driver.pyc
creating build/bdist.linux-armv7l/egg/EGG-INFO
copying Adafruit_DHT.egg-info/PKG-INFO -> build/bdist.linux-armv7l/egg/EGG-INFO
copying Adafruit_DHT.egg-info/SOURCES.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying Adafruit_DHT.egg-info/dependency_links.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying Adafruit_DHT.egg-info/top_level.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
writing build/bdist.linux-armv7l/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/Adafruit_DHT-1.3.0-py2.7-linux-armv7l.egg' and adding 'build/bdist.linux-armv7l/egg' to it
removing 'build/bdist.linux-armv7l/egg' (and everything under it)
Processing Adafruit_DHT-1.3.0-py2.7-linux-armv7l.egg
Copying Adafruit_DHT-1.3.0-py2.7-linux-armv7l.egg to /usr/local/lib/python2.7/dist-packages
Adding Adafruit-DHT 1.3.0 to easy-install.pth file
.
Installed /usr/local/lib/python2.7/dist-packages/Adafruit_DHT-1.3.0-py2.7-linux-armv7l.egg
Processing dependencies for Adafruit-DHT==1.3.0
Finished processing dependencies for Adafruit-DHT==1.3.0
pi@OpenHAB:~/Adafruit_Python_DHT $ ls
Adafruit_DHT Adafruit_DHT.egg-info build dist examples ez_setup.py ez_setup.pyc LICENSE README.md setup.py source
pi@OpenHAB:~/Adafruit_Python_DHT $ cd examples
pi@OpenHAB:~/Adafruit_Python_DHT/examples $ ls -l
total 16
-rwxr-xr-x 1 pi pi 2340 Aug 1 18:36 AdafruitDHT.py
-rwxr-xr-x 1 pi pi 5715 Aug 1 18:36 google_spreadsheet.py
-rw-r--r-- 1 pi pi 2035 Aug 1 18:36 simpletest.py
pi@OpenHAB:~/Adafruit_Python_DHT/examples $ sudo ./AdafruitDHT.py 22 4
Temp=27.8* Humidity=50.7%

Great Success! Don’t worry if you need Fahrenheit we will convert it later.

Sending Temperature and Humidity Readings to Initial State

For this basically took some code from the Grove Pi DHT22 Initial State Git and combines it with the code used from psutil that we did previously to send Raspberry Pi system health to Initial State and came up with the following. (Keep in mind I really don’t know much Python and any improvements/recommendations to the code are welcome!)

import sys
import Adafruit_DHT
import os
import time
from ISStreamer.Streamer import Streamer

# --------- 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 = 22
# 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 = "dht-rpi"
ACCESS_KEY = "ENTER YOUR INITIAL STATE KEY HERE"
# Set the time between sensor reads
MINUTES_BETWEEN_READS = 1
CONVERT_TO_FAHRENHEIT = True
# ---------------------------------

def main():
  streamer = Streamer(bucket_name=BUCKET_NAME, bucket_key=BUCKET_KEY, access_key=ACCESS_KEY)
  while True:
    [hum1,temp_c1] = Adafruit_DHT.read_retry(DHT_SENSOR_TYPE,DHT_SENSOR_PIN)
    #if isFloat(temp_c1):
    temp_c = float(round(temp_c1,2))
    if (CONVERT_TO_FAHRENHEIT):
     temp_f = temp_c * 9.0 / 5.0 + 32.0
     # print("Temperature(F) = ", temp_f)
     streamer.log("Temperature(F)",temp_f)
    else:
     # print("Temperature(C) = ", temp_c)
     streamer.log("Temperature(C)",temp_c)
    #if ((isFloat(hum1)) and (hum1 >= 0)):
    hum = float(round(hum1,1))
    # print("Humidity(%) = ", hum)
    streamer.log(":sweat_drops: Humidity(%)",hum)
    streamer.flush()

    #except IOError:
     #   print ("Error")

    time.sleep(60*MINUTES_BETWEEN_READS)

if __name__ == '__main__':
    main()

 


Create a file and copy the code into it:

nano rpi-temp.py

Run the code

sudo python /home/pi/pihealth/rpi-temp2.py

 


To run the code and not have it cancel if your SSH session terminates:

chmod +x rpi-temp.py
nohup /home/pi/pihealth/rpi-temp2.py &

 


Wait a minute or so and go check your Initial State Dashboard:

Screenshot

I am using a standalone “Comfortmeter” to make some comparisons. Temperature seems to be pretty much the same, but humidity is off. That said my Nest Controller in the next room says 53% humidity, so I think the DHT22/AM2302 is fairly accurate.

Next I’ll figure out to also send this data to OpenHab. I’ll update this post when I do!

Picture of IoT Temperature & Humidity Sensor DHT22 AM2302 comparing to standalone sensor

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 4.00 out of 5)
Loading...


Reminder, some links in the post might be affiliate links, so I’ll make some coffee/beer/gadget money if you buy anything. You get it at the normal price, though!

[geoip-content region=”TX, CA.”] Content just for everyone in Texas and California [/geoip-content]

Scroll to Top