Introduction

In this IoT article, we will use a DHT11, Raspberry Pi, and ThingSpeak to analyze the temperature and humidity.
Requirement
DHT11 Sensor Actions
Refer to my previous article Stream Sensor Data In Real-Time With IoT Hub Using PowerBI
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(11, 27) # 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. humidity, temperature = Adafruit_DHT.read_retry(11, 27) # GPIO27 (BCM notation)
  7. #Report Raspberry Pi internal temperature to Thingspeak Channel
  8. def thermometer():
  9. while True:
  10. headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept": "text/plain"}
  11. conn = httplib.HTTPConnection("api.thingspeak.com:80")
  12. try:
  13. params = urllib.urlencode({'field1': temperature, 'key':key }) # channel name is field1 or field 2
  14. conn.request("POST", "/update", params, headers)
  15. response = conn.getresponse()
  16. print humidity
  17. print temperature
  18. #print response.status, response.reason
  19. data = response.read()
  20. conn.close()
  21. except:
  22. print "connection failed"
  23. break
  24. #sleep for desired amount of time
  25. if __name__ == "__main__":
  26. while True:
  27. thermometer()
  28. time.sleep(sleep)
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.