Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/tmp/env python
- #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
- import os
- import re
- import RPi.GPIO as GPIO
- import time
- import math
- def main():
- GPIO.setwarnings(False)
- GPIO.cleanup()
- GPIO.setmode(GPIO.BCM)
- # set BCM pin number for the Boiler.
- BOILER = 18
- GPIO.setup(BOILER, GPIO.OUT)
- GPIO.output(BOILER, GPIO.HIGH)
- # see if the one-wire "w1" interfaces are loaded
- modfile = open("/proc/modules")
- moduletext = modfile.read()
- modfile.close()
- if not (re.search("w1_gpio", moduletext) and re.search("w1_therm", moduletext)):
- # if modules not found, install them
- os.system('sudo modprobe w1-gpio')
- os.system('sudo modprobe w1-therm')
- # define serial number for the DS18B20 temperature sensor
- sensmaintemp = "/sys/bus/w1/devices/28-00000568ddcc/w1_slave" #inside sensor
- # this reads the temperature and rounds the value to the nearest decimal and also does a crc check
- def currtemp():
- while(1):
- tfile = open(sensmaintemp)
- text = tfile.read()
- tfile.close()
- firstline = text.split("\n")[0]
- crc_check = text.split("crc=")[1]
- crc_check = crc_check.split(" ")[1]
- if crc_check.find("YES")>=0:
- break
- secondline = text.split("\n")[1]
- temperaturedata = secondline.split(" ")[9]
- temperature = float(temperaturedata[2:])
- temperature = temperature / 1000.0
- temperature = round(temperature, 1)
- return temperature
- # set desired temperature using value in "settemp = (XX)"
- def settemp():
- settemp = (67)
- return float(settemp)
- # hold the temperature at the settemp. write status to file in /var/tmp
- def holdtemp():
- # the + or - of 0.5 is a heuristic value. modify to desired setting
- while currtemp() >= settemp() + 0.5:
- GPIO.output(BOILER, GPIO.HIGH)
- status = open("/var/tmp/temperaturedata.log", "w+")
- status.write(str(time.strftime("%H:%M:%S")) + " Current Temp is " + str(currtemp()) + " Boiler is off" + "\n")
- status.close()
- #print "Off loop", currtemp()
- time.sleep(1)
- else:
- while currtemp() <= settemp() - 0.5:
- GPIO.output(BOILER, GPIO.LOW)
- status = open("/var/tmp/temperaturedata.log", "w+")
- status.write(str(time.strftime("%H:%M:%S")) + " Current Temp is " + str(currtemp()) + " Boiler is on" + "\n")
- status.close()
- #print "On loop", currtemp()
- time.sleep(1)
- # this constructs an infinite loop
- infloop = 1
- while infloop == 1 :
- holdtemp()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement