-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_rrd.py
72 lines (58 loc) · 2.36 KB
/
update_rrd.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
import sys
import time
import random
import rrdtool
import urllib2
import json
import ConfigParser, os
temperature = 0
pressure = 0
YOURDEVICEID = ""
ACCESS_TOKEN = ""
def read_metric(metric):
reading = None
try:
http_response = urllib2.urlopen('https://api.spark.io/v1/devices/' + YOURDEVICEID + '/' + metric + '?access_token=' + ACCESS_TOKEN)
html_output = http_response.read()
json_raw = json.loads(html_output)
reading = json_raw['result']
except urllib2.URLError, e:
reading = None
print "Unable to retrieve reading: ", e
return reading
def update_rrd(temperature, pressure):
ret = rrdtool.update('climate.rrd','--template','temperature:pressure','N:' + str(temperature) + ':' + str(pressure));
if ret:
print rrdtool.error()
def update_rrd_dht22(dht22temp, humidity, dewpoint):
ret = rrdtool.update('climate.rrd','--template','dht22temperature:humidity:dewpoint', 'N:' + str(dht22temp) + ':' + str(humidity) + ':' + str(dewpoint));
if ret:
print rrdtool.error()
def update_rrd_all(temperature, pressure, dht22temp, humidity, dewpoint, light):
ret = rrdtool.update('climate.rrd','--template','temperature:pressure:dht22temperature:humidity:dewpoint:light', 'N:' + str(temperature) + ':' + str(pressure) + ':' + str(dht22temp) + ':' + str(humidity) + ':' + str(dewpoint) + ":" + str(light));
if ret:
print rrdtool.error()
# main class if this file is used directly
if __name__ == '__main__':
# read config information from file
config = ConfigParser.ConfigParser()
config.readfp(open('spark.cfg'))
YOURDEVICEID = config.get('SparkCloud', 'deviceID')
ACCESS_TOKEN = config.get('SparkCloud', 'accessToken')
# print out config information
print "Config DeviceID: " + YOURDEVICEID
print "Config Access Token: " + ACCESS_TOKEN
temperature = read_metric("temperature")
pressure = read_metric("pressure")
dht22temp = read_metric("dht22temp")
humidity = read_metric("humidity")
dewpoint = read_metric("dewpoint")
light = read_metric("light")
print "Temperature: " + str(round(temperature,2)) + " C"
print "Temperature DHT22: " + str(round(dht22temp,2)) + " C"
print "Pressure: " + str(round(pressure,2)) + " hPa"
print "Humidity: " + str(round(humidity,2)) + " %"
print "Dew point: " + str(round(dewpoint,2)) + " C"
print "Light: " + str(light)
update_rrd_all(temperature, pressure, dht22temp, humidity, dewpoint, light)