I am building a system to remotely control a well pump. My Pi will be set up at remote water tanks and is programmed to sense what level the tanks are at (using 3 float switches) and trigger the pump on and off appropriately. I also have 3 indicator lights that will be triggered to indicate the current tank level. After sensing a change, the Pi activates a radio link to the well pump and sends a DTMF tone that is received by a DTMF relay board that does the switching of the pump and indicator lights.
My problem.... I am using event detect functions to do the sensing and triggering. I need a way for the script to auto detect the current tank level and act appropriately. For example... after a power failure, I need the pi to reboot, read its sensors and update the well's level indicators and pump control.
Redundancy is the name of the game here. I am fairly new to programming so there is some clean up to be done here.... but the biggest problem I am having is auto sensing on start up.
Thanks!
Code: Select all
import time
import RPi.GPIO as GPIO
import pygame
from subprocess import call
GPIO.setmode(GPIO.BOARD)
# 100% Pin
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# 2/3 Pin
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# 1/3 Pin
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# KILL
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def my_callback(channel):
if GPIO.input(11):
print ('Tank was FULL - Now DRAINING')
GPIO.setup(16, GPIO.OUT)
time.sleep(4)
GPIO.setup(18, GPIO.OUT)
time.sleep(1)
call(["aplay","/home/pi/Desktop/DTMF/3off.wav"])
time.sleep(2)
call(["aplay","/home/pi/Desktop/DTMF/3off.wav"])
time.sleep(1)
GPIO.setup(18, GPIO.IN)
GPIO.setup(16, GPIO.IN)
time.sleep(3)
else:
print ('Tank FULL')
GPIO.setup(16, GPIO.OUT)
time.sleep(4)
GPIO.setup(18, GPIO.OUT)
time.sleep(1)
call(["aplay","/home/pi/Desktop/DTMF/3on4off.wav"])
time.sleep(2)
call(["aplay","/home/pi/Desktop/DTMF/3on4off.wav"])
time.sleep(1)
GPIO.setup(18, GPIO.IN)
GPIO.setup(16, GPIO.IN)
time.sleep(3)
def my_callback2(channel):
if GPIO.input(13):
print ('Tank 2/3 and DRAINING')
GPIO.setup(16, GPIO.OUT)
time.sleep(4)
GPIO.setup(18, GPIO.OUT)
time.sleep(1)
call(["aplay","/home/pi/Desktop/DTMF/4on2off.wav"])
time.sleep(2)
call(["aplay","/home/pi/Desktop/DTMF/4on2off.wav"])
time.sleep(1)
GPIO.setup(18, GPIO.IN)
GPIO.setup(16, GPIO.IN)
time.sleep(3)
else:
print ('Tank 2/3')
GPIO.setup(16, GPIO.OUT)
time.sleep(4)
GPIO.setup(18, GPIO.OUT)
time.sleep(1)
call(["aplay","/home/pi/Desktop/DTMF/2on.wav"])
time.sleep(2)
call(["aplay","/home/pi/Desktop/DTMF/2on.wav"])
time.sleep(1)
GPIO.setup(18, GPIO.IN)
GPIO.setup(16, GPIO.IN)
time.sleep(3)
def my_callback3(channel):
if GPIO.input(15):
print ('Tank 1/3 and DRAINING')
GPIO.setup(16, GPIO.OUT)
time.sleep(4)
GPIO.setup(18, GPIO.OUT)
time.sleep(1)
call(["aplay","/home/pi/Desktop/DTMF/1off4on.wav"])
time.sleep(2)
call(["aplay","/home/pi/Desktop/DTMF/1off4on.wav"])
time.sleep(1)
GPIO.setup(18, GPIO.IN)
GPIO.setup(16, GPIO.IN)
time.sleep(3)
else:
print ('Tank 1/3')
GPIO.setup(16, GPIO.OUT)
time.sleep(4)
GPIO.setup(18, GPIO.OUT)
time.sleep(1)
call(["aplay","/home/pi/Desktop/DTMF/1on4on.wav"])
time.sleep(2)
call(["aplay","/home/pi/Desktop/DTMF/1on4on.wav"])
time.sleep(1)
GPIO.setup(18, GPIO.IN)
GPIO.setup(16, GPIO.IN)
time.sleep(3)
#raw_input("Press Enter when ready\n>")
GPIO.add_event_detect(11, GPIO.BOTH, callback=my_callback, bouncetime=30000)
GPIO.add_event_detect(13, GPIO.BOTH, callback=my_callback2, bouncetime=30000)
GPIO.add_event_detect(15, GPIO.BOTH, callback=my_callback3, bouncetime=30000)
try:
print('Waiting for input...')
GPIO.wait_for_edge(7, GPIO.RISING)
print('Program killed.')
except KeyboardInterrupt:
GPIO.cleanup()
finally:
GPIO.cleanup()