Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import RPi.GPIO as GPIO
  4. import time,sys,os,Adafruit_DHT
  5. import sqlite3 as lite
  6.  
  7. ### config
  8. STAGE="veg" # or "flower"
  9.  
  10. if STAGE == "veg":
  11.     dawn=17 # lights on
  12.     dusk=13 # lights off
  13.     temphigh=29
  14.     templow=15
  15.  
  16. if STAGE == "flower":
  17.     dawn=15
  18.     dusk=11
  19.     temphigh=27
  20.     templow=15
  21.  
  22. ### is testing?
  23. if (os.path.basename(__file__)[0:4]) == "test":
  24.     testing = True
  25. else:
  26.     testing = False
  27.  
  28.  
  29. ### do dat GPIO
  30. GPIO.setmode(GPIO.BCM)
  31. GPIO.setwarnings(False) # otherwise it whines about re-setting-up channels
  32.  
  33. # setup pins for stuff here
  34. GPIO.setup(17, GPIO.OUT)
  35. GPIO.setup(22, GPIO.OUT)
  36. GPIO.setup(27, GPIO.OUT)
  37.  
  38. # utility functions
  39. def switchRelay(pin,state):
  40.     if testing == False:
  41.         # check pin state, if not equal, switch
  42.         curstate = GPIO.input(pin)
  43.         if curstate != state:
  44.             GPIO.output(pin,state)
  45.  
  46. def DayOrNight():
  47.     now = int(time.strftime("%H",time.localtime()))
  48.     if now >= dawn or now < dusk:
  49.             return("day")
  50.     else:
  51.         return("night")
  52.  
  53. rightnow = DayOrNight()
  54.  
  55. ### read temps
  56. def read_temp(sensor):
  57.     rtemp = read_sensor(sensor)
  58.     # sometimes the sensor returns 85. this is wrong.
  59.     while rtemp > 60:
  60.         rtemp = read_sensor(sensor)
  61.     return(rtemp)
  62.  
  63. def read_sensor(sensor):
  64.         sensorpath="/sys/bus/w1/devices/" + sensor + "/w1_slave"
  65.         tfile = open(sensorpath)
  66.         text = tfile.read()
  67.         tfile.close()
  68.         temp = text.split("\n")[1].split(" ")[9]
  69.         temp = float(temp[2:])/1000
  70.     return(temp)
  71.  
  72. # temp wall
  73. temp_wall_id = "28-0115158265ff"
  74. temp_wall = read_temp(temp_wall_id)
  75.  
  76. # temp stem
  77. temp_stem_id = "28-0115526b3eff"
  78. temp_stem = read_temp(temp_stem_id)
  79.  
  80. # this might need weighting in one direction or another. like (2xwall+stem)/3
  81. temp_avg = (temp_stem + temp_wall)/2
  82.  
  83. # temp water
  84. temp_water_id = "28-0115159df3ff"
  85. temp_water = read_temp(temp_water_id)
  86.  
  87. # temp outside
  88. temp_outside_id = "28-011515ada1ff"
  89. temp_outside = read_temp(temp_outside_id)
  90.  
  91. # temp indoors
  92. temp_indoors_id = "28-0215526a4aff"
  93. temp_indoors = read_temp(temp_indoors_id)
  94.  
  95. ### read humidity
  96. # Try to grab a sensor reading.
  97. def readHumidity():
  98.     # Adafruit_DHT.read_retry(sensortype, pin)
  99.     humidity, temperature = Adafruit_DHT.read_retry(22, 23)
  100.     return humidity
  101.  
  102. humidity = None
  103. # retry if the result is stupid. Sometimes it's still stupid but less so.
  104. #while humidity is None or humidity > 100 or humidity < 40:
  105. #   humidity = readHumidity()
  106.  
  107. # humidity = round(humidity,2)
  108. # humidity sensor is currently broken
  109. humidity = 33
  110.  
  111. ### on and off and on and off again
  112.  
  113. ## some Smart is needed here. If it's night, we should aim for templow plus a bit. If it's day, aim for temphigh
  114. midtemp = (temphigh + templow)/1.8
  115.  
  116. if rightnow == "day":
  117.     # allow for some rampup time on the temp when the lights first come on. No sense in heating when it's not needed.
  118.     hournow = int(time.strftime("%H",time.localtime()))
  119.     if temp_avg < midtemp:
  120.         if hournow > dawn or hournow < dusk - 5:
  121.             heatstate = "on"
  122.         else:
  123.             heatstate = "off"
  124.     else:
  125.         heatstate = "off"
  126.  
  127.     if temp_avg >= temphigh:
  128.         fanstate = "on"
  129.     else:
  130.         fanstate = "off"
  131.  
  132. else:
  133.     if temp_avg > templow:
  134.         fanstate = "on"
  135.     else:
  136.         fanstate = "off"
  137.  
  138.     if temp_avg < templow:
  139.         heatstate = "on"
  140.     else:
  141.         heatstate = "off"
  142.  
  143.  
  144. # turn on/off fan if needed
  145. if fanstate == "on":
  146.     switchRelay(27,0)
  147. else:
  148.     switchRelay(27,1)
  149.  
  150. # heater control
  151. if heatstate == "on":
  152.     switchRelay(22,1)
  153. else:
  154.     switchRelay(22,0)
  155.  
  156.  
  157. # turn on/off lights if needed
  158.  
  159. ## allow for lights off override
  160. lightstate = "default"
  161. to = "/tmp/timeoff"
  162. if os.path.isfile(to):
  163.         execfile(to)
  164.         if timeoff > time.time():
  165.         lightstate = "tempoff"
  166.  
  167. if rightnow == "day" and lightstate != "tempoff":
  168.     lightstate = "on"
  169.     switchRelay(17,0)
  170. else:
  171.     lightstate = "off"
  172.     switchRelay(17,1)
  173.  
  174.  
  175. ### log all the things
  176. 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()))) + ")"
  177.  
  178.  
  179. ### only do a thing if we're not in test mode
  180. if testing == True:
  181.     print(ins)
  182.     exit()
  183. else:
  184.     con = lite.connect("/home/pi/webroot/temps.db")
  185.     with con:
  186.         cur = con.cursor()
  187.         cur.execute(ins)
  188.     # update the web graph
  189.     # currently not needed due to change of graphing stuff
  190.     # os.system("/home/pi/webroot/db-to-csv.py")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement