Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import RPi.GPIO as GPIO
- import time,sys,os,Adafruit_DHT
- import sqlite3 as lite
- ### config
- STAGE="veg" # or "flower"
- if STAGE == "veg":
- dawn=17 # lights on
- dusk=13 # lights off
- temphigh=29
- templow=15
- if STAGE == "flower":
- dawn=15
- dusk=11
- temphigh=27
- templow=15
- ### is testing?
- if (os.path.basename(__file__)[0:4]) == "test":
- testing = True
- else:
- testing = False
- ### do dat GPIO
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False) # otherwise it whines about re-setting-up channels
- # setup pins for stuff here
- GPIO.setup(17, GPIO.OUT)
- GPIO.setup(22, GPIO.OUT)
- GPIO.setup(27, GPIO.OUT)
- # utility functions
- def switchRelay(pin,state):
- if testing == False:
- # check pin state, if not equal, switch
- curstate = GPIO.input(pin)
- if curstate != state:
- GPIO.output(pin,state)
- def DayOrNight():
- now = int(time.strftime("%H",time.localtime()))
- if now >= dawn or now < dusk:
- return("day")
- else:
- return("night")
- rightnow = DayOrNight()
- ### read temps
- def read_temp(sensor):
- rtemp = read_sensor(sensor)
- # sometimes the sensor returns 85. this is wrong.
- while rtemp > 60:
- rtemp = read_sensor(sensor)
- return(rtemp)
- def read_sensor(sensor):
- sensorpath="/sys/bus/w1/devices/" + sensor + "/w1_slave"
- tfile = open(sensorpath)
- text = tfile.read()
- tfile.close()
- temp = text.split("\n")[1].split(" ")[9]
- temp = float(temp[2:])/1000
- return(temp)
- # temp wall
- temp_wall_id = "28-0115158265ff"
- temp_wall = read_temp(temp_wall_id)
- # temp stem
- temp_stem_id = "28-0115526b3eff"
- temp_stem = read_temp(temp_stem_id)
- # this might need weighting in one direction or another. like (2xwall+stem)/3
- temp_avg = (temp_stem + temp_wall)/2
- # temp water
- temp_water_id = "28-0115159df3ff"
- temp_water = read_temp(temp_water_id)
- # temp outside
- temp_outside_id = "28-011515ada1ff"
- temp_outside = read_temp(temp_outside_id)
- # temp indoors
- temp_indoors_id = "28-0215526a4aff"
- temp_indoors = read_temp(temp_indoors_id)
- ### read humidity
- # Try to grab a sensor reading.
- def readHumidity():
- # Adafruit_DHT.read_retry(sensortype, pin)
- humidity, temperature = Adafruit_DHT.read_retry(22, 23)
- return humidity
- humidity = None
- # retry if the result is stupid. Sometimes it's still stupid but less so.
- #while humidity is None or humidity > 100 or humidity < 40:
- # humidity = readHumidity()
- # humidity = round(humidity,2)
- # humidity sensor is currently broken
- humidity = 33
- ### on and off and on and off again
- ## some Smart is needed here. If it's night, we should aim for templow plus a bit. If it's day, aim for temphigh
- midtemp = (temphigh + templow)/1.8
- if rightnow == "day":
- # allow for some rampup time on the temp when the lights first come on. No sense in heating when it's not needed.
- hournow = int(time.strftime("%H",time.localtime()))
- if temp_avg < midtemp:
- if hournow > dawn or hournow < dusk - 5:
- heatstate = "on"
- else:
- heatstate = "off"
- else:
- heatstate = "off"
- if temp_avg >= temphigh:
- fanstate = "on"
- else:
- fanstate = "off"
- else:
- if temp_avg > templow:
- fanstate = "on"
- else:
- fanstate = "off"
- if temp_avg < templow:
- heatstate = "on"
- else:
- heatstate = "off"
- # turn on/off fan if needed
- if fanstate == "on":
- switchRelay(27,0)
- else:
- switchRelay(27,1)
- # heater control
- if heatstate == "on":
- switchRelay(22,1)
- else:
- switchRelay(22,0)
- # turn on/off lights if needed
- ## allow for lights off override
- lightstate = "default"
- to = "/tmp/timeoff"
- if os.path.isfile(to):
- execfile(to)
- if timeoff > time.time():
- lightstate = "tempoff"
- if rightnow == "day" and lightstate != "tempoff":
- lightstate = "on"
- switchRelay(17,0)
- else:
- lightstate = "off"
- switchRelay(17,1)
- ### log all the things
- ins = "INSERT INTO templog (temp1,temp2,temp3,temp4,temp5,humid,fanstate,lightstate,heatstate,time) VALUES (" + str(temp_stem) + "," + str(temp_wall) + "," + str(temp_water) + "," + str(temp_outside) + "," + str(temp_indoors) + "," + str(humidity) + ",'" + fanstate + "','" + lightstate + "','" + heatstate + "'," + str(int(round(time.time()))) + ")"
- ### only do a thing if we're not in test mode
- if testing == True:
- print(ins)
- exit()
- else:
- con = lite.connect("/home/pi/webroot/temps.db")
- with con:
- cur = con.cursor()
- cur.execute(ins)
- # update the web graph
- # currently not needed due to change of graphing stuff
- # os.system("/home/pi/webroot/db-to-csv.py")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement