Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import glob
- import time
- import datetime
- def time_stamp():
- ts = time.time()
- st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d,%H:%M:%S')
- return st
- # initiation of each devices
- os.system('modprobe w1-gpio')
- os.system('modprobe w1-therm')
- base_dir = '/sys/bus/w1/devices/'
- # Grabs data from each of the sensors
- device_folder = glob.glob(base_dir + '28-00000354947f')[0]
- device_file = device_folder + '/w1_slave' #file where the sensors are located
- sensor1 = glob.glob(base_dir + '28-0000047ed310')[0]
- sensor_file = sensor1 + '/w1_slave'
- #Data into Text File
- #log = open("/ram/temperaturelog.txt", "w")
- #datalog = log.read()
- #grabs the data raw
- def read_temp_raw():
- f = open(device_file, 'r')
- lines = f.readlines()
- f1 = open(sensor_file, 'r')
- sen1 = f1.readlines()
- f.close()
- f1.close()
- return lines, sen1
- #interpret each data
- def read_temp():
- lines, lines2 = 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 temp_c, temp_f
- def read_sensor2():
- lines, lines2 = read_temp_raw()
- #while lines2[0].strip()[-3:] != 'YES':
- # time.sleep(0.2)
- # lines2 = read_temp_raw()
- equals_pos = lines2[1].find('t=')
- if equals_pos != -1:
- temp_string = lines2[1][equals_pos+2:]
- temp_c2 = float(temp_string) / 1000.0
- temp_f2 = temp_c2 * 9.0 / 5.0 + 32.0
- return temp_c2, temp_f2
- #outputs data
- while True:
- #print('S1', read_temp())
- #print('S2', read_sensor2())
- log = open("/ram/temperaturelog.txt", "a")
- #time = str(time_stamp())
- temp = str(read_temp())
- temp2 = str(read_sensor2())
- data1 = str('S1 ' + time_stamp() + ' ' + temp)
- data2 = str('S2 ' + time_stamp() + ' ' + temp2)
- #log = open('temperaturelog.txt', 'a')
- log.write(data1+'\n')
- log.write(data2+'\n')
- log.close()
- #print('S2', time_stamp(), read_sensor2())
- #time.sleep(1)
- #log.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement