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.
Download the Adafruit DHT11 library. In the terminal, type the following command.
Navigate to Adafruit_Python_DHT directory (folder).
Run the following commands in the terminal.
- sudo apt-get install build-essential python-dev
- sudo apt-get install build-essential python3-dev
To install the library, in the terminal, type the following.
- sudo python setup.py install
- sudo python3 setup.py install
Navigate to the example folder.
- cd examples
- sudo nano dhtsimple.p
Replace the following demo code.
- import Adafruit_DHT
- while True:
- humidity, temperature = Adafruit_DHT.read_retry(11, 27)
- print ("Humidity = {} %; Temperature = {} C".format(humidity, temperature))
Run the program.
Step 2
- Open Thingspeak (https://thingspeak.com).
- Create a new channel and select fields 1 and 2.
- Select Ras_Pi channel and API Keys.
- Get the Write API Key or generate the new Write API Key.
Step 3
Open the new Python file and name it as dht1234567.py like below.
sudo nano dht1234567.py
Replace the following code.
- import httplib, urllib
- import time
- import Adafruit_DHT
- sleep = 30 # how many seconds to sleep between posts to the channel
- key = '****************' # Write API key
-
- humidity, temperature = Adafruit_DHT.read_retry(11, 27) # GPIO27 (BCM notation)
-
-
- #Report Raspberry Pi internal temperature to Thingspeak Channel
- def thermometer():
- while True:
- headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept": "text/plain"}
- conn = httplib.HTTPConnection("api.thingspeak.com:80")
- try:
- params = urllib.urlencode({'field1': temperature, 'key':key }) # channel name is field1 or field 2
- conn.request("POST", "/update", params, headers)
- response = conn.getresponse()
- print humidity
- print temperature
- #print response.status, response.reason
- data = response.read()
- conn.close()
- except:
- print "connection failed"
- break
- #sleep for desired amount of time
- if __name__ == "__main__":
- while True:
- thermometer()
- time.sleep(sleep)
-
-
Run the program.
- sudo python dht1234567.py
Graphical representation.
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.