-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglerelay.sh
executable file
·74 lines (60 loc) · 1.6 KB
/
singlerelay.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
#!/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
POWERCOMMAND=$1
# Common path for all GPIO access
BASE_GPIO_PATH=/sys/class/gpio
echo "$BASE_GPIO_PATH"
# Assign names to GPIO pin numbers for each AC relay
# Relay1 is connected to GPIO 11 on header pin 23
RELAY1="11"
# Assign Device Relay
AC5160_AC5164CC="$RELAY1"
# Assign names to states
ON="1"
OFF="0"
# 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"
echo "setLightState $AC5160_AC5164CC $OFF"
setRelayState $AC5160_AC5164CC $OFF
}
# Ctrl-C handler for clean shutdown
shutdown()
{
allRelaysOff
exit 0
}
trap shutdown SIGINT
# Export pins so that we can use them
exportPin $AC5160_AC5164CC
# Set pins as outputs
setOutput $AC5160_AC5164CC
POWERCOMMAND=$1
if [ $POWERCOMMAND == "ON" ]
then
setRelayState $AC5160_AC5164CC $ON
else
setRelayState $AC5160_AC5164CC $OFF
fi