Testing Data Logging RESTful Webservice – using Python and Thingsspeak

Your windows “TaskManager” just went LIVE!

Imagine being able to broadcast your CPU/MEM/Disk capacity data or data from a hardware device or appliance (Arduino based sensor) to a service. Now imagine being able to view this data in a live graphical manner and being able to share it with the rest of the world (power to correlate data sets).

This well written article at http://www.australianrobotics.com.au/content/how-talk-thingspeak-python-memorycpu-monitor  inspired me to test out this capability and this post is a testament to the ease with which it can be done and a log of some minor modifications I had to make to make the example work with the latest version of Python (3.2).

Thingsspeak provides a data logging webservice with RESTful APIs to update the data.  I outline the steps on how to use this service and provide links to live feed from my machine.




The following code was obtain from https://github.com/sirleech/thingspeak and modified to use the python 3.2 libraries

 # Original Source: https://github.com/sirleech/thingspeak
 import http.client, urllib.parse
 from time import localtime, strftime
 # 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
 params = urllib.parse.urlencode({'field1': cpu_pc, 'field2': mem_avail_mb,'key':'XBYEL45GOXF7F760'})
 headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
 conn = http.client.HTTPConnection("api.thingspeak.com:80")
 conn.request("POST", "/update", params, headers)
 response = conn.getresponse()
 print (cpu_pc)
 print (mem_avail_mb)
 print (strftime("%a, %d %b %Y %H:%M:%S", localtime()))
 print (response.status, response.reason)
 data = response.read()
 print ("connection failed")
 print (response.status, response.reason)
#sleep for 16 seconds (api limit of 15 secs)
 if __name__ == "__main__":
 while True:

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s