IoT using Raspberry Pi

Introduction

  • DHT11 sensor measures and provides humidity and temperature values serially over a single wire.

  • It can measure the relative humidity in percentage (20 to 90% RH) and temperature in degree Celsius in the range of 0 to 50°C.

  • It has 4 pins; one of which is used for data communication in serial form.

  • Pulses of different TON and TOFF are decoded as logic 1 or logic 0 or start pulse or end of a frame.

Interfacing diagram

DHT11 Interfacing with Raspberry Pi

DHT11 Interfacing with Raspberry Pi 3

Example

  • Here, we are going to interface the DHT11 sensor with Raspberry Pi 3 and display Humidity and Temperature on the terminal.

  • We will be using the DHT Sensor Python library by Adafruit from GitHub. The Adafruit Python DHT Sensor library is created to read the Humidity and Temperature on raspberry Pi or Beaglebone Black. It is developed for DHT series sensors like DHT11, DHT22 or AM2302.

Download Adafruit DHT Sensor library from here.

  • Extract the library and install it in the same root directory of the downloaded library by executing the following command,

sudo python setup.py install

  • Once the library and its dependencies have been installed, open the example sketch named simple test from the library kept in the examples folder.

  • In this code, raspberry Pi reads Humidity and Temperature from the DHT11 sensor and prints them on the terminal. But, it read and display the value only once. So, here we made a change in the program to print value continuously.

Note:

  • Assign proper sensor type to the sensor variable in this library. Here, we are using the DHT11 sensor.

sensor = Adafruit_DHT.DHT11

  • If anyone is using sensor DHT22 then we need to assign Adafruit_DHT.DHT22 to the sensor variable shown above.

  • Then assign pin no. to which DHT sensor’s data pin is connected. Here, data out of the DHT11 sensor is connected to GPIO4. As shown in the above interfacing diagram.

Python Program:

#Install Paho module by “sudo pip3 install Adafruit_Python_DHT” in Command prompt
#Install Paho module by “pip install paho-mqtt” in Command prompt

import os
import time
import sys
import paho.mqtt.client as mqtt
import json
import Adafruit_DHT

TOKEN = ‘Replace With your device token’
sensor = Adafruit_DHT.DHT11
topic = ‘v1/devices/me/telemetry’

data = {‘Temperature’: 0,‘Humdifity’: 0}
pin = 4

qos=1

client = mqtt.Client()
client.username_pw_set(TOKEN)
client.connect(‘web.cloudchip.io’, 1883, 60)
client.loop_start()

try:
    while True:
        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        print(‘Temp={0:0.1f}*C  Humidity={1:0.1f}%’.format(temperature, humidity))
        count = count+1
        data = {‘Temperature’: temperature,‘Humdifity’: humidity}
        client.publish(topic, json.dumps(data), qos)
        time.sleep(1)
except KeyboardInterrupt:
    pass

client.loop_stop()
client.disconnect()

Output:

DHT11 Output

The temperature and humidity is uploaded to the cloud:

Leave a Reply

Your email address will not be published. Required fields are marked *