electronut Programming & Embedded Systems - by Mahesh Venkitachalam

Plotting DHT11 sensor data at ThingSpeak.com using Raspberry Pi Model A+

sensor data

Internet Of Things

Feeling left behind, I too want to get on board the Internet Of Things bandwagon by putting a sensor on the net. For this experiment, I decided to attach a DHT11 Temperature/Humidity sensor to a Raspberry Pi Model B+ and use thingspeak.com to plot the data. I also looked at data.sparkfun.com which offers a similar service, but it doesn’t actually plot your data.


ThingSpeak is an “open data platform for the Internet Of Things”. To get started, you need to create a channel that specifies what you are plotting - title, range, number of fields, etc. You then update data in your channel with an HTTP request of the form:


The data stream itself can be viewed at the following URL:


ThingSpeak provides a lot of options for displaying your channel’s data - make it public/private, changing extents, layouts, etc. The only limitation is the rate of data updates - it has to be no more frequent than 15 seconds. But the whole software is Open Source, which means you could host it on your own if you need faster updates.

Raspberry Pi and DHT11

I’ve written before about the DHT11 sensor here. Here is how I connected it to Raspberry Pi:

sensor data

Here is how it looks on a breadboard:

sensor data

Now we need to connect this to the internet.

The Code

I used Python to communicate with the DHT11 sensor and put the data on ThingSpeak. To talk to the sensor, I used the Adafruit DHT Library. Here is the code, and the output from ThingSpeak is what you saw at the beginning of this article.


Temperature/Humidity monitor using Raspberry Pi and DHT11.
Data is displayed at thingspeak.com

Author: Mahesh Venkitachalam
Website: electronut.in


import sys
import RPi.GPIO as GPIO
from time import sleep  
import Adafruit_DHT
import urllib2

def getSensorData():
    RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 23)
    # return dict
    return (str(RH), str(T))

# main() function
def main():
    # use sys.argv if needed
    if len(sys.argv) < 2:
        print('Usage: python tstest.py PRIVATE_KEY')
    print 'starting...'

    baseURL = 'https://api.thingspeak.com/update?api_key=%s' % sys.argv[1]

    while True:
            RH, T = getSensorData()
            f = urllib2.urlopen(baseURL +
                                "&field1=%s&field2=%s" % (RH, T))
            print f.read()
            print 'exiting.'

# call main
if __name__ == '__main__':

You run it by uploading it to your Pi and doing:

sudo python dht11_thingspeak.py YOURWRITEAPIKEY


I was looking for a quick way to put sensor data on the Internet, and ThingSpeak fits the bill. And now I can claim to be part of the IOT buzz. ;-)

If you liked this article, please consider supporting my efforts by purchasing my book.

Python Playground, published by No Starch Press, USA, is a collection of imaginative programming projects that will inspire you to use Python to make art and music, build simulations of real-world phenomena, and interact with hardware like the Arduino and Raspberry Pi. Do check it out!