Advertisement
Guest User

Untitled

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