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.
Steps:
-
- You will need a Thingsspeak.com account – Signup if you haven’t already
- Create a channel at Thingsspeak [https://www.thingspeak.com/channels/]
- Install Python – I got version 3.2 and the code below uses this version
- Download psutil – install psutil for python version 3.2. You will need this python utility to monitor your processes on your computer [CPU, Memory etc]
- Test your Thingsspeak REST call [ For example: http://api.thingspeak.com/update?key=<your_api_key_here>&field1=0&field2&….]
- Test Python http.client and urllib.parse and then run the code below [see http://docs.python.org/py3k/library/http.client.html]
- Result: You can see the graphs below, they represent the updates from my machine ….
CPU
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")
try:
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()
conn.close()
except:
print ("connection failed")
print (response.status, response.reason)
#sleep for 16 seconds (api limit of 15 secs)
if __name__ == "__main__":
while True:
doit()
time.sleep(16)

