Rasp_grid/temperaturerasp.py

55 lines
1.4 KiB
Python

from time import sleep, time
import os
import datetime
import glob
import sys
from IPython import embed
def main():
# create folder and temperature file
if os.path.exists('/media/pi/data1'):
init_path = '/media/pi/data1'
else:
init_path = '/home/raab/data/rasp_test'
path_format = "%04Y-%02m-%02d-%02H_%02M"
now = datetime.datetime.now()
path = os.path.join(init_path, now.strftime(path_format))
os.makedirs(path)
temp_file = os.path.join(path, 'temperatures.csv')
# open file
temp_f = open(temp_file, 'w')
temp_f.write('%-6s; %-7s\n' % ('time/s', 'T/C'))
temp_f.flush()
# detect w1 device
w1_bus_path = glob.glob('/sys/bus/w1/devices/28*/w1_slave')
if len(w1_bus_path) > 0:
w1_bus_path = w1_bus_path[0]
else:
embed()
quit()
# set dt for temp recording
temp_t0 = time()
next_temp_t = 0
temp_interval = sys.argv[1] * 60 # sec --> 5 min
while True:
if time() - temp_t0 > next_temp_t:
w1_f = open(w1_bus_path, 'r')
w1_file = w1_f.readlines()
for line in w1_file:
if 't=' in line:
temp = float((line.split('=')[-1].strip())) / 1000
temp_f.write('%6.0f; %7.3f\n' % (next_temp_t, temp))
temp_f.flush()
break
w1_f.close()
next_temp_t += temp_interval
if __name__ == '__main__':
main()