-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.sh
More file actions
executable file
·109 lines (84 loc) · 2.56 KB
/
control.sh
File metadata and controls
executable file
·109 lines (84 loc) · 2.56 KB
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
#!/usr/bin/env bash
# WiringPi pin 5 is BCM GPIO 24
# Output for relay to open/close garage
OUT_PIN=5
# File that holds the current state of the garage
STATE_FILE="/home/pi/GarageDoorControl/.state"
# File that holds the current time that the garage was triggered
TIME_FILE="/home/pi/GarageDoorControl/.time"
# File that holds the distance of the garage from the sensor in cm
DISTANCE_FILE="/home/pi/GarageDoorControl/.distance"
# If the garage is closer than this threshold, then it is considered open
DISTANCE_THRESHOLD=100
# How long it takes garage to completely open/close in seconds
MOVING_TIME=12
function set_state()
{
echo $1 > $STATE_FILE
STATE=$1
}
function get_state()
{
STATE=$(cat $STATE_FILE)
DISTANCE=$(cat $DISTANCE_FILE)
DISTANCE=${DISTANCE%.*} # Keep just the integer val
# Since the .distance file is constantly being written, it can be read as
# empty, so we need to check if it's a number
re='^[0-9]+$'
if ! [[ $DISTANCE =~ $re ]]; then
set_state $STATE
return # Return the same state that was read from the file
fi
IS_OPEN=$(( $DISTANCE < $DISTANCE_THRESHOLD ))
if [[ $IS_OPEN -eq 1 ]]; then
set_state OPEN
fi
# The garage has been manually closed
if [[ $IS_OPEN -eq 0 ]] && [[ $STATE == OPEN ]]; then
set_state CLOSING
fi
LAST_TRIGGERED=$(cat $TIME_FILE)
NOW=$(date +%s)
DIFF=$(( $NOW - $LAST_TRIGGERED ))
if [[ $DIFF -ge $MOVING_TIME ]] && [[ $STATE == CLOSING ]]; then
set_state CLOSED
fi
if [[ $DIFF -gt $MOVING_TIME ]] && [[ $STATE == OPENING ]]; then
set_state CLOSED
fi
}
function trigger_garage()
{
# Must be manually untriggered later (doing it this way so that the state
# of the garage can be printed out immediately for better responsiveness of
# the script)
gpio write $OUT_PIN 1
# Save time that the garage was triggered
echo $(date +%s) > $TIME_FILE
}
if [[ $1 == "setup" ]]; then
gpio mode $OUT_PIN out
gpio write $OUT_PIN 0
echo $(date +%s) > $TIME_FILE
fi
if [[ $1 == "open" ]]; then
trigger_garage
set_state OPENING
echo $STATE
# Manually set the output back to 0
sleep 0.5
gpio write $OUT_PIN 0
fi
if [[ $1 == "close" ]]; then
trigger_garage
set_state CLOSING
echo $STATE
# Manually set the output back to 0
sleep 0.5
gpio write $OUT_PIN 0
fi
if [[ $1 == "state" ]]; then
get_state
echo $STATE
fi
echo "$(TZ=":America/Los_Angeles" date) $1 State: $STATE distance: $(cat $DISTANCE_FILE)" >> /home/pi/GarageDoorControl/log