Skip to content

Instantly share code, notes, and snippets.

@gsampallo
Created October 20, 2019 13:04
Show Gist options
  • Save gsampallo/d19f2c910dd30f5f1dd83e464a29a01f to your computer and use it in GitHub Desktop.
Save gsampallo/d19f2c910dd30f5f1dd83e464a29a01f to your computer and use it in GitHub Desktop.
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