Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. import os
  2. import glob
  3. import time
  4. import datetime
  5.  
  6. # initiation of each devices
  7. os.system('modprobe w1-gpio')
  8. os.system('modprobe w1-therm')
  9. base_dir = '/sys/bus/w1/devices/'
  10.  
  11. # Grabs data from each of the sensors
  12. device_folder = glob.glob(base_dir + '28-00000354947f')[0]
  13. device_file = device_folder + '/w1_slave' #file where the sensors are located
  14.  
  15. sensor1 = glob.glob(base_dir + '28-0000047ed310')[0]
  16. sensor_file = sensor1 + '/w1_slave'
  17.  
  18. #Time
  19. def time_stamp():
  20.     ts = time.time()
  21.     st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d,%H:%M:%S')
  22.     return st
  23. #grabs the data raw
  24. def read_temp_raw():
  25.     f = open(device_file, 'r')
  26.     lines = f.readlines()
  27.    
  28.     f1 = open(sensor_file, 'r')
  29.     sen1 = f1.readlines()
  30.  
  31.     f.close()
  32.     f1.close()
  33.     return lines, sen1
  34.  
  35. #interpret each data
  36. def read_temp():
  37.     lines, lines2 = read_temp_raw()
  38.     while lines[0].strip()[-3:] != 'YES':
  39.         time.sleep(0.2)
  40.         lines = read_temp_raw()
  41.     equals_pos = lines[1].find('t=')
  42.     if equals_pos != -1:
  43.         temp_string = lines[1][equals_pos+2:]
  44.         temp_c = float(temp_string) / 1000.0
  45.         temp_f = temp_c * 9.0 / 5.0 + 32.0
  46.         return temp_c, temp_f
  47.  
  48. def read_sensor2():
  49.     lines, lines2 = read_temp_raw()
  50.     while lines2[0].strip()[-3:] != 'YES':
  51.         time.sleep(0.2)
  52.         lines2 = read_temp_raw()
  53.     equals_pos = lines2[1].find('t=')
  54.     if equals_pos != -1:
  55.         temp_string = lines2[1][equals_pos+2:]
  56.         temp_c2 = float(temp_string) / 1000.0
  57.         temp_f2 = temp_c2 * 9.0 / 5.0 + 32.0
  58.         return temp_c2, temp_f2
  59.  
  60. #outputs data
  61. while True:
  62.     print('S1', read_temp())
  63.     print('S2', read_sensor2())
  64.     #log = open("temperaturelog.txt", "w")
  65.     #time = str(time_stamp())
  66.     temp = str(read_temp())
  67.     temp2 = str(read_sensor2())
  68.     data1 = str('S1 ' + time_stamp() + ' ' + temp)
  69.     data2 = str('S2 ' + time_stamp() + ' ' + temp2)
  70.     log = open('temperaturelog.txt', 'a')
  71.     log.write(data1+'\n')
  72.     log.write(data2+'\n')
  73.     log.close()
  74.     #print('S2', time_stamp(), read_sensor2())
  75.     #time.sleep(1)
  76.  
  77. #log.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement