-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadrelay.sh
executable file
·132 lines (119 loc) · 2.36 KB
/
quadrelay.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
#!/bin/bash
# Borrowed heavily from https://simonprickett.dev/controlling-raspberry-pi-gpio-pins-from-bash-scripts-traffic-lights/
# 1st time through requires running with sudo. After that sudo is not requried
# Usage quadrelay.sh <device name> < ON|OFF|>
# Example:
# acpower 3926_Water ON
#
# Operation
# assign device names to relays 1-4
# assign relaystate ON to 1 and any other state to 0
# export pin if not already exported
# set pin as an output
# set pin state
RELAY="0"
DEVICE=$1
POWERCOMMAND=$2
echo "DEVICE = $DEVICE, POWERCOMMAND = $POWERCOMMAND"
if [ $DEVICE == "4" ]
then
# GPIO 11 Pin 23
RELAY="11"
fi
if [ $DEVICE == "3926_Water" ]
then
# GPIO 11 Pin 23
RELAY="11"
fi
if [ $DEVICE == "3" ]
then
# GPIO 9 Pin 21
RELAY="9"
fi
if [ $DEVICE == "3926_Damage" ]
then
# GPIO 9 Pin 21
RELAY="9"
fi
if [ $DEVICE == "2" ]
then
# GPIO 10 Pin 21
RELAY="10"
fi
if [ $DEVICE == "5160" ]
then
# GPIO 10 Pin 21
RELAY="10"
fi
if [ $DEVICE == "1" ]
then
# GPIO 22 Pin 15
RELAY="22"
fi
if [ $DEVICE == "CC5164" ]
then
# GPIO 22 Pin 15
RELAY="22"
fi
echo "RELAY set to $RELAY"
#POWERCOMMAND=$2
if [ $POWERCOMMAND == "ON" ]
then
STATE="1"
else
STATE="0"
fi
ON="1"
OFF="0"
# Common path for all GPIO access
BASE_GPIO_PATH=/sys/class/gpio
echo "$BASE_GPIO_PATH"
# Utility function to export a pin if not already exported
exportPin()
{
echo "Utility function to export a pin $1 if not already exported"
if [ ! -e $BASE_GPIO_PATH/gpio$1 ]; then
echo "$1" > $BASE_GPIO_PATH/export
fi
}
# Utility function to set a pin as an output
setOutput()
{
echo "Utility function to set a pin $1 as an output"
echo "out" > $BASE_GPIO_PATH/gpio$1/direction
}
# Utility function to change state of a AC relay
setRelayState()
{
echo "Utility function to change state of a AC relay $1 to $2"
echo $2 > $BASE_GPIO_PATH/gpio$1/value
}
# Utility function to turn all AC relays off
allRelaysOff()
{
echo "Utility function to turn all AC relays off"
setRelayState "11" $OFF
setRelayState "9" $OFF
setRelayState "10" $OFF
setRelayState "22" $OFF
}
# Ctrl-C handler for clean shutdown
shutdown()
{
allRelaysOff
exit 0
}
trap shutdown SIGINT
# Export pins so that we can use them
exportPin "11"
exportPin "9"
exportPin "10"
exportPin "22"
if [ $RELAY == "0" ]
then
allRelaysOff
exit 0
fi
# Set pins as outputs
setOutput $RELAY
setRelayState $RELAY $STATE