Send Sensor’s Data to ThingSpeak Using Raspberry Pi

Use ThingSpeak in your Raspberry Pi IoT projects.

Introduction

Data acquisition is important to further do analysis. ThingSpeak allows us to send the data and display it in the graph. For the student, the best thing is, it also offer a free account for you to try. So in this tutorial, I will share with you how to send sensor’s data to ThingSpeak using Raspberry Pi. You may need to refer the following tutorial to get familiar with other IoT platforms.

  1. Displaying Sensor’s Data on Blynk App Using Raspberry Pi
  2. Getting Started With Favoriot IoT Platform Using Raspberry Pi

Video

This video will show you how to send sensor’s data to ThingSpeak IoT platform using Raspberry Pi.

Hardware Preparation

This is the list of items used in the video.

Sample Program

Below is the sample program for Thonny Python IDE used in the video. Before that please install the DHT sensor python library from Adafruit. Write the following command on the terminal.

git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo python3 setup.py install
from gpiozero import Buzzer
import Adafruit_DHT
from time import time, sleep
from urllib.request import urlopen
import sys
WRITE_API = "Your ThingSpeak Write API" # Replace your ThingSpeak API key here
BASE_URL = "https://api.thingspeak.com/update?api_key={}".format(WRITE_API)
buzzer = Buzzer(26)
SENSOR_PIN = 17
SENSOR_TYPE = Adafruit_DHT.DHT22
SensorPrevSec = 0
SensorInterval = 2 # 2 seconds
ThingSpeakPrevSec = 0
ThingSpeakInterval = 20 # 20 seconds
try:
while True:
if time() SensorPrevSec > SensorInterval:
SensorPrevSec = time()
humidity, temperature = Adafruit_DHT.read_retry(SENSOR_TYPE, SENSOR_PIN)
print("Humidity = {:.2f}%\tTemperature = {:.2f}C".format(humidity, temperature))
if time() ThingSpeakPrevSec > ThingSpeakInterval:
ThingSpeakPrevSec = time()
thingspeakHttp = BASE_URL + "&field1={:.2f}&field2={:.2f}".format(temperature, humidity)
print(thingspeakHttp)
conn = urlopen(thingspeakHttp)
print("Response: {}".format(conn.read()))
conn.close()
buzzer.beep(0.05, 0.05, 1)
sleep(1)
except KeyboardInterrupt:
conn.close()

view raw
RPiThingSpeakHTTP.py
hosted with ❤ by GitHub

Thank You

References:

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

2 thoughts on “Send Sensor’s Data to ThingSpeak Using Raspberry Pi”

  1. Thank you for this. Had loads of errors trying to use old urllib libraries (newbie you see) . Using a sensehat and now have it writing data to thingspeak. Question though how do I get the script to keep sending data when there has been an interruption in the internet connection?
    Many thanks in advance
    Pete

  2. hi change library
    try:
    from urllib.request import urlopen
    except ImportError:
    from urllib2 import urlopen
    and error
    Traceback (most recent call last):
    File “dht_11thingspeak.py”, line 29, in
    humidity, temperature = Adafruit_DHT.read_retry(SENSOR_TYPE, SENSOR_PIN)
    File “build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py”, line 94, in read_retry
    File “build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py”, line 81, in read
    File “build/bdist.linux-armv7l/egg/Adafruit_DHT/Beaglebone_Black.py”, line 213, in read
    RuntimeError: Error accessing GPIO. Make sure program is run as root with sudo!

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

IoT on Raspberry Pi Pico using CircuitPython and Adafruit IO
ESP32 as WiFi co-processor for Raspberry Pi Pico
3.3V Serial Character LCD Display with micro:bit via I2C
Displaying Telegram Message on 3.3V Serial LCD using NodeMCU ESP8266
micro:bit Quick Start Kit
Tutorials of Cytron Technologies Scroll to Top