Raspberry Pi DHT22 & DHT11 Sensor

It's amazing how expensive it can appear to be to buy a digital temperature sensor. You can put one together yourself for very little using things you might already have.

Raspberry Pi DHT22 & DHT11 Sensor

Setup

Assemble some bits:

  • Pi Zero W or really any flavour of Pi you have around.
  • SD Card
  • DHT22 Sensor and some cables. I bought this one.
  • Or a DHT11 sensor like this one. DHT11 has slightly less resolution and accuracy, but is cheaper. Wiring is the same.

I suggest you get one which comes prepackaged on a small PCB with the headers. Generally these come with the appropriate resistor already soldered on, and some wires to hook up and get you started quicker.

Grab the latest Raspbian from the RPi Foundation, or use a noobs image to
get your Pi Zero installed. Remember to add a empty file called ssh to
your /boot partition to enable SSH Server, and fill in a wpa_supplicant.conf
if you need to setup your wifi:

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="WIFI_SSID"
scan_ssid=1
psk="WIFI_PASSWORD"
key_mgmt=WPA-PSK
}

More details here.

Now your pi should arrive on the network and you will be able to SSH into it
using ssh pi@raspberrypi. Do the updates etc., and we can get the DHT sensor
setup!

Cheap DHT22 Wifi Sensor

If you have the same sensor listed above, wire up your Pi as follows:

DHT22 Pin 1 -> 3.3V
DHT22 Pin 2 -> GPIO4
DHT22 Pin 3 -> GND

This will give your sensor some power, and allow us to communicate with it.
Next you'll need to get the appropriate libraries installed. Since we're
doing all this in Python, you can run the following commands to get going:

# Get the python tools we need
sudo apt install python3-dev python3-pip

# Ensure we have the latest modules
sudo python3 -m pip install --upgrade pip setuptools wheel

# Install the Adafruit module for talking to DHT sensors, and influxdb to talk to our Influx server:
sudo pip3 install Adafruit_DHT influxdb

Checkout the code from my github page. Here's the key bits:

# Create the device using GPIO 4
dhtDevice = adafruit_dht.DHT11(board.D4)

# Take a reading:
t = dhtDevice.temperature
h = dhtDevice.humidity

If you followed my other guide on how to setup your own InfluxDB server, you
can start pushing information into your database. Here's the important bits of the script to accomplish this, which you can adapt as you need:

from influxdb import InfluxDBClient
# Prep some JSON we will send to influxdb:
json_body = [
        {
            "measurement": "temperature",
            "tags": {
                "device": "kitchen",
                "type": "dht11"
                },
            "fields": {
               "deg_c": float(t),
               "humidity_percent": float(h)
            },
            "time": datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
            }
        ]

# Connect to the influxdb, setting all these settings as appropriate. 
client = InfluxDBClient('influxserver.lan', 8086, 'influx_username', 'influx_password', 'influx_database_name_here')
client.write_points(json_body)

I have this script scheduled on a Cron job to run every few minutes, logging temperature in my kitchen.

Hope this helps you get started!