Advertisement
Guest User

Untitled

a guest
Jul 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. import time
  2. import os
  3. import glob
  4. import os.path
  5. import datetime
  6.  
  7. #################################################################
  8. #Setup local output file to store temp information for use in the event of the pi losing connection to the network
  9. file_name = "brew_data.txt"
  10. if (not(os.path.isfile(file_name))):
  11. tempfile = open(file_name,"w")
  12. tempfile.close
  13. with open(file_name) as f:
  14. try:
  15. saved_temp = float(f.read())
  16. except:
  17. saved_temp = None
  18.  
  19.  
  20. #################################################################
  21. #setup GPIO (pin BCM 11 for ssr use)
  22. import RPi.GPIO as GPIO
  23. GPIO.setwarnings(False)
  24. GPIO.setmode(GPIO.BCM)
  25. GPIO.setup(11,GPIO.OUT, initial=GPIO.LOW)
  26.  
  27. #################################################################
  28. #setup temp probe
  29. os.system('modprobe w1-gpio')
  30. os.system('modprobe w1-therm')
  31. base_dir = '/sys/bus/w1/devices/'
  32. device_folder = glob.glob(base_dir + '28*')[0]
  33. device_file = device_folder + '/w1_slave'
  34. def read_temp_raw():
  35. f = open(device_file, 'r')
  36. lines = f.readlines()
  37. f.close()
  38. return lines
  39. def read_temp():
  40. lines = 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. return temp_c
  49.  
  50. #credits Ian Howson @ ianhowson.com for FetchOneAssoc Func
  51. def FetchOneAssoc(cursor):
  52. data = cursor.fetchone()
  53. if data == None:
  54. return None
  55. desc = cursor.description
  56. dict = {}
  57. for (name, value) in zip(desc, data):
  58. dict[name[0]] = value
  59. return dict
  60. try:
  61.  
  62. ##################################################################
  63. #setup mysql connection to server
  64. import mysql.connector
  65. cnx = mysql.connector.connect(user='fermentation', password='beerbeer',
  66. host='192.168.1.4',
  67. database='fermentation')
  68. cursor = cnx.cursor()
  69.  
  70.  
  71. ######################################################################
  72. #program
  73.  
  74.  
  75. chamber_id = 1
  76. script_iteration_delay = 60 #how often in seconds the script is executed with cron
  77. query = ("""SELECT * FROM chambers JOIN fermentation on
  78. (chambers.active_fermentation = fermentation.f_id)
  79. WHERE chamber_id = """+str(chamber_id)+"""
  80. """)
  81.  
  82. brew_query = cursor.execute(query)
  83. brew_data = (FetchOneAssoc(cursor))
  84.  
  85.  
  86. if not(brew_data == None):
  87. #Currently a ongoing fermentation in this chamber, proceed to edit both chamber and fermentation tables
  88. target_temp = brew_data['temp_c']
  89. current_temp = read_temp()
  90. element_status = 0
  91. element_runtime = brew_data['element_active_seconds']
  92. active_ferm_id = brew_data['active_fermentation']
  93.  
  94.  
  95. current_element_status = brew_data['element_active']
  96. if (current_element_status == 1):
  97. last_conn = brew_data['last_conn']
  98. if (not(last_conn == None)):
  99. f = '%Y-%m-%d %H:%M:%S'
  100. timestamp = time.mktime(datetime.datetime.strptime(str(last_conn), "%Y-%m-%d %H:%M:%S").timetuple())
  101. current_timestamp = time.time()
  102. script_iteration_delay = int(round(current_timestamp-timestamp,0))
  103.  
  104.  
  105. f = open(file_name, 'r+')
  106. f.truncate(0)
  107. f.write(str(float(target_temp)))
  108. f.close()
  109. if (current_temp < target_temp):
  110. element_status = 1
  111. print(str(current_temp))
  112.  
  113. if (element_status == 1):
  114. GPIO.output(11,1)
  115. print("element on")
  116. element_runtime = element_runtime + script_iteration_delay
  117. else:
  118. GPIO.output(11,0)
  119. print("element off")
  120.  
  121. query = ("UPDATE chambers SET last_conn = NOW(), real_time_temp="+str(current_temp)+", element_active="+str(element_status)+" WHERE chamber_id="+str(chamber_id))
  122. cursor.execute(query)
  123. cursor.execute("UPDATE fermentation SET element_active_seconds = "+str(element_runtime)+" WHERE f_id="+str(active_ferm_id))
  124. else:
  125. #No current active fermentation for this chamber, just update chamber information
  126. chamber_query = cursor.execute(query)
  127. chamber_data = FetchOneAssoc(cursor)
  128. current_temp = read_temp()
  129. element_active = 0
  130. GPIO.output(11,0) #failsafe
  131. query = ("UPDATE chambers SET last_conn=NOW(), real_time_temp = "+str(current_temp)+", element_active=0 WHERE chamber_id="+str(chamber_id))
  132. cursor.execute(query)
  133. open(file_name,"w").close() #clear contents of file
  134. cnx.commit()
  135. cnx.close()
  136. except Exception as e:
  137. print("exception: " + str(e))
  138. #Error in processing query, clean up by checking saved file
  139. current_temp = read_temp() #get temp in C
  140. if (saved_temp == None):
  141. print("No saved temp, turn off element")
  142. GPIO.output(11,0)
  143. else:
  144.  
  145. if (saved_temp > current_temp):
  146. #turn element on
  147. print("Saved temp "+str(saved_temp))
  148. print("Current temp "+str(current_temp))
  149. print("Element on")
  150. GPIO.output(11,1)
  151. else:
  152. #turn element off
  153. print ("Saved temp "+str(saved_temp))
  154. print ("Current temp "+str(current_temp))
  155. print ("Element off")
  156. GPIO.output(11,0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement