-
Notifications
You must be signed in to change notification settings - Fork 2
/
ups.sh
executable file
·73 lines (57 loc) · 1.63 KB
/
ups.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
#!/bin/bash
#GPIO17 (input) used to read current power status.
#0 - normal (or battery power switched on manually).
#1 - power fault, swithced to battery.
echo "17" > /sys/class/gpio/export;
echo "in" > /sys/class/gpio/gpio17/direction;
#GPIO27 (input) used to indicate that UPS is online
echo "27" > /sys/class/gpio/export;
echo "in" > /sys/class/gpio/gpio27/direction;
#GPIO18 used to inform UPS that Pi is still working. After power-off this pin returns to Hi-Z state.
echo "18" > /sys/class/gpio/export;
echo "out" > /sys/class/gpio/gpio18/direction;
echo "0" > /sys/class/gpio/gpio18/value;
power_timer=0;
inval_power="0";
ups_online1="0";
ups_online2="0";
ups_online_timer="0";
while true
do
#read GPIO27 pin value
#normally, UPS toggles this pin every 0.5s
ups_online1=$(cat /sys/class/gpio/gpio27/value);
sleep 0.1;
ups_online2=$(cat /sys/class/gpio/gpio27/value);
ups_online_timer=$((ups_online_timer+1));
#toggled?
if (( "$ups_online1" != "$ups_online2" )); then
ups_online_timer=0;
fi
#reset all timers if ups is offline longer than 3s (no toggling detected)
if (("$ups_online_timer" > 30));
then
echo "$ups_online_timer";
ups_online_timer=30;
power_timer=0;
inval_power=0;
#echo "UPS offline. Exit";
#exit;
fi
#read GPIO17 pin value
inval_power=$(cat /sys/class/gpio/gpio17/value);
# echo $inval_power;
if (( "$inval_power" == 1 )); then
power_timer=$((power_timer+1));
else
power_timer=0;
fi
#If power was not restored in 60 seconds
if (( "$power_timer" == 600 )); then
#echo $power_timer;
echo "Powering off..."
sleep 2;
systemctl poweroff; #turn off
exit;
fi
done