forked from haboque/Raspberry-Pi-Web-GPIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPIOServer.sh
executable file
·101 lines (86 loc) · 2.83 KB
/
GPIOServer.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
#!/bin/bash
{
# Initial Script created by Daniel Curzon (http://www.instructables.com/member/drcurzon).
# Initial version created 10th June 2012.
# Initial Version: 1.0.
# Heavily modified script by linwiz.
# January 7th 2015
# Set working directory.
dir="$(dirname "$0")"
# Read config file (relative).
source "$dir/GPIOServer.conf.sh"
# Retrieve revision information.
rev_cmd="python $dir/revision.py"
revision=`$rev_cmd`
addLogItem() {
logdatas="$1 $2 $3"
echo "INSERT INTO log (data) VALUES (\"$logdatas\");" | mysql --host=$mysqlhostname --user=$mysqlusername --password=$mysqlpassword $mysqldatabase;
}
addLogItem "Starting GPIO Server"
trap "addLogItem Stopping GPIO Server" EXIT
mysqlquery="mysql -B --host=$mysqlhostname --disable-column-names --user=$mysqlusername --password=$mysqlpassword $mysqldatabase"
# Retreive all pins.
pins=`echo "SELECT pinNumberBCM FROM pinRevision$revision" | $mysqlquery`
# Start Loop.
while true; do
for PIN in $pins ;
do
# Enable or Disable pins accordingly.
enabled[$PIN]=`echo "SELECT pinEnabled FROM pinRevision$revision WHERE pinNumberBCM='$PIN'" | $mysqlquery`
if [ "${enabled[$PIN]}" == "1" ]; then
if [ ! -d "/sys/class/gpio/gpio$PIN" ]
then
gpio export $PIN out
if [ "$logging" ]; then addLogItem "Enabled Pin $PIN"; fi
fi
else
if [ -d "/sys/class/gpio/gpio$PIN" ]
then
gpio unexport $PIN
if [ "$logging" ]; then addLogItem "Disabled Pin $PIN"; fi
fi
fi
# Skip disabled pins.
if [ -d "/sys/class/gpio/gpio$PIN" ]; then
# Read Pin Directions.
direction[$PIN]=`echo "SELECT pinDirection FROM pinRevision$revision WHERE pinNumberBCM='$PIN'" | $mysqlquery`
direction2=`cat /sys/class/gpio/gpio$PIN/direction`
# Read Pin Status'.
status[$PIN]=`echo "SELECT pinStatus FROM pinRevision$revision WHERE pinNumberBCM='$PIN'" | $mysqlquery`
status2=`gpio -g read $PIN`
# Change Pin Status'.
if [ "${direction[$PIN]}" != "$direction2" ]; then
if [ -n $PIN ]; then
if [ -n ${direction[$PIN]} ]; then
gpio -g write $PIN ${direction[$PIN]}
if [ "$logging" ]; then
addLogItem "Pin $PIN direction to: ${direction[$PIN]}"
fi
elif [ -z ${direction[$PIN]} ]; then
addLogItem "PIN direction zero"
fi
elif [ -z $PIN ]; then
addLogItem "PIN value zero"
fi
fi
if [ "${status[$PIN]}" != "$status2" ]; then
if [ -n $PIN ]; then
if [ -n ${status[$PIN]} ]; then
gpio -g write $PIN ${status[$PIN]}
if [ "$logging" ]; then
addLogItem "Pin $PIN changed to: ${status[$PIN]}"
fi
elif [ -z ${status[$PIN]} ]; then
addLogItem "PIN status zero"
fi
elif [ -z $PIN ]; then
addLogItem "PIN value zero"
fi
fi
fi
sleep 0.2
done
# Complete Loop.
sleep $waitTime
done
} >> /var/log/GPIOServer.log