Python Program 4: Interfacing Sensor Data to ThingSpeak through Raspberry Pi
Objective:
To interface temperature and humidity data from a DHT11 sensor to ThingSpeak using a Raspberry Pi.
Resources Required:
- Raspberry Pi 2
- DHT11 Sensor
- Connectors
- Sensor connecting wires
- Computer system
Theory:
Setting Up ThingSpeak Account
Before we begin, you need to create an account on ThingSpeak:
-
Visit the ThingSpeak website.
-
After signing up or logging in, click on New Channel to create a new channel.
-
Provide the required details for your channel:

-
Channel Name: Choose a suitable name.
-
Channel Description: Describe the purpose of your channel (e.g., "Temperature and Humidity Data").
-
You can configure up to 8 fields for different parameters. For this experiment, we’ll use:
-
Field 1: Temperature (°C)
-
Field 2: Humidity (%)
-
-
After setting the fields, click Save.

Once saved, you will be redirected to the Private View tab. Here, you’ll find the Channel ID and API Keys that you will need for your project.
-
Write API Key: Required to send data to ThingSpeak.
-
Read API Key: Used to fetch data from the channel.
Make sure to copy and store these keys safely.
Circuit Diagram
For this experiment, the DHT11 sensor is connected to the Raspberry Pi GPIO pin (GPIO4 in this case). Here's how the connections should be made:
- VCC pin of the DHT11 goes to 3.3V on the Raspberry Pi.
- GND pin of the DHT11 goes to GND on the Raspberry Pi.
- DATA pin of the DHT11 connects to GPIO4 on the Raspberry Pi.
Python Code:
The following Python code will read data from the DHT11 sensor and send it to your ThingSpeak channel.
Explanation of the Code:
-
Libraries Used:
http.clientandurllib: These libraries are used to make HTTP requests to ThingSpeak.adafruit_dht: This library allows interaction with the DHT11 sensor.psutil: This library is used to check for conflicting processes and kill them before starting the script.- Sensor Setup: The DHT11 sensor is initialized, and the temperature and humidity are read in the
thermometer()function. -
Sending Data to ThingSpeak: The function then constructs a POST request containing the temperature and humidity data, along with the ThingSpeak API key. This data is sent to ThingSpeak using an HTTP connection.
Result:
After running the code, the temperature and humidity data should be successfully uploaded to your ThingSpeak channel. You can view the real-time data on your ThingSpeak dashboard, which will automatically update with new readings.
Conclusion:
This experiment demonstrates how to interface the DHT11 sensor with a Raspberry Pi to send temperature and humidity data to ThingSpeak. This project serves as a foundational step toward building IoT-based applications, where remote data monitoring is key.
By uploading sensor data to ThingSpeak, we can access it from anywhere and use the platform’s visualization tools to analyze trends and patterns in the data.
✅ 👉 If you found this helpful, don’t forget to like, follow, comment, and share!
Let’s continue learning and building cool sensor projects. Team up with me and let’s complete this together! 💻✨





Comments
Post a Comment