How to Talk To ThingSpeak With Python (A Memory/CPU Monitor)

Here's a short tutorial on getting Python talking to the new ThingSpeak data logging web service. ThingSpeak joins the likes of Pachube.com allowing web citizens to upload their data from devices, computers, and sensors using a RESTful web service. ThingSpeak out of the box comes with a beautiful chart visualisation API that features real-time updates. In this blog post I'll use Python to connect to ThingSpeak and send my computer's available memory and CPU usage, then visualise the data in real-time using the ThingSpeak Charts API. Watch the examples below for some real-time updates!

1. Register For Your ThingSpeak API Key and Channel

Get your Thingspeak account and register a new channel. If you haven't already, install Python on your machine. I was able to register Channel 300.

2. A Python HTTP Test

Start interactive Python-- we'll PUT some data to ThingSpeak as a proof of concept. Make sure to replace "YOUR_API_KEY" with your own API key!


#Pushing data to Thingspeak
# python
>>> import httplib, urllib
>>> params = urllib.urlencode({'field1': 12, 'field2': 11,'key':'YOUR_API_KEY'})
>>> headers = {"Content-type": "application/x-www-form-urlencoded","Accept":  
    "text/plain"}
>>> conn = httplib.HTTPConnection("api.thingspeak.com:80")
>>> conn.request("POST", "/update", params, headers)
>>> response = conn.getresponse()
>>> print response.status, response.reason
    200 OK
>>> data = response.read()
>>> conn.close()

3. System Performance Data with PSUTIL

Install the psutil cross-platform system process manager library to easily access your system performance data with Python. Compiled OS-specific libraries are available for Linux, Mac OS X and Windows. This tutorial is running currently on my Windows 7 machine.

4. The Python Script

Write a Python script to log CPU and Memory Usage to Thingspeak. This will PUT our data to Thinkspeak every 16 seconds. Put this code into something like "thingspeak_cpu.py". Code is available on my Github repo.

April 18th, 2011- Code has been updated! Check the Github repository for the lastest code.


import httplib, urllib
# download from http://code.google.com/p/psutil/
import psutil
import time

def doit():
	cpu_pc = psutil.cpu_percent()
	mem_avail_mb = psutil.avail_phymem()/1000000
	print cpu_pc
	print mem_avail_mb
	params = urllib.urlencode({'field1': cpu_pc, 'field2': mem_avail_mb,'key':'YOUR_API_KEY'})
	headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
	conn = httplib.HTTPConnection("api.thingspeak.com:80")
	conn.request("POST", "/update", params, headers)
	response = conn.getresponse()
	print response.status, response.reason
	data = response.read()
	conn.close()

#sleep for 16 seconds (api limit of 15 secs)
if __name__ == "__main__":
	while True:
		doit()
		time.sleep(16) 

5. Make Some Pretty Pictures

Visualise your data with the awesome ThingSpeak charts API. The charts are embedded as iframes in your website-- the code for the memory monitor is included below: