daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #----------------------------------------------------------------
  4. #   Note:
  5. #       ds18b20's data pin must be connected to pin7.
  6. #       replace the 28-XXXXXXXXX as yours.
  7. #----------------------------------------------------------------
  8. import os
  9. import time
  10. import datetime
  11. import openpyxl
  12. from openpyxl.styles import Font
  13. os.system('modprobe w1-gpio')
  14. os.system('modprobe w1-therm')
  15.  
  16. def temp_raw():
  17.     temp_sensor = "/sys/bus/w1/devices/28-0316b38974ff/w1_slave"
  18.     f = open(temp_sensor, 'r')
  19.     lines = f.readlines()
  20.     f.close()
  21.     return lines
  22.    
  23. def read_temp():
  24.     lines = temp_raw()
  25.     while lines[0].strip()[-3:] != 'YES':
  26.         time.sleep(0.1)
  27.         lines = temp_raw()
  28.     temp_output = lines[1].find('t=')
  29.     if temp_output != -1:
  30.     temp_string = lines[1].strip()[temp_output+2:]
  31.     temp_c = float(temp_string) / 1000.0
  32.     return temp_c
  33.    
  34. def percentage_change(current, previous):
  35.     if (current > previous):
  36.         return float(current - previous) / abs(previous) * 100
  37.     else:
  38.         return float(current - previous) / abs(previous) * 100 * (-1)
  39.                
  40. def main():
  41.     # Check current working directory.
  42.     currentdirectory = os.getcwd()
  43.     print ("Current working directory %s" % currentdirectory)
  44.     # Change Directory
  45.     if not os.path.exists('/home/pi/Logs/'):
  46.         os.mkdir('/home/pi/Logs')
  47.         print ("Directory not found will create one")
  48.     os.chdir('/home/pi/Logs')
  49.     # confirm current working directory.
  50.     currentdirectory = os.getcwd()
  51.     print ("Directory changed successfully %s" % currentdirectory)
  52.    
  53.     #Create Excel document
  54.     wb = openpyxl.Workbook()
  55.     sheet = wb.active
  56.     sheet.title = 'TemperatureData'
  57.    
  58.     #Configure Header
  59.     bold20Font = Font(size=20, bold=True)
  60.     sheet['A1'].font = bold20Font
  61.     sheet.column_dimensions['A'].width = 20
  62.     sheet['A1'] = 'Timestamp:'
  63.     sheet['B1'].font = bold20Font
  64.     sheet.column_dimensions['B'].width = 12
  65.     sheet['B1'] = 'Temp:'
  66.     sheet.freeze_panes = 'A2'
  67.    
  68.     count = 2
  69.     read_temp()
  70.     while True:
  71.         if (percentage_change(temp c, b) > 2):
  72.             sheet["A"+str(count)] = datetime.datetime.now()
  73.             sheet["B"+str(count)] = ("%0.1f" % read_temp())
  74.             wb.save('TemperatureLog.xlsx')
  75.             count = count + 1
  76.             read_temp() = a
  77.             print ("Temp Logged")
  78.         else:
  79.             print ("Temp not logged")
  80.  
  81.            
  82.  
  83. #Run main Program
  84. while True:
  85.         temp_raw()
  86.         read_temp()
  87.         main()
RAW Paste Data
Top