Skip to content

Instantly share code, notes, and snippets.

@gsampallo gsampallo/stats.py
Created Oct 20, 2019

Embed
What would you like to do?
Raspberry Pi System Stats
#
# Raspberry Pi System Stats
# Save temperature, cpu usage and others to ThingSpeak channel
# Check the article: https://www.gsampallo.com/blog/2019/10/20/estadisticas-de-sistemas-de-raspberry-pi/
#
import subprocess
import json
import requests
p = subprocess.Popen(['iostat','-c','-o','JSON'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = p.communicate()
data = json.loads(out)
system = data['sysstat']['hosts'][0]['statistics'][0]['avg-cpu']['system']
idle = data['sysstat']['hosts'][0]['statistics'][0]['avg-cpu']['idle']
p = subprocess.Popen(['vcgencmd', 'measure_temp'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = p.communicate()
temp = out[5:9]
print system,idle,temp
#Replace with your own API Key
apiKey = "XXXXXXXXXXX"
url = "https://api.thingspeak.com/update?api_key"+apiKey+"&field1="+str(temp)+"&field2="+str(system)+"&field3="+str(idle)
r = requests.get(url)
print r.status_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.