SHARE
TWEET

(HotPi) Raspberry Pi Thermostat 0.8b

onepoint21 Jan 5th, 2013 6,604 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. #Beta 0.8b of HotPi. Thank you 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 furnace.
  17.     FURNACE = 18
  18.     GPIO.setup(FURNACE, GPIO.OUT)
  19.     GPIO.output(FURNACE, 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-00000426c4eb/w1_slave" #inside sensor
  33.     sensoutsidetemp = "/sys/bus/w1/devices/28-00000426c4eb/w1_slave" #NOT CURRENTLY CONFIGURED
  34.  
  35. # this reads the temperature and rounds the value to the nearest decimal and also does a crc check
  36.     def currtemp():
  37.         while(1):
  38.             tfile = open(sensmaintemp)
  39.             text = tfile.read()
  40.             tfile.close()
  41.             firstline  = text.split("\n")[0]
  42.             crc_check = text.split("crc=")[1]
  43.             crc_check = crc_check.split(" ")[1]
  44.             if crc_check.find("YES")>=0:
  45.                 break
  46.         secondline = text.split("\n")[1]
  47.         temperaturedata = secondline.split(" ")[9]
  48.         temperature = float(temperaturedata[2:])
  49.         temperature = temperature / 1000.0
  50.         temperature = round(temperature, 1)
  51.         return temperature
  52.  
  53. # set desired temperature using thermostat file from web server. reads reads a text file in /var/bin                
  54.     def settemp():
  55.         readtemp = open("/var/bin/thermostat", "r")
  56.         settemp = readtemp.readline(4)
  57.         readtemp.close()
  58.         return float(settemp)
  59.        
  60. # hold the temperature at the settemp. write status to file in /var/bin
  61.     def holdtemp():
  62.         # the + or - of 0.5 is a heuristic value. modify to desired setting
  63.         while currtemp() >= settemp() - 0.5:
  64.            GPIO.output(FURNACE, GPIO.HIGH)
  65.            status = open("/var/bin/status", "w+")
  66.            status.write("Furnace is off.")
  67.            status.close()
  68.            #print "Off loop", currtemp()
  69.            time.sleep(30)
  70.         else:
  71.             while currtemp() <= settemp() + 0.5:
  72.                GPIO.output(FURNACE, GPIO.LOW)
  73.                status = open("/var/bin/status", "w+")
  74.                status.write("Furnace is on.")
  75.                status.close()
  76.                #print "On loop", currtemp()
  77.                time.sleep(30)
  78.          
  79. # this constructs an infinite loop
  80.     infloop = 1
  81.     while infloop == 1 :
  82.         holdtemp()
  83.  
  84. if __name__ == '__main__':
  85.         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