Home » Blog » Monitoring Temperature and Humidity Using Cayenne, Raspberry Pi and DHT11
DHT11 Raspberry Pi Cayenne

Monitoring Temperature and Humidity Using Cayenne, Raspberry Pi and DHT11

In this article, we will acquire the ambient temperature and humidity data using Raspberry Pi and DHT11 sensor. The acquired sensor data will then be pushed to the Cayenne IoT project builder using the Cayenne MQTT Python Library.

DHT11 is a temperature and humidity sensor equipped with humidity measurement, temperature measurement, and communication function. Also, the output value is calibrated and can be used without adjustment. NTC thermistor is used for temperature measurement and polymer film humidity sensor is used for humidity measurement. The sensor measures the relative humidity because the amount of moisture contained in the polymer membrane changes according to the change of the relative humidity and the dielectric constant changes accordingly. The DHT11 performs digital conversion of these sensors with a dedicated ASIC and outputs a 1-wire serial signal. The user can easily measure humidity and temperature simply by reading the output data with a microcomputer like Raspberry Pi. All the circuits necessary for measurement are built in, and it is possible to mount in a small space. Since the data output is open drain output, it can extend up to 20 meters.

Requirements:
  1. Raspberry Pi 2/3 connected to Internet
  2. DHT11 temperature sensor
  3. Cayenne IoT Project Builder

Interfacing DHT11 with Raspberry Pi

dht11 raspberry pi wiring

 

For a four pin DHT11 sensor connect the Vcc of DHT11 and 3.3 V of Pi, GND of DHT11 and GND of Pi, DATA of DHT11 and GPIO 14 (BCM) of Pi. The resistor shown in the above schematic is a 10K Ohm pull up resistor connected between the Vcc and signal line. Basic knowledge of GPIO pins of Raspberry Pi can be found here.

Programming the DHT11 using Adafruit Python library

Since Cayenne doesn’t support the DHT11/DHT22 sensors natively, we have to install a driver that can read from it. We’ll be using the Adafruit DHT11 Python library to read sensor data. To install the Adafruit DHT11 library follow the below steps in order:

  1. Download the library from github page using Clone or download option.
    https://github.com/adafruit/Adafruit_Python_DHT
  2. Extract the contents of the zip file to a directory. In my case its dht11_python
    dht11_python_setup
  3. Open terminal and navigate to the extracted folder using :
    cd dht11_python
  4. Enter:
    sudo apt-get install build-essential python-dev
  5. Install the library with:
    sudo python setup.py install
  6. If you would like to test your sensor, you can do so by using the example codes. Navigate to examples folder and modify the simpletest.py file.
    #!/usr/bin/python
    
    # Copyright (c) 2014 Adafruit Industries
    # Author: Tony DiCola
    
    # Permission is hereby granted, free of charge, to any person obtaining a copy
    # of this software and associated documentation files (the "Software"), to deal
    # in the Software without restriction, including without limitation the rights
    # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    # copies of the Software, and to permit persons to whom the Software is
    # furnished to do so, subject to the following conditions:
    
    # The above copyright notice and this permission notice shall be included in all
    # copies or substantial portions of the Software.
    
    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    # SOFTWARE.
    import Adafruit_DHT
    
    # Sensor should be set to Adafruit_DHT.DHT11,
    # Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
    sensor = Adafruit_DHT.DHT11
    
    # Example using a Raspberry Pi with DHT sensor
    # connected to GPIO23.
    pin = 14
    
    # Try to grab a sensor reading.  Use the read_retry method which will retry up
    # to 15 times to get a sensor reading (waiting 2 seconds between each retry).
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    
    # Note that sometimes you won't get a reading and
    # the results will be null (because Linux can't
    # guarantee the timing of calls to read the sensor).
    # If this happens try again!
    if humidity is not None and temperature is not None:
        print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
    else:
        print('Failed to get reading. Try again!')

    Once the above code is executed, the temperature and humidity data will be displayed on the terminal. Make sure to update the pin number if you are using a different setup.

    dht11 sample code

Setting up Cayenne

Getting your Pi connected to the internet and up and running with Cayenne only takes a few minutes. myDevices has put up a detailed setup document on their GitHub page. You can follow the instruction to setup Cayenne on your raspberry Pi. At the time of writing this article Cayenne is yet provide support for DHT11/DHT22 sensors on Raspberry Pi. So instead, we will be using the Cayenne Bring Your Own Thing API to publish DHT11 sensor data to Cayenne dashboard.

The Cayenne MQTT Python Library provides functions to easily connect various IoT hardware to the Cayenne IoT project builder. With it you can send data to and receive data from Cayenne. After connecting your device you can send data from your device [DHT11] to the Cayenne dashboard and display it using widgets. You may also setup triggers and notification for your device.

Requirements
  1. Python 2.7 or 3.x.
  2. Cayenne MQTT Python Library
  3. Eclipse Paho MQTT Python client library. This is installed as part of the Cayenne library installation.

 

Installation

This library can be installed using pip:

pip install cayenne-mqtt
Cayenne Setup
  1. Create your Cayenne account at mydevices.com.
  2. Add a new device using the Bring Your Own Thing API selection.

Cayenne Bring Your Own Thing API

Below is an modified Adafruit_DHT python code that publishes DHT11 data to Cayenne dashboard. The Cayenne authentication variables must be modified with the authentication info you received when adding a new device in Cayenne.

#!/usr/bin/python

# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import Adafruit_DHT
import cayenne.client
import time

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "a48c34e0-6958-11e7-a041-7d8fa56d7b64"
MQTT_PASSWORD  = "d7e6f1b3cc6748d16983f21a3021a66456131648"
MQTT_CLIENT_ID = "f0d702e0-695c-67e7-a938-f527c54b168b"

client = cayenne.client.CayenneMQTTClient()
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)

# Sensor should be set to Adafruit_DHT.DHT11,
# Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
sensor = Adafruit_DHT.DHT11

# Example using a Raspberry Pi with DHT sensor
# connected to GPIO23.
pin = 14

while True:
  client.loop()

  humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
  if humidity is not None and temperature is not None:
    print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
    client.celsiusWrite(1, temperature)
    client.luxWrite(2, humidity)
  else:
    print('Failed to get reading. Try again!')

Save the file as dht11_cayenne.py and execute it.

dht11 cayenne

dht11 cayenne sensor data

Sensor data on Cayenne Dashboard

cayenne dashboard