Internet of Things – A simple demonstration – how build an IoT Device?

Internet of Things ( IoT ) is the trending word in recent times . Well, this not only a fad. It is a reality. All the devices are becoming internet enabled, data collected by these devices are being analyzed and meshed up with other data sources. We as a human species are going to improve our overall knowledge level by use of this collective data. Internet connected fitness devices such as fitbit, garmin & apple watches are collecting humongous data of health – fitness activity, patterns of burning calories, live heart rate data, sleep data etc. This data is opening new viewports for healthcare researchers & they are getting a new perspective about human health.

Building a simple IoT device.

Today let’s build a simple IoT device. This device collects local temperature and humidity data and sends it to a server over internet. Alerts can be created on various triggers.

For building this simple IoT device we will use following.

  1. Raspberry PI – A credit card sized computer.
  2. DHT11 temperature & humidity sensor
  3. ThingSpeak – A server side platform for collecting IoT device data.
  4. A Python Program – It will run on Raspberry Pi
  5. Twitter alert – Create a twitter alert on specific events.

Internet connected devices ( IoT ) are built using either custom hardware designed for that specific device or a general purpose hardware. A general purpose hardware is also called as a development board. Raspberry PI , Arduino , Chip from, Omega from are examples of general purpose development boards.  These boards are complete small sized computers with scaled down version of Operating System. These boards are having various ports and General-purpose input/output (GPIO) pins to control external hardware.

In this example, we will use Raspberry PI for building a simple IoT device. There are many models of raspberry PI. This example should work on any model. Just need to make sure that we are connecting our sensor to right GPIO pin & use the same pin in our python program.

Connecting Sensor DHT11 to Raspberry PI.

The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin. Let’s connect this sensor to GPIO PIN 2 of Raspberry PI.  Please see connection diagram below. DHT11 can have three or four pins. If it is a 4 pin version, then a pin is unused[NC].  PIN 1 is VCC, PIN 2 is data, if you have 4 PIN version then PIN3 is unused[NC], PIN4 is ground. [ In case of 3 PIN version PIN 3 is ground]

  • Connect 3.3V VCC from Raspberry PI to bread board and then to pin 1
  • Connect 10K register from pin 2 of DHT 11 to VCC
  • Connect PIN 4 of DHT 11 to Raspberry PI ground as shown in diagram 1
  • GPIO 2 is connected to DHT11 data pin


Connection Diagram
Diagram 1: Connection Diagram


Raspberry PI GPIO pin diagram
Diagram 2 – Raspberry PI GPIO pin diagram. (Source:


Once circuit is built, we can write a python program to read temperature and humidity to GPIO PIN2 & this program will also send data to server.

Let’s setup server side to receive data. We will use . ThingSpeak provides server side infrastructure to collect device data and process it.

  1. Create a free account on
  2. Create a new channel as shown below. Here you define the fields for device to collect & send data.
  3. Once you save the Channel, go to API Key section of channel & note the Write API key. This key will be needed to send data to ThingSpeak channel from device.

Channel Settings

Before we run python program for reading date, we need to setup raspberry pi to run python & required libraries.

Install updates

            sudo apt-get update

Install Python

            sudo apt-get install python-dev

Install Rpi.GPIO ( This is python library to interact with GPIO pins )

            sudo apt-get install python-rpi.gpio

Install library of ADH11 sensor – Download library

git clone
cd Adafruit_Python_DHT

Install Library

sudo apt-get update

sudo apt-get install build-essential python-dev python-openssl
sudo python install

Above steps will install all required libraries & code will be ready to run .

Python program to read temperature & humidity from sensor.

Use any text editor such as VIM or Nano to create this program.

This code is also available on github

You can download the code by using following commad

     git clone

Code :

# Script will be used to read sensor data and then post the IoT Thinkspeak
# Read Temperature & Humidity using DHT11 sensor attached to raspberry PI
# Program posts these values to a thingspeak channel
# Import all the libraries we need to run
import sys
import RPi.GPIO as GPIO
import os
from time import sleep
import Adafruit_DHT
import urllib2
# Define GPIO pin to which DHT11 is connected
DHTpin = 2
#Setup our API and delay
myAPI = "GET_YOUR_KEY"  # API Key from channel
myDelay = 15 #how many seconds between posting data

def getSensorData():
    print "In getSensorData";
    humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, DHTpin)
    if humidity is not None and temperature is not None:
        print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
        print('Failed to get reading. Try again!')
    return (str(humidity), str(temperature),str(TWF))

def main():
    print 'starting...'
    baseURL = '' % myAPI
    print baseURL
    while True:
            print "Reading Sensor Data now"
            RHW, TW, TWF = getSensorData()
            print TW + " " + TWF+ " " + RHW + " "
            f = urllib2.urlopen(baseURL + "&field1=%s&field2=%s&field3=%s" % (TW, TWF, RHW))
        except Exception as e:
            print e
            print 'exiting.'
# call main
if __name__ == '__main__':

Save and run the program . It will start reading data every 15 seconds and start posting on ThingSpeak channel , output should like below.

pi@raspberrypi:~/incubx $ python
Reading Sensor Data now
In getSensorData
Temp=25.0*C  Humidity=56.0%
25.0 77.0 56.0
Reading Sensor Data now
In getSensorData
Temp=25.0*C  Humidity=56.0%
25.0 77.0 56.0

Login to thinkSpeak & look at dashboards generated from the data posted by our IoT device.  The graphs are generated showing data of temperature and humidity on timescale. ThingSpeak provides a ready made framework for capturing and displaying data and features. It also provide many features such as creating alerts , analyzing data etc. There are many such frameworks such as Thinkworx , Amazon IoT , Azure or we can create our own APIs.

ThingSpeak Dashboard

Creating an alert based on an event.

ThingSpeak provides apps perform various actions based on data received.  Goto apps section on thingspeak and select “React”. Select new react , and create an action. In test frequency select “On Data Insertion”, and in condition define,  if temperature is greater than 27 degree Celsius then action as ThingTweet to tweet an alert.


Create Alerts

As soon as temperature rises more than trigger value then a twit is sent using configured account. The action can also be defined to call another API to specific thing such as turning on IoT enabled fan.

Tweet from IoT Device
Tweet from IoT Device

There are many more ways of building IoT devices.  We can use Frameworks such as Amazon AWS, ThingWorx, MQTT protocols etc. Using collected data for analytics and machine learning new limits of technology can be tested. Let’s start new journey on this road.

That’s all folks! Feel free to contact us for your queries regarding building any IoT devices.




Leave a Reply

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