Created
August 16, 2014 19:01
-
-
Save Yettimania/879371d481e8847e407c to your computer and use it in GitHub Desktop.
Raspberry Pi Data logging
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 program will be used to log data during the fermentation process. | |
The sensors will be hooked upto the raspberry pi and the data will be | |
recorded to a .txt file on timed internals. It will record temperature | |
and light as of right now. Eventually this will also contorl a Peltier | |
cooler and a fan. It will be installed on top of a fermentation chamber to | |
maintain temperatures in acceptable yeast ranges. | |
August 10, 2014 K. Snyder""" | |
#IMPORT MODULES | |
import datetime | |
import time | |
import os | |
import glob | |
#CREATE THE FILE/DIRECTORY FOR THE SENSOR | |
base_dir = '/sys/bus/w1/devices/' | |
device_folder = glob.glob(base_dir + '28*')[0] | |
device_file = device_folder + '/w1_slave' | |
'''THIS TAKES THE TIME DATA IN SECONDS AND CONVERTS IT INTO | |
YEAR MONTH DATE HOUR MINUTES SECOND''' | |
def timestamp(): | |
ts = time.time() | |
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') | |
return st | |
#GLOBAL VARIABLES THAT ARE USED IN FUNCTIONS | |
record_rate = .5 #Measured in minutes | |
'''THIS CREATES A FILE BASED ON THE BEER NAME AND CREATES | |
THE HEADER. THE USER CAN DEFINE IT AND IT WILL THEN CLOSE THE | |
DATA FILE. THE HEADERS ARE ALSO CREATED IN THIS SECTION INCLUIDNG | |
TIME TEMPERATURE LIGHT''' | |
def file_create(): | |
print 'Beer Data Logger Program version 0.1' | |
print 'Creater: Kyle Snyder\n' | |
file_name = raw_input("What beer type is this? ") | |
file_list = [file_name,'.txt'] | |
f = open(''.join(file_list),'w') | |
f.write(file_name + " " + timestamp() + '\n') | |
f.write('TIME' + '\t' + 'TEMPERATURE' + '\t' + 'LUX\n') | |
f.close() | |
print 'Data file created...' | |
print 'Data recording!!!' | |
return ''.join(file_list) | |
#THIS FUNCTION WILL READ THE RAW DATA FROM THE SENSOR. IT WILL | |
#THEN BE CONVERTED TO MEANINFUL TEMPERATURE VALUES | |
def read_temp_raw(): | |
f = open(device_file, 'r') | |
lines = f.readlines() | |
f.close() | |
return lines | |
#THIS ACTUALLY READS THE TEMP AND CONVERTS IT | |
def read_temp(): | |
lines = read_temp_raw() | |
while lines[0].strip()[-3:] != 'YES': | |
time.sleep(0.2) | |
lines = read_temp_raw() | |
equals_pos = lines[1].find('t=') | |
if equals_pos != -1: | |
temp_string = lines[1][equals_pos+2:] | |
temp_c = float(temp_string) / 1000.0 | |
temp_f = temp_c * 9.0 / 5.0 + 32.0 | |
return "%.2f" % temp_f | |
#THIS WILL ACTUALLY RECORD THE DATA FROM THE SENSORS TO THE FILE | |
#IN TAB FORM. | |
def record_data(record_rate): | |
start_time = time.time() | |
while(True): | |
new_time = time.time() | |
if ((new_time-start_time)/60 > record_rate): | |
f = open(file_name,'a') | |
f.write(timestamp() + '\t' + str(read_temp()) + '\t' + '100' + '\n') | |
f.close() | |
print timestamp() + '\t' + str(read_temp()) + '\t' + '100' | |
start_time = new_time | |
while True: | |
file_name = file_create() | |
record_data(record_rate) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment