How To Send Sensor Data To Thingspeak Using Raspberry Pi
How To Send Sensor Data To Thingspeak Using Raspberry Pi
Raspberry Pi
iotstarters.com/how-to-send-sensor-data-to-thingspeak-using-raspberry-pi
In some IoT projects data is generated continuously. One way to permanently store them
is by saving the data in a local database, however there are other alternatives also
available. One of these alternatives is ThingSpeak. Here you can create a free account
for small applications and transfer your data very easily. And that’s exactly what this
tutorial is about on How to send sensor data to Thingspeak using Raspberry Pi.
Things Needed
For building this project you need below components.
Raspberry Pi
DHT11 Sensor
Jumper Cables
How it works?
The working of this project is simple and easy. DHT11 sensor will collect the temperature
and humidity data and send it to Raspberry Pi. Later Raspberry Pi will send it to
Thingspeak channel.
1/8
We will define the channels by entering the a proper name, description, and up to 8 fields
can be used to name the parameter. For the Field 1 and Field 2 we have named
Temperature and humidity. These field values that you set can be modified later. These
values will be in Degree Centigrade and Relative Humidity in %. Once you update the
name, click on Save.
Once you have saved the channel, you will be automatically redirected to the “Private
View” tab. Here the mapped fields are shown as a diagram. You will find the “Channel ID”
(we will need it later). Below You will also see API Keys option.
2/8
Later, click on the “API Keys” tab. The two values of “Write API key” and “Read API
key” are equally necessary for us to write or retrieve data. Copy these keys and keep it
safe as we need to put it in the code.
Connection Diagram
This is how you have to make the connection with the sensor to the Pi board. Data or
signal pin of DHT11 sensor is connected with GPIO 4 with Raspberry Pi.
3/8
Installing Required Libraries
Before installing the libraries we need to update the packages installed in Raspberry Pi.
For installing the basic updates run these commands in a terminal window on your
Raspberry Pi
Now we install the library to read the DHT11 or DHT22 sensors. Below library will work
for both the sensor types.
In order to use the service, it is possible to simply send the data via “POST” or retrieve it
via “GET”. The functions are available in almost any programming language. If you don’t
have enough experience or just don’t feel like writing it, you can also use the ThingSpeak
library. For this we simply install it using below command
4/8
The python script will read the DHT11 temperature and humidity every 15 seconds and
send it to our channel.
Now Update the code with Channel ID and write key values from Thingspeak portal. Now
save it and your code is ready to run.
channel_id = 1391845 # put here the ID of the channel you created before
write_key = 'TNXXJJII892UHJ1C' # update the "WRITE KEY"
Code
If you are using a different GPIO for the sensor apart from GPIO4 then you have to make
changes in pin variable in the code. For DHT22 sensor, the code needs to be adjusted a
bit more.
5/8
You can download the code from the below link and extract it in the root of Raspberry pi
directory (/home/pi).
For executing the code run the below command in your raspberry pi terminal.
python thingspeak_DHT11.py
Now you will see the output in raspberry pi terminal as well as in Thingspeak dashboard.
In Thingspeak dashboard you can see the graph and the readings.
6/8
More Interesting Projects:
DIY Exercise
If you want to build a weather station then you can use the below sensors. You can use
the same code but you need to tweak it as per the sensors below. You can do it as a DIY
exercise
DHT22
BMP180
LDR
Summary
In this project we are using a DHT11 temperature and humidity sensor with Raspberry Pi.
The raspberry pi will connect the values and send it to Thingspeak platform to a particular
channel to display the data. We are using python code to run this project. You can
enhance this project by adding some more sensors and send the same data to
Thingspeak. Hope you like this project and don’t forget to share this article.
7/8
8/8