DHT11/22 Temperature Sensors using the WebIOPi REST API

I have been really impressed with the WebIOPi REST API, I am using it to control my garage door. However WebIOPi doesn't support the DHT11/DHT22 tempreature sensors natively. In this guide I will walk you through the steps to get an endpoint to return temperature and humidity data via the WebIOPi REST API.

Step 1: Install the sensor

I followed these wiring instructions to get my DHT11 sensor plugged into my Raspberry Pi. He basically says to connect the pins like this (left to right):

  1. Power to a 3.3V or 5V pin
  2. Data to a regular GPIO pin
  3. (skip this pin)
  4. Ground to a Ground pin

I also connected a pull-up 10K resistor between the Power and Data wires.

Step 2: Install Adafruit's DHT library

Since WebIOPi doesn't support the DHT11/DHT22 sensors natively, we have to install a driver that can read from it. I was able to get Adafruit's DHT library working pretty easily:

apt-get update
sudo apt-get install build-essential python-dev
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
sudo python setup.py install

Optional: Test your sensor

If you would like to test your sensor, you can do so like this:

sudo python examples/AdafruitDHT.py 11 4
  • Change the first number to 22 if you have the DHT22 sensor.
  • Change the second number to the GPIO pin you have your sensor on.

You should see something like:

Temp=15.0*C  Humidity=45.0%

Step 3: Install WebIOPi using Python 2.7

By default, WebIOPi installs itself using Python 3. Unfortunately Python 3 is not widely adopted and many developers prefer to work in Python 2.7--The driver we will be using for the DHT11 requires Python 2.7.

Installing WebIOPi using Python 2.7 is easy, we just have to remove " python3" from the fourth line setup.sh:

$ wget WebIOPi-x.y.z.tar.gz
$ tar xvzf WebIOPi-x.y.z.tar.gz
$ cd WebIOPi-x.y.z
$ sed -i 's/ python3//' setup.sh
$ sudo ./setup.sh
$ sudo webiopi-passwd
$ sudo service webiopi restart

Now if you go to http://(raspbery.pi.ip):8000/app/gpio-header and login using the user and password you just set, you should see a graphical representation of your Pi's GPIO header.

Step 4: Set up a WebIOPi Macro

To get the WebIOPi REST API to return the temperature and humidity data, we are going to set up a macro:

$ cd ~
$ pico temperature.py

Paste in the following:

#!/usr/bin/env python
import webiopi
import Adafruit_DHT
import json

SENSOR = Adafruit_DHT.DHT11
PIN = 24

@webiopi.macro
def temperature():
  humidity, temperature = Adafruit_DHT.read_retry(SENSOR, PIN)
  if humidity is not None and temperature is not None:
    fahrenheit = 9.0 / 5.0  * temperature + 32
    return json.dumps({
      "temperature": fahrenheit,
      "humidity": humidity
    })
  else:
    return False

Change SENSOR and PIN if necessary. If you want the temperature in Celcius, remove line 13 and change line 15 to: "temperature": temperature

The last step is to add this file to the WebIOPi config:

$ sudo pico /etc/webiopi/config

Find the [SCRIPTS] section and add:

temperature = /home/pi/temperature.py

Then restart webiopi:

sudo service webiopi restart

Testing the endpoint

All thats left now is to test the WebIOPi macro. Here's an example using cURL:

$ curl -X POST -u webiopi:(password) localhost:8000/macros/temperature

You should see something like this:

{"temperature": 60.8, "humidity": 45.0}

There you have it! You can now read temperature and humidity data from your DHT11 or DHT22 sensor using WebIOPi!