-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall_fan.sh
86 lines (64 loc) · 1.8 KB
/
install_fan.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
#!/bin/bash
set -e
[[ $- == *i* ]] && tput setaf 2
echo "Installing fan program"
echo """
A 30 x 30 mm Fan 5V 7 mA was used to test this script, but other kinds of fans could be used
GPIO mapping
------------------------------------
| Device | Raspberry Pi |
------------------------------------
| Red wire (VCC 5V) | VGPIO 14 |
| Black wire (Gnd) | Ground |
------------------------------------
"""
[[ $- == *i* ]] && tput sgr0
if [ -d /home/pi/fan ]; then
rm -rf /home/pi/fan;
fi
mkdir -p /home/pi/fan
cat << 'EOF' > /home/pi/fan/fan.py
#!/usr/bin/python3
import os
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
FAN = 14
GPIO.setup(FAN,GPIO.OUT)
GPIO.output(FAN, 0)
def measure_temp():
temp = os.popen("vcgencmd measure_temp").readline()
return (float(temp.replace("temp=", "").replace("'C", "")))
try:
while (True):
time.sleep(1)
temp = measure_temp()
print(temp)
if (temp > 50):
GPIO.output(FAN, 1)
else:
GPIO.output(FAN, 0)
except KeyboardInterrupt:
GPIO.cleanup()
EOF
chmod +x /home/pi/fan/fan.py
cat << 'EOF' > /home/pi/fan/cron.sh
#!/bin/bash
kill $(ps aux | grep '[f]an.py' | awk '{print $2}')
/usr/bin/python3 /home/pi/fan/fan.py > /home/pi/iot.log 2>&1 &
EOF
chmod +x /home/pi/fan/cron.sh
crontab -l > /tmp/crontabentry 2>&1 || true
if grep -q "no crontab" /tmp/crontabentry; then
echo -e "\n* * * * * /home/pi/fan/cron.sh > /home/pi/iot.log 2>&1 \n" > /tmp/crontabentry
crontab /tmp/crontabentry
fi
if ! grep -q "fan/cron.sh" /tmp/crontabentry; then
echo -e "\n* * * * * /home/pi/fan/cron.sh > /home/pi/iot.log 2>&1 \n" >> /tmp/crontabentry
crontab /tmp/crontabentry
fi
crontab -l
[[ $- == *i* ]] && tput setaf 2
echo "Installation complete"
[[ $- == *i* ]] && tput sgr0
exit 0