-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy paththingspeak_cpu_loop.py
More file actions
30 lines (27 loc) · 874 Bytes
/
thingspeak_cpu_loop.py
File metadata and controls
30 lines (27 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import httplib, urllib
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.urlencode({'field1': cpu_pc, 'field2': mem_avail_mb,'key':'YOURKEYHERE'})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.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"
#sleep for 16 seconds (api limit of 15 secs)
if __name__ == "__main__":
while True:
doit()
time.sleep(16)