Last active
October 21, 2015 20:26
-
-
Save robstwd/bd38107e46b598f64cf6 to your computer and use it in GitHub Desktop.
Thingspeak upload with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# dependency: installed python library python-psutil | |
# dependency: a library file containing the thingspeak api keys | |
# 'thingspeak_keys.py' containing eg: | |
# RASPBERRY_PI2 = "KEYABCXYZETECETC" | |
# ====================================================================== | |
import sys | |
import subprocess as sp | |
import psutil | |
import os | |
import re | |
# append the relative location to import local modules from | |
sys.path.append("/path/to/library") | |
# import my local modules | |
import thingspeak_keys | |
import thingspeak | |
# thingspeak api key & data dictionary | |
TS_KEY = thingspeak_keys.RASPBERRY_PI2 | |
thingspeak_data = {} | |
# ====================================================================== | |
TEMP_CMD = [ '/opt/vc/bin/vcgencmd', 'measure_temp' ] | |
BOOT = '/boot' | |
ROOT = '/' | |
MEM_CMD = [ 'free', '-m' ] | |
# ====================================================================== | |
def get_output(cmd): | |
''' | |
Runs a subprocess command to check its output | |
Returns a byte, therefore needs to be converted to string | |
and removal of the trailing '\n' | |
''' | |
output = sp.check_output(cmd) | |
output = output.decode(encoding='UTF-8') # convert byte to string | |
return output.strip() | |
def clean_temp(temp_string): | |
""" | |
Extracts the temperature value from the string "temp=47.1'C" | |
https://pythex.org/?regex=\Atemp%3D%28\d%2B\.\d%2B%29%27C%24&test_string=temp%3D47.1%27C&ignorecase=0&multiline=0&dotall=0&verbose=0 | |
""" | |
tmpval = re.match(r"\Atemp=(\d+\.\d+)'C$", temp_string) | |
return tmpval.group(1) | |
def get_temp(): | |
""" | |
This method returns the HD temp | |
via the command `/opt/vc/bin/vcgencmd measure_temp` | |
Returns a float | |
""" | |
tempval = get_output(TEMP_CMD) | |
return clean_temp(tempval) | |
def get_drive_used(path): | |
""" | |
Method uses the psutil module to determine the usage stats | |
psutil.disk_usage('/') returns a psutil object as follows | |
>>> sdiskusage(total=7762042880, used=2968965120, free=4380626944, percent=38.2) | |
Specifically return the percentage used value. | |
Returns a float | |
""" | |
return psutil.disk_usage(path).percent | |
def get_5min_load_avg(): | |
""" | |
Gets the 5 minute load average from the os module | |
A tuple is returned with the 3 values | |
""" | |
(one, five, fifteen) = os.getloadavg() | |
return five | |
def get_mem_percent(): | |
""" | |
Source: http://pythonhosted.org//psutil/#psutil.virtual_memory | |
Method uses the psutil module to determine the memory stats | |
psutil.disk_usage('/') returns a psutil object as follows | |
>>> svmem(total=2649165824, available=1928642560, percent=27.2, used=2007678976, free=641486848, active=975646720, inactive=912330752, buffers=86536192, cached=1200619520) | |
Specifically return the percentage used value. | |
That is, the percentage usage calculated as (total - available) / total * 100. | |
Returns a float | |
""" | |
return psutil.virtual_memory().percent | |
# ====================================================================== | |
print(":: Raspi stats......") | |
# ====================================================================== | |
# field1: read the raspi temp | |
temp = get_temp() | |
print("Raspi temperature is: {0}'C".format(temp)) | |
# ====================================================================== | |
# field2: read the used % of /boot | |
boot = get_drive_used(BOOT) | |
print("Raspi boot drive usage percentage is: {0}%".format(boot)) | |
# ====================================================================== | |
# field3: read the used % of / drive => root | |
root = get_drive_used(ROOT) | |
print("Raspi root drive usage percentage is: {0}%".format(root)) | |
# ====================================================================== | |
# field4: read the 5 min load average => load | |
load = get_5min_load_avg() | |
print("Raspi 5 min load avg is: {0}%".format(load)) | |
# ====================================================================== | |
# field5: read the memory in use => mem | |
# mem = get_mem() | |
mem = get_mem_percent() | |
print("Raspi percentage memory used is: {0}%".format(mem)) | |
# ====================================================================== | |
# field6: | |
# ====================================================================== | |
# field7: | |
# ====================================================================== | |
# field8: | |
# ====================================================================== | |
# populate the thingspeak content dictionary | |
thingspeak_data['field1'] = temp | |
thingspeak_data['field2'] = boot | |
thingspeak_data['field3'] = root | |
thingspeak_data['field4'] = load | |
thingspeak_data['field5'] = mem | |
# thingspeak_data['field6'] = | |
# thingspeak_data['field7'] = | |
# thingspeak_data['field8'] = | |
# ====================================================================== | |
# POST to thingspeak | |
print(":: Thinkspeak POST:") | |
thingspeak.post(TS_KEY, thingspeak_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this is the library file that is imported by another python script | |
# depends upon the python library 'requests' http://docs.python-requests.org/en/latest/ | |
# see 'testfile.py' for example invocation | |
THINGSPEAK_URL = 'http://api.thingspeak.com/update' | |
import requests | |
# method to post to thingspeak | |
def post(key, content): | |
# create a dictionary with the api key | |
key = {'api_key': key} | |
# add the content to be posted to the dictionary | |
payload = dict(key, **content) | |
# post to thingspeak, using the params in the dictionary | |
r = requests.post(THINGSPEAK_URL, params=payload) | |
# get the response code | |
print("Thingspeak POST response:", r.status_code) | |
# get the channel entry number | |
print("Thingspeak channel entry:", int(r.content)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment