Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #----------------------------------------------------------------
- # Note:
- # ds18b20's data pin must be connected to pin7.
- # replace the 28-XXXXXXXXX as yours.
- #----------------------------------------------------------------
- import os
- import time
- import datetime
- import openpyxl
- from openpyxl.styles import Font
- os.system('modprobe w1-gpio')
- os.system('modprobe w1-therm')
- def temp_raw():
- temp_sensor = "/sys/bus/w1/devices/28-0316b38974ff/w1_slave"
- f = open(temp_sensor, 'r')
- lines = f.readlines()
- f.close()
- return lines
- def read_temp():
- lines = temp_raw()
- while lines[0].strip()[-3:] != 'YES':
- time.sleep(0.1)
- lines = temp_raw()
- temp_output = lines[1].find('t=')
- if temp_output != -1:
- temp_string = lines[1].strip()[temp_output+2:]
- temp_c = float(temp_string) / 1000.0
- return temp_c
- def percentage_change(current, previous):
- if (current > previous):
- return float(current - previous) / abs(previous) * 100
- else:
- return float(current - previous) / abs(previous) * 100 * (-1)
- def main():
- # Check current working directory.
- currentdirectory = os.getcwd()
- print ("Current working directory %s" % currentdirectory)
- # Change Directory
- if not os.path.exists('/home/pi/Logs/'):
- os.mkdir('/home/pi/Logs')
- print ("Directory not found will create one")
- os.chdir('/home/pi/Logs')
- # confirm current working directory.
- currentdirectory = os.getcwd()
- print ("Directory changed successfully %s" % currentdirectory)
- #Create Excel document
- wb = openpyxl.Workbook()
- sheet = wb.active
- sheet.title = 'TemperatureData'
- #Configure Header
- bold20Font = Font(size=20, bold=True)
- sheet['A1'].font = bold20Font
- sheet.column_dimensions['A'].width = 20
- sheet['A1'] = 'Timestamp:'
- sheet['B1'].font = bold20Font
- sheet.column_dimensions['B'].width = 12
- sheet['B1'] = 'Temp:'
- sheet.freeze_panes = 'A2'
- count = 2
- read_temp()
- while True:
- if (percentage_change(temp c, b) > 2):
- sheet["A"+str(count)] = datetime.datetime.now()
- sheet["B"+str(count)] = ("%0.1f" % read_temp())
- wb.save('TemperatureLog.xlsx')
- count = count + 1
- read_temp() = a
- print ("Temp Logged")
- else:
- print ("Temp not logged")
- #Run main Program
- while True:
- temp_raw()
- read_temp()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement