Analyzing Live Data Streams In Cloud Using Raspberry Pi, DHT11, And ThingSpeak

Introduction

 
In this IoT article, we will use a DHT11, Raspberry Pi, and ThingSpeak to analyze the temperature and humidity.
 
Requirement
  • DHT11 - Temperature and Humidity Sensor Module
  • ThingSpeak
  • Raspberry Pi
  • Putty
DHT11 Sensor Actions
 
 
Thingspeak 
 
ThingSpeak allows you to aggregate, visualize, and analyze live data streams in the cloud. ThingSpeak provides instant visualizations of data posted by your devices or equipment. Execute MATLAB code in ThingSpeak, and perform online analysis and processing of the data as it comes in. ThingSpeak accelerates the development of proof-of-concept IoT systems, especially those that require analytics. You can build IoT systems without setting up servers or developing web software.
 
Step 1
  • Set up the DHT11 humidity sensor on the Raspberry Pi.
  • GPIO pin is 27.
Analyzing Live Data Streams In The Cloud Using Raspberry Pi, DHT11 And Thingspeak  
 
Download the Adafruit DHT11 library. In the terminal, type the following command.
  1. git clone https://github.com/adafruit/Adafruit_Python_DHT.git  
Navigate to Adafruit_Python_DHT directory (folder).
  1. cd Adafruit_Python_DHT  
Run the following commands in the terminal. 
  1. sudo apt-get install build-essential python-dev  # python2      
  2. sudo apt-get install build-essential python3-dev  # python3    
To install the library, in the terminal, type the following.
  1. sudo python setup.py install  # python2    
  2. sudo python3 setup.py install  # python3    
Navigate to the example folder.
  1. cd examples    
  2. sudo nano dhtsimple.p 
Replace the following demo code.
  1. import Adafruit_DHT  # is a must  
  2. while True:    
  3.     humidity, temperature = Adafruit_DHT.read_retry(1127)  # GPIO27 (BCM notation)    
  4.     print ("Humidity = {} %; Temperature = {} C".format(humidity, temperature))   
Run the program.
  1. sudo python dhtsimple.py  
Output 
 
Analyzing Live Data Streams In The Cloud Using Raspberry Pi, DHT11 And Thingspeak 
 
Step 2
  • Open Thingspeak (https://thingspeak.com).
  • Create a new channel and select fields 1 and 2.
Analyzing Live Data Streams In The Cloud Using Raspberry Pi, DHT11 And Thingspeak
  • Select Ras_Pi channel and API Keys.
Analyzing Live Data Streams In The Cloud Using Raspberry Pi, DHT11 And Thingspeak
  • Get the Write API Key or generate the new Write API Key.
Analyzing Live Data Streams In The Cloud Using Raspberry Pi, DHT11 And Thingspeak
 
Step 3
 
Open the new Python file and name it as dht1234567.py like below.
sudo nano dht1234567.py
Replace the following code.
  1. import httplib, urllib  
  2. import time  
  3. import Adafruit_DHT  
  4. sleep = 30 # how many seconds to sleep between posts to the channel  
  5. key = '****************'  # Write API key 
  6.   
  7. humidity, temperature = Adafruit_DHT.read_retry(11, 27)  # GPIO27 (BCM notation)  
  8.   
  9.   
  10. #Report Raspberry Pi internal temperature to Thingspeak Channel  
  11. def thermometer():  
  12.     while True:        
  13.         headers = {"Content-typZZe""application/x-www-form-urlencoded","Accept""text/plain"}  
  14.         conn = httplib.HTTPConnection("api.thingspeak.com:80")  
  15.         try:  
  16.             params = urllib.urlencode({'field1': temperature, 'key':key }) # channel name is field1 or field 2
  17.             conn.request("POST""/update", params, headers)  
  18.             response = conn.getresponse()  
  19.             print humidity  
  20.         print temperature  
  21.             #print response.status, response.reason  
  22.             data = response.read()  
  23.             conn.close()  
  24.         except:  
  25.             print "connection failed"  
  26.         break  
  27. #sleep for desired amount of time  
  28. if __name__ == "__main__":  
  29.         while True:  
  30.                 thermometer()  
  31.                 time.sleep(sleep)  
  32.                   
  33.                   
Run the program.
  1. sudo python dht1234567.py  
Analyzing Live Data Streams In The Cloud Using Raspberry Pi, DHT11 And Thingspeak
 
Graphical representation.
 
Analyzing Live Data Streams In The Cloud Using Raspberry Pi, DHT11 And Thingspeak 
 
Download all of this Channel's feeds in the CSV format.
 

Summary

 
Finally, we have successfully created a graph that live-streams the data in the cloud with Raspberry Pi and ThingSpeak.