├── drain.txt ├── pump.txt ├── README.md ├── FileLogger.py ├── status_Plotly.py └── pumpController.py /drain.txt: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /pump.txt: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PiPonics 2 | ======== 3 | 4 | Aquaponics with Raspberry Pi 5 | -------------------------------------------------------------------------------- /FileLogger.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import logging.handlers 3 | import time 4 | 5 | #import ntplib 6 | #import os 7 | 8 | def startLogger(filename, maxBytes, backupCount): 9 | 10 | logger = logging.getLogger(__name__) 11 | logger.setLevel(logging.INFO) 12 | 13 | # create a file handler 14 | #handler = logging.FileHandler(filename) 15 | handler = logging.handlers.RotatingFileHandler(filename, 'a', maxBytes, backupCount) 16 | handler.setLevel(logging.INFO) 17 | 18 | # create a logging format 19 | formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 20 | handler.setFormatter(formatter) 21 | 22 | # add the handlers to the logger 23 | logger.addHandler(handler) 24 | 25 | return logger 26 | 27 | def main(): 28 | 29 | global logger 30 | 31 | logger = startLogger('hello.log', 100000, 5) 32 | 33 | #logger.info("date") 34 | #logger.info(os.popen("date").read()) 35 | 36 | #logger.info("export") 37 | #logger.info(os.popen("export").read()) 38 | 39 | #logger.info("cat .profile") 40 | #logger.info(os.popen("cat .profile").read()) 41 | 42 | for i in range(60): 43 | #c = ntplib.NTPClient() 44 | #response = c.request('europe.pool.ntp.org', version=3) 45 | #print(time.ctime(response.tx_time)) 46 | #logger.info(time.ctime(response.tx_time)) 47 | 48 | print('Hello ' + str(i)) 49 | logger.info('Hello ' + str(i)) 50 | time.sleep(1) 51 | 52 | try: 53 | print x 54 | 55 | except Exception, e: 56 | logger.error('Failed to open file', exc_info=True) 57 | 58 | test() 59 | 60 | def test(): 61 | global logger 62 | 63 | try: 64 | print y 65 | except Exception, e: 66 | logger.error('Failed to open file', exc_info=True) 67 | 68 | 69 | if __name__ == '__main__': 70 | main() -------------------------------------------------------------------------------- /status_Plotly.py: -------------------------------------------------------------------------------- 1 | import os 2 | import glob 3 | import time 4 | import datetime 5 | 6 | import plotly # plotly library 7 | import json # used to parse config.json 8 | import time # timer functions 9 | 10 | os.system('modprobe w1-gpio') 11 | os.system('modprobe w1-therm') 12 | 13 | base_dir = '/sys/bus/w1/devices/' 14 | device_file_list = [glob.glob(base_dir + '28*')[0] + '/w1_slave',glob.glob(base_dir + '28*')[1] + '/w1_slave'] 15 | 16 | script_path = "/home/pi/Aquaponics/prod/" 17 | pump_file = script_path + "pump.txt" 18 | drain_file = script_path + "drain.txt" 19 | 20 | username = 'Plot.ly Username' 21 | api_key = 'API_KEY' 22 | stream_token = ['#######','######','######','######'] 23 | 24 | def initPlotly(): 25 | p = plotly.plotly(username, api_key) 26 | 27 | trace_1 = {'x': [], 'y': [], 'stream': {'token': stream_token[0], 'maxpoints': 5000}} 28 | trace_2 = {'x': [], 'y': [], 'stream': {'token': stream_token[1], 'maxpoints': 5000}} 29 | trace_3 = {'x': [], 'y': [], 'stream': {'token': stream_token[2], 'maxpoints': 5000}} 30 | trace_4 = {'x': [], 'y': [], 'stream': {'token': stream_token[3], 'maxpoints': 5000}} 31 | 32 | print p.plot([trace_1, trace_2, trace_3, trace_4], 33 | filename='Aquaponics May 15',fileopt='extend') 34 | 35 | def read_temp_raw(device_file_no): 36 | device_file_current=device_file_list[device_file_no] 37 | f = open(device_file_current, 'r') 38 | lines = f.readlines() 39 | f.close() 40 | return lines 41 | 42 | def read_temp(device_file_no): 43 | lines = read_temp_raw(device_file_no) 44 | while lines[0].strip()[-3:] != 'YES': 45 | time.sleep(0.2) 46 | lines = read_temp_raw(device_file_no) 47 | equals_pos = lines[1].find('t=') 48 | if equals_pos != -1: 49 | temp_string = lines[1][equals_pos+2:] 50 | temp_c = float(temp_string) / 1000.0 51 | temp_f = temp_c * 9.0 / 5.0 + 32.0 52 | return round(temp_f,0) 53 | 54 | initPlotly() 55 | 56 | stream_1 = plotly.stream(stream_token[0]) 57 | stream_2 = plotly.stream(stream_token[1]) 58 | stream_3 = plotly.stream(stream_token[2]) 59 | stream_4 = plotly.stream(stream_token[3]) 60 | 61 | while True: 62 | timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 63 | 64 | pump_status=0 65 | drain_status=0 66 | 67 | with open(pump_file, "r") as fo: 68 | fo.seek(0, 0) 69 | pump_status = fo.read(1) 70 | fo.closed 71 | 72 | with open(drain_file, "r") as fo: 73 | fo.seek(0, 0) 74 | drain_status = fo.read(1) 75 | fo.closed 76 | 77 | print "checking temp and posting" 78 | print timestamp + 'Temp 1:' + str(read_temp(0)) + ' Temp 2:' + str(read_temp(1)) + ' Pump:' + str(pump_status) + ' Drain:' + str(drain_status) 79 | 80 | try: 81 | stream_1.write({'x': timestamp, 'y': read_temp(0)}) 82 | stream_2.write({'x': timestamp, 'y': read_temp(1)}) 83 | stream_3.write({'x': timestamp, 'y': pump_status}) 84 | stream_4.write({'x': timestamp, 'y': drain_status}) 85 | 86 | except Exception, e: 87 | print 'Error writing to plot.ly' + str(e) 88 | initPlotly() 89 | 90 | print 'Sleeping' 91 | time.sleep(55) 92 | -------------------------------------------------------------------------------- /pumpController.py: -------------------------------------------------------------------------------- 1 | import RPi.GPIO as GPIO ## Import GPIO Library 2 | import time ## Import 'time' library. Allows us to use 'sleep' 3 | import datetime 4 | import FileLogger 5 | 6 | # Start File Logger 7 | global logger 8 | logger = FileLogger.startLogger("/var/www/pumpController.log", 5000, 5) 9 | logger.info("Starting Logger...") 10 | 11 | logger.info(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S ") + "Script Started" ) 12 | time.sleep(1) 13 | 14 | GPIO.setmode(GPIO.BCM) ## Use Broadcom pin numbering 15 | gpio_pump=24 16 | gpio_drain=18 17 | 18 | GPIO.setwarnings(False) 19 | GPIO.setup(gpio_pump, GPIO.OUT) 20 | GPIO.output(gpio_pump, True) 21 | GPIO.setup(gpio_drain, GPIO.OUT) 22 | GPIO.output(gpio_drain, True) 23 | 24 | script_path = "/home/pi/Aquaponics/PiPonics/" 25 | pump_file = script_path + "pump.txt" 26 | drain_file = script_path + "drain.txt" 27 | 28 | with open(pump_file, "r+") as fo: 29 | fo.seek(0, 0) 30 | fo.write("0") 31 | fo.closed 32 | 33 | with open(drain_file, "r+") as fo: 34 | fo.seek(0, 0) 35 | fo.write("0") 36 | fo.closed 37 | 38 | ## Define function named Blink() 39 | def pumpcycle(pump_time,hold_time,drain_time,pause_time,cycle_count): 40 | if cycle_count == 0: 41 | cycle_count=999 42 | 43 | for i in range(0,cycle_count): ## Run loop numTimes 44 | ## Pump Cycle 45 | print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Pump On for " + str(pump_time*60) 46 | logger.info(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Pump On for " + str(pump_time*60) ) 47 | GPIO.output(gpio_pump, False) ## Turn on pump 48 | with open(pump_file, "r+") as fo: 49 | fo.seek(0, 0) 50 | fo.write("1") 51 | fo.closed 52 | 53 | time.sleep(pump_time*60) ## pump timer 54 | 55 | print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Pump Off " 56 | logger.info(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Pump Off ") 57 | GPIO.output(gpio_pump, True) ## Switch off GPIO 58 | with open(pump_file, "r+") as fo: 59 | fo.seek(0, 0) 60 | fo.write("0") 61 | fo.closed 62 | 63 | 64 | print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Growbed Hold for " + str(hold_time*60) 65 | logger.info(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Growbed Hold for " + str(hold_time*60)) 66 | 67 | time.sleep(hold_time*60) ## Wait 68 | 69 | ## Drain Cycle 70 | print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Drain On for " + str(drain_time*60) 71 | logger.info(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Drain On for " + str(drain_time*60)) 72 | GPIO.output(gpio_drain, False) ## Turn on pump 73 | with open(drain_file, "r+") as fo: 74 | fo.seek(0, 0) 75 | fo.write("1") 76 | fo.closed 77 | 78 | time.sleep(drain_time*60) ## pump timer 79 | 80 | print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Drain Off " 81 | logger.info(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Drain Off " ) 82 | GPIO.output(gpio_drain, True) ## Switch off GPIO 83 | with open(drain_file, "r+") as fo: 84 | fo.seek(0, 0) 85 | fo.write("0") 86 | fo.closed 87 | 88 | print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Hold for " + str(pause_time*60) 89 | logger.info(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " Hold for " + str(pause_time*60) ) 90 | 91 | time.sleep(pause_time*60) ## Wait 92 | 93 | print "Done" ## When loop is complete, print "Done" 94 | GPIO.cleanup() 95 | 96 | ## Prompt user for input 97 | pump_time_input = raw_input("Minutes to run pump: ") 98 | hold_time_input = raw_input("Minutes to hold in grow bed: ") 99 | drain_time_input = raw_input("Minutes to drain: ") 100 | pause_time_input = raw_input("Minutes to wait for next cycle: ") 101 | cycle_count_input = raw_input("How many cycles: ") 102 | 103 | pumpcycle(float(pump_time_input),float(hold_time_input), float(drain_time_input),float(pause_time_input),int(cycle_count_input)) 104 | --------------------------------------------------------------------------------