SHARE
TWEET

Boiler Controller version of HotPi

a guest Aug 26th, 2014 610 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/tmp/env python
  2. #This is based on Beta 0.8b of HotPi. Thank you to David for posting the code for HotPi and therefore to all the people who have contributed in the RPi forums
  3.  
  4. import os
  5. import re
  6. import RPi.GPIO as GPIO
  7. import time
  8. import math
  9.  
  10. def main():
  11.  
  12.     GPIO.setwarnings(False)
  13.     GPIO.cleanup()
  14.     GPIO.setmode(GPIO.BCM)
  15.  
  16. # set BCM pin number for the Boiler.
  17.     BOILER = 18
  18.     GPIO.setup(BOILER, GPIO.OUT)
  19.     GPIO.output(BOILER, GPIO.HIGH)
  20.  
  21. # see if the one-wire "w1" interfaces are loaded
  22.     modfile = open("/proc/modules")
  23.     moduletext = modfile.read()
  24.     modfile.close()
  25.     if not (re.search("w1_gpio", moduletext) and re.search("w1_therm", moduletext)):
  26.  
  27. # if modules not found, install them
  28.         os.system('sudo modprobe w1-gpio')
  29.         os.system('sudo modprobe w1-therm')
  30.  
  31. # define serial number for the DS18B20 temperature sensor
  32.     sensmaintemp = "/sys/bus/w1/devices/28-00000568ddcc/w1_slave" #inside sensor
  33.  
  34. # this reads the temperature and rounds the value to the nearest decimal and also does a crc check
  35.     def currtemp():
  36.         while(1):
  37.             tfile = open(sensmaintemp)
  38.             text = tfile.read()
  39.             tfile.close()
  40.             firstline  = text.split("\n")[0]
  41.             crc_check = text.split("crc=")[1]
  42.             crc_check = crc_check.split(" ")[1]
  43.             if crc_check.find("YES")>=0:
  44.                 break
  45.         secondline = text.split("\n")[1]
  46.         temperaturedata = secondline.split(" ")[9]
  47.         temperature = float(temperaturedata[2:])
  48.         temperature = temperature / 1000.0
  49.         temperature = round(temperature, 1)
  50.         return temperature
  51.  
  52. # set desired temperature using value in "settemp = (XX)"                
  53.     def settemp():
  54.         settemp = (67)
  55.         return float(settemp)
  56.        
  57. # hold the temperature at the settemp. write status to file in /var/tmp
  58.     def holdtemp():
  59.         # the + or - of 0.5 is a heuristic value. modify to desired setting
  60.         while currtemp() >= settemp() + 0.5:
  61.            GPIO.output(BOILER, GPIO.HIGH)
  62.            status = open("/var/tmp/temperaturedata.log", "w+")
  63.            status.write(str(time.strftime("%H:%M:%S")) + " Current Temp is " + str(currtemp()) + " Boiler is off" + "\n")
  64.            status.close()
  65.            #print "Off loop", currtemp()
  66.            time.sleep(1)
  67.         else:
  68.             while currtemp() <= settemp() - 0.5:
  69.                GPIO.output(BOILER, GPIO.LOW)
  70.                status = open("/var/tmp/temperaturedata.log", "w+")
  71.                status.write(str(time.strftime("%H:%M:%S")) + " Current Temp is " + str(currtemp()) + " Boiler is on" + "\n")
  72.                status.close()
  73.                #print "On loop", currtemp()
  74.                time.sleep(1)
  75.          
  76. # this constructs an infinite loop
  77.     infloop = 1
  78.     while infloop == 1 :
  79.         holdtemp()
  80.  
  81. if __name__ == '__main__':
  82.         main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top