forked from shearn89/enviropi
/
enviro.py
36 lines (29 loc) · 1.14 KB
/
enviro.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
# Based heavily on https://blog.bartbania.com/raspberry_pi/temperature-log-howto/
import rrdtool
import time, sys
from subprocess import PIPE, Popen
from envirophat import light, weather, leds
dataFile = "/home/shearna/repos/enviropi/environment.rrd"
FACTOR = 1.3
def get_environment():
data = {'boardTemp': weather.temperature(), 'atm': weather.pressure(), 'light': light.light()}
data['cpuTemp'] = get_cpu_temperature()
data['calibTemp'] = data['boardTemp'] - ((data['cpuTemp'] - data['boardTemp'])/FACTOR)
# print data
return data
def get_cpu_temperature():
process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
output, _error = process.communicate()
return float(output[output.index('=') + 1:output.rindex("'")])
if __name__ == '__main__':
print "Starting..."
while True:
try:
data = get_environment()
dataString = "N:{cpuTemp}:{boardTemp}:{calibTemp}:{atm}:{light}".format(**data)
# print dataString
rrdtool.update(dataFile, dataString)
time.sleep(60)
except KeyboardInterrupt:
print "Exiting..."
sys.exit()