Guest User

Untitled

a guest
Jun 1st, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import os
  3. import glob
  4. import datetime
  5. import time
  6. import RPi.GPIO as GPIO
  7.  
  8. control_pin = 6 # broadcom pin 4
  9.  
  10. def F_to_C(t):
  11.     return (t-32)/1.8
  12.  
  13. min_temperature = F_to_C(67.0)
  14. max_temperature = F_to_C(68.0)
  15. heater_on = False
  16. heater_change_ts = 0
  17. heater_hysteresis = 5 * 60  # five minutes
  18.  
  19. os.system('modprobe w1-gpio')
  20. os.system('modprobe w1-therm')
  21.  
  22. base_dir = '/sys/bus/w1/devices/'
  23. device_folder = glob.glob(base_dir + '28*')[0]
  24. device_file = device_folder + '/w1_slave'
  25.  
  26. def update_setting(t):
  27.     global heater_on, heater_change_ts, heater_hysteresis
  28.     global min_temperature, max_temperature
  29.  
  30.     want_on = True if t <= min_temperature else heater_on
  31.     if t >= max_temperature: want_on = False
  32.  
  33.     if want_on != heater_on:
  34.         now = unix_ts()
  35.         date_time = ymdhms(now)
  36.         if now - heater_change_ts < heater_hysteresis:
  37.             print(date_time, 'Heater changed too recently...not changing')
  38.         else:
  39.             heater_on = want_on
  40.             heater_change_ts = now
  41.             set_heater_on(heater_on)
  42.             print(date_time, 'Heater on' if heater_on else 'Heater off')
  43.  
  44. def read_temperature_raw():
  45.     f = open(device_file, 'r')
  46.     lines = f.readlines()
  47.     f.close()
  48.     return lines
  49.  
  50. def read_temperature():
  51.     lines = read_temperature_raw()
  52.     while lines[0].strip()[-3:] != 'YES':
  53.         time.sleep(0.2)
  54.         lines = read_temperature_raw()
  55.     equals_pos = lines[1].find('t=')
  56.     if equals_pos != -1:
  57.         temp_string = lines[1][equals_pos+2:]
  58.         temp_c = float(temp_string) / 1000.0
  59.         return temp_c, time.time()
  60.     else:
  61.         return 0, 0
  62.  
  63. def unix_ts():
  64.     return int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp())
  65.  
  66. def ymdhms(ts):
  67.     return datetime.datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
  68.  
  69. def init_control():
  70.     global control_pin
  71.     GPIO.setmode(GPIO.BCM)
  72.     GPIO.setwarnings(False)
  73.     GPIO.setup(control_pin, GPIO.OUT)
  74.     GPIO.output(control_pin, GPIO.LOW)
  75.  
  76. def set_heater_on(want_on):
  77.     GPIO.output(control_pin, GPIO.HIGH if want_on else GPIO.LOW)
  78.  
  79. def read_range():
  80.     with open("range.txt") as f:
  81.         low = (float(f.readline()) - 32)/1.8;
  82.         high = (float(f.readline()) - 32)/1.8;
  83.     return low, high
  84.  
  85. def mainloop():
  86.     global min_temperature, max_temperature, heater_on
  87.     error_count = 0
  88.     while True:
  89.         temperature, temperature_ts = read_temperature()
  90.         if temperature_ts == 0:
  91.             error_count += 1
  92.             if error_count > 20:
  93.                 set_heater_on(False)
  94.                 return
  95.             time.sleep(60)
  96.         else:
  97.             error_count = 0
  98.             print(temperature, temperature_ts, min_temperature, max_temperature, heater_on)
  99.             update_setting(temperature)
  100.             time.sleep(60)
  101.             min_temperature, max_temperature = read_range()
  102.  
  103. init_control()
  104. try:
  105.     mainloop()
  106. except Exception as e:
  107.     print('Exception', e)
  108.     set_heater_on(False)
  109.     GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment