-
Notifications
You must be signed in to change notification settings - Fork 0
/
pump.sh
executable file
·149 lines (134 loc) · 3.15 KB
/
pump.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
#
# Simple script that can be used to control pumps for an aeroponic farm
# the script toggles gpio pins on a raspberry pi
#
# default values
PUMP="8"
DURATION="180"
SOLENOID=""
function echo_log {
echo "$1" | logger
}
# get input
print_usage() {
echo "Usage: pump.sh --pump-pin <pin> --duration <seconds>"
echo ""
echo "Options:"
echo " -h, --help"
echo " Print detailed help screen"
echo " -p, --pump-pin"
echo " Which pin that controls the pump."
echo " -d, --duration"
echo " Duration the pump should run in seconds."
echo " -s, --solenoid"
echo " Which pin to open for 5 seconds in case the pump ran"
echo " dry the previous cycle."
echo ""
echo "Example:"
echo "pump.sh --pump-pin 8 --duration 120 --solenoid 9"
echo ""
}
# Make sure the correct number of command line
# arguments have been supplied
if [ $# -lt 1 ]; then
print_usage
exit 3
fi
# read input variables
while test -n "$1"; do
case "$1" in
--help)
print_usage
exit 0
;;
-h)
print_usage
exit 0
;;
--pump-pin)
PUMP=$2
shift
;;
-p)
PUMP=$2
shift
;;
--duration)
DURATION=$2
shift
;;
-d)
DURATION=$2
shift
;;
--solenoid)
SOLENOID=$2
shift
;;
-s)
SOLENOID=$2
shift
;;
*)
echo "Unknown argument: $1"
print_usage
exit 3
;;
esac
shift
done
# function for initializing pins
function gpio_initialize {
echo "$1" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$1/direction
echo "0" > /sys/class/gpio/gpio$1/value
echo_log "Pin $1 initialized"
}
# function for deinitialize pins
function gpio_deinitialize {
if [ -e /sys/class/gpio/gpio$1/value ] ; then
echo "0" > /sys/class/gpio/gpio$1/value
echo "$1" > /sys/class/gpio/unexport
echo_log "Pin $1 deinitialized"
fi
}
# toggle gpio pin on
function gpio_on {
echo "1" > /sys/class/gpio/gpio$1/value
}
# toggle gpio pin off
function gpio_off {
echo "0" > /sys/class/gpio/gpio$1/value
}
# unexpected abort
function abort {
gpio_deinitialize $PUMP
echo_log "Pump on pin $PUMP aborted"
if [ ! -z "$SOLENOID" ] ; then
gpio_deinitialize $SOLENOID
echo_log "Solenoid on pin $SOLENOID aborted"
fi
exit 255
}
trap abort SIGINT SIGTERM
# Do the work
gpio_initialize $PUMP
gpio_on $PUMP
echo_log "Pump on pin $PUMP on."
# toggle the solenoid (if set) to allow the pump to free-flow
# and "auto fix" issues when beeing ran dry.
if [ ! -z "$SOLENOID" ] ; then
echo_log "Solenoid on pin $SOLENOID on."
gpio_initialize $SOLENOID
gpio_on $SOLENOID
sleep 5
gpio_off $SOLENOID
gpio_deinitialize $SOLENOID
echo_log "Solenoid on pin $SOLENOID is off."
DURATION=$(($DURATION - 5))
fi
sleep $DURATION
gpio_off $PUMP
gpio_deinitialize $PUMP
echo_log "Pump on pin $PUMP off."