As an Amazon Associate we earn from qualifying purchases.
ThingSpeak is a free website for collecting data and plotting it as live charts. You send a value to it over the internet, and it stores the value and draws it on a graph. With a few lines of Python you can do this from a laptop, a Raspberry Pi, or any machine that runs Python and has a network connection.
This article shows how to plot data with Python and ThingSpeak, step by step. It also explains what each part of the code does, so you can adapt it to your own sensor or project.
How ThingSpeak works
ThingSpeak organises data into channels. A channel has up to eight fields, and each field holds one kind of value, such as temperature, humidity or a counter. Every time you send a value, ThingSpeak adds a new entry with a timestamp and updates the chart for that field.
Sending data is done with a simple HTTP request to the ThingSpeak update address. The request carries two things: your channel’s Write API Key and the value for one or more fields. Because it is just a web request, Python’s requests library is all you need.
What you need
- A free ThingSpeak account.
- Python 3 installed on your computer.
- The
requestslibrary (install it withpip install requests). - An internet connection.
Step 1: Create a channel and get your key
- Sign in to ThingSpeak and create a new channel.
- Give the channel a name and enable at least one field, for example Field 1.
- Save the channel.
- Open the API Keys tab and copy the Write API Key.
The key is different for every account and every channel, so the key in any example code you find online will not work for you. Replace it with your own before you run the script. Treat the Write API Key like a password, because anyone who has it can add data to your channel.
Step 2: Send a value from Python
The script below sends the value 30 to Field 1 of your channel. Replace YOUR_WRITE_API_KEY with the key you copied.
import requests
WRITE_API_KEY = "YOUR_WRITE_API_KEY"
URL = "https://api.thingspeak.com/update"
r = requests.get(URL, params={"api_key": WRITE_API_KEY, "field1": 30})
print(r.text)
When you run it, ThingSpeak replies with the number of the new entry in your channel. If the update is rejected, the reply is . Open your channel page and you should see a point on the Field 1 chart.
You can also print the reply with pprint, which is handy when you are checking responses:
from pprint import pprint
import requests
r = requests.get("https://api.thingspeak.com/update",
params={"api_key": "YOUR_WRITE_API_KEY", "field1": 30})
pprint(r.json())
If you do not need the reply, you can skip printing it. The call below sends the value 50 to Field 3 and ignores the response:
import requests
requests.get("https://api.thingspeak.com/update",
params={"api_key": "YOUR_WRITE_API_KEY", "field3": 50})
Use one version or the other. They are alternatives, not parts of one script.
Step 3: Plot a stream of readings
One point does not make much of a chart. To plot data over time, send values in a loop with a pause between them. Free ThingSpeak accounts limit how often a channel can be updated (historically about once every 15 seconds), so wait at least that long between requests. Check the current limits in your account.
import random
import time
import requests
WRITE_API_KEY = "YOUR_WRITE_API_KEY"
URL = "https://api.thingspeak.com/update"
while True:
value = random.randint(20, 40) # replace with a real reading
r = requests.get(URL, params={"api_key": WRITE_API_KEY, "field1": value})
print("Sent", value, "-> entry", r.text)
time.sleep(20)
The random number stands in for a real measurement. Swap it for a value from a sensor, a file, or any calculation you want to track.
Send several fields at once
If your channel has more than one field, put them all in the same request. This uses a single update instead of several, which keeps you inside the rate limit.
params = {
"api_key": WRITE_API_KEY,
"field1": 24.5, # for example, temperature
"field2": 60, # for example, humidity
}
r = requests.get(URL, params=params)
Using real sensor data
Plotting becomes more useful when the values come from hardware. A common setup is an Arduino reading a sensor and sending the numbers to your computer over a USB cable. Python reads them and forwards them to ThingSpeak. Our guides on how to read serial data in Python and on displaying temperature in Python with an LM35 sensor cover the first half of that chain. Once you have a number in a variable, you can pass it as field1 in the code above.
If you want to keep a local copy as well, see our article on temperature and humidity data logging. For more ways to chart readings on your own machine, read about Arduino data visualization, and for processing the numbers further, data analysis with Python.
Troubleshooting
- The reply is 0. The update was not accepted. Check the Write API Key and make sure you are not sending updates too quickly.
- Nothing appears on the chart. Make sure the field you send to (for example
field1) is enabled in the channel settings. Then refresh the page. - Connection errors. Check your internet connection and that nothing, such as a firewall, blocks the request.
- Wrong key. Use the Write API Key, not the Read API Key or your account key.
Keep your key safe
Do not publish your Write API Key in public code, screenshots or videos. If it leaks, you can generate a new key on the channel’s API Keys tab, and the old one stops working. For larger projects, store the key in an environment variable instead of writing it into the script.
FAQ
Is ThingSpeak free?
ThingSpeak offers a free tier that is enough for small hobby projects. It has limits on how many messages you can send and how often, so check the current terms on the site if you plan to log data heavily.
Do I need an Arduino to plot data on ThingSpeak?
No. Any value that Python can produce can be sent, including random test numbers, system statistics or data from a file. An Arduino or other sensor board is only one way to get real measurements.
Why does the key in the example not work for me?
Every ThingSpeak channel has its own Write API Key. You must create your own channel and use your own key in the code.
Can I send more than one value at a time?
Yes. A channel can have up to eight fields. Add each one as a parameter, such as field1 and field2, in the same request.