Guest User

temp log

a guest
Dec 20th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. # 'thermcron.sh'
  2. # thermo cron job script
  3.  
  4. #!/bin/sh
  5. python /root/thermo.py >> /root/hulk_log.csv
  6.  
  7.  
  8.  
  9. # 'thermo.py'
  10. # script to get temp, convert to fahrenheit.
  11. # output is '2015-12-20 17:04:02,83.75'
  12.  
  13. import os
  14. import time
  15. import sys
  16. import datetime
  17.  
  18. os.system('modprobe w1-gpio')
  19. os.system('modprobe w1-therm')
  20.  
  21. temp_sensor='/sys/bus/w1/devices/28-000006c83b77/w1_slave' # replace 28-000006c83b77 with serial of your DS18b20 temp sensor
  22. ts = time.time()
  23. st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
  24.  
  25. def temp_raw():
  26.  
  27.     f = open(temp_sensor, 'r')
  28.     lines = f.readlines()
  29.     f.close()
  30.     return lines
  31.  
  32.  
  33. def read_temp():
  34.  
  35.     lines = temp_raw()
  36.     while lines[0].strip()[-3:] != 'YES':
  37.         time.sleep(0.2)
  38.         lines = temp_raw()
  39.  
  40.     temp_output = lines[1].find('t=')
  41.  
  42.     if temp_output != -1:
  43.         temp_string = lines[1].strip()[temp_output+2:]
  44.         temp_c = float(temp_string) / 1000.0      # defines temp in celcius, in xx.xxxx format
  45.         temp_f = temp_c * 9.0 / 5.0 + 32.0        # defines temp in fahrenheit, with respect to temp_c
  46.         return temp_f
  47.  
  48.  
  49.  
  50. #while True:
  51. #    print(read_temp()) ;   print(ts)
  52. #    time.sleep(1)
  53. #sys.exit(0)
  54.  
  55. if True:
  56.     print(st + "," + str(read_temp()))   #output is 2015-12-20 17:04:02,83.75
Add Comment
Please, Sign In to add comment