-
Notifications
You must be signed in to change notification settings - Fork 1
/
relay.sh
executable file
·68 lines (62 loc) · 1.38 KB
/
relay.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
#! /bin/bash
show_help() {
echo "Usage: set_relay [CHANNEL] [STATE]"
echo
echo "Controls relays on the specified channel."
echo
echo "CHANNEL:"
echo " CH1 Relay channel 1"
echo " CH2 Relay channel 2"
echo " CH3 Relay channel 3"
echo
echo "STATE:"
echo " ON Turns the relay on (connects NO contact)"
echo " OFF Turns the relay off (connects NC contact)"
echo
echo "Relay contacts:"
echo " NO (Normally Open) - Contact is open when the relay is off, closes when the relay is on."
echo " NC (Normally Closed) - Contact is closed when the relay is off, opens when the relay is on."
echo
echo "Examples:"
echo " set_relay CH1 ON # Turns relay channel 1 on (connects NO)"
echo " set_relay CH2 OFF # Turns relay channel 2 off (connects NC)"
}
for arg in "$@"; do
case $arg in
-h|--help)
show_help
exit 0
;;
esac
done
if [ $1 == 'CH1' ]
then
ch=538
elif [ $1 == 'CH2' ]
then
ch=532
elif [ $1 == 'CH3' ]
then
ch=533
else
echo "Parameter error"
exit
fi
if [ $2 == 'ON' ]
then
state=0
elif [ $2 == 'OFF' ]
then
state=1
else
echo "Parameter error"
exit
fi
# Unexport if already exported
if [ -d "/sys/class/gpio/gpio$ch" ]; then
echo $ch > /sys/class/gpio/unexport
fi
echo $ch > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio$ch/direction
echo $state > /sys/class/gpio/gpio$ch/value
echo Relay $1 $2