-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelaycontroller.sh
executable file
·146 lines (132 loc) · 3.18 KB
/
relaycontroller.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
#!/bin/bash
#title :Relaycontroller.sh
#description :This Script allows to controll a relay by a RPI GPIO pin.
#author :Fabian Hartmann
#date :2018-11-29
#version :0.1
#usage :./Relaycontroller.sh
#notes :
#bash_version :
#============================================================================
#============================================================================
### Revision History:
##
## Date Version Personnel Notes
## ---- ------- ---------------- -----
## 2018-11-29 0.1 Fabian Hartmann Script created
##
#============================================================================
#
SCRIPT_NAME=`basename "${0}"`
SCRIPT_PATH=`dirname "${0}"`
# Name of the Log File
logfilename="$SCRIPT_NAME.log"
# Log to a File if true
logtofile="false"
function mylogger() {
logtext="${@}"
if [[ -w /var/log/ ]]; then
logpath="/var/log/"
else
logpath="/Users/$loggedInUser/Library/Logs/"
fi
logfile=$logpath$logfilename
echo $logtext
if [[ $logtofile == "true" ]]; then
echo $(date "+%b %e %H:%M:%S") $SCRIPT_NAME[]: $logtext >> $logfile
logger -is -t $SCRIPT_NAME "$logtext"
fi
}
# specifies the pin.
gpiopin=$2
# specifies what should be done
function=$1
function init() {
for i in `seq 1 3`; do
if [[ ! -d /sys/class/gpio/gpio$1 ]]; then
echo "$1" > /sys/class/gpio/export
echo "GPIO PIN: $1 - initialized."
else
echo "GPIO PIN: $1 - is already initialized."
fi
sleep 0.25
if [[ -f /sys/class/gpio/gpio$1/direction ]]; then
if [[ $( cat /sys/class/gpio/gpio$1/direction ) == "out" ]]; then
echo "GPIO PIN: $1 - is already configured as output."
else
echo "out" > /sys/class/gpio/gpio$1/direction
echo "GPIO PIN: $1 - is configured as output."
fi
fi
ret_value=$?
[ $ret_value -eq 0 ] && return 2
sleep 0.5
done
}
function disable() {
if [[ -d /sys/class/gpio/gpio$1 ]]; then
echo "$1" > /sys/class/gpio/unexport
echo "GPIO PIN: $1 - disabled."
else
echo "GPIO PIN: $1 - is not enabled."
exit 1
fi
}
function on() {
for i in `seq 1 3`; do
if [[ -d /sys/class/gpio/gpio$1 ]]; then
echo "0" > /sys/class/gpio/gpio$1/value
status $1
break
else
init $1
fi
done
}
function off() {
for i in `seq 1 3`; do
if [[ -d /sys/class/gpio/gpio$1 ]]; then
echo "1" > /sys/class/gpio/gpio$1/value
status $1
break
else
init $1
fi
done
}
function status() {
for i in `seq 1 3`; do
if [[ -d /sys/class/gpio/gpio$1 ]]; then
statusvalue=$(cat /sys/class/gpio/gpio$1/value)
if [[ $statusvalue = 1 ]]; then
# Because I use the homebridge-script2 plugin, I have to return a simple value back.
#echo "GPIO PIN: $1 - Value is set to: OFF"
echo "OFF"
else
#echo "GPIO PIN: $1 - Value is set to: ON"
echo "ON"
fi
break
else
init $1
fi
sleep 0.5
done
}
case $function in
on )
on $gpiopin
;;
off )
off $gpiopin
;;
status )
status $gpiopin
;;
disable )
disable $gpiopin
;;
* )
init $gpiopin
;;
esac