forked from SpellFoundry/Sleepy-Pi-Setup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSleepy-Pi-Setup.sh
More file actions
executable file
·310 lines (269 loc) · 9.87 KB
/
Sleepy-Pi-Setup.sh
File metadata and controls
executable file
·310 lines (269 loc) · 9.87 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env bash
set -eu
# trap "set +x; sleep 5; set -x" DEBUG
# Check whether we are running sudo
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# check if it is Jessie or Stretch
osInfo=$(cat /etc/os-release)
if [[ $osInfo == *"jessie"* ]]; then
Jessie=true
elif [[ $osInfo == *"stretch"* ]]; then
Stretch=true
elif [[ $osInfo == *"buster"* ]]; then
Buster=true
else
echo "This script only works on Jessie, Stretch or Buster at this time"
exit 1
fi
echo '================================================================================ '
echo '| |'
echo '| Sleepy Pi Installation Script - Jessie, Stretch or Buster |'
echo '| |'
echo '================================================================================ '
## Update and upgrade
# sudo apt-get update && sudo apt-get upgrade -y
## Detecting Pi model
## list available https://elinux.org/Rpi_HardwareHistory
RPi3=false
RPi4=false
RpiCPU=$(/bin/cat /proc/cpuinfo | /bin/grep Revision | /usr/bin/cut -d ':' -f 2 | /bin/sed -e "s/ //g")
if [ "$RpiCPU" == "a02082" ]; then
echo "Rapberry Pi 3 detected"
RPi3=true
elif [ "$RpiCPU" == "a22082" ]; then
echo "Rapberry Pi 3 B detected"
RPi3=true
elif [ "$RpiCPU" == "a32082" ]; then
echo "Rapberry Pi 3 B detected"
RPi3=true
elif [ "$RpiCPU" == "a020d3" ]; then
echo "Rapberry Pi 3 B+ detected"
RPi3=true
elif [ "$RpiCPU" == "9020e0" ]; then
echo "Rapberry Pi 3 A+ detected"
RPi3=true
elif [ "$RpiCPU" == "9000c1" ]; then
echo "Rapberry Pi Zero W detected"
RPi3=true
elif [ "$RpiCPU" == "a03111" ]; then
echo "Raspberry Pi 4 1GB detected"
RPi4=true
elif [ "$RpiCPU" == "b03111" ]; then
echo "Raspberry Pi 4 2GB detected"
RPi4=true
elif [ "$RpiCPU" == "c03111" ]; then
echo "Raspberry Pi 4 4GB detected"
RPi4=true
else
# RaspberryPi 2 or 1... let's say it's 2...
echo "Non-RapberryPi 3 or 4 detected"
RPi3=false
RPi4=false
fi
echo 'Begin Installation ? (Y/n) '
read ReadyInput
if [[ "$ReadyInput" == "Y" || "$ReadyInput" == "y" ]]; then
echo "Beginning installation..."
else
echo "Aborting installation"
exit 0
fi
##-------------------------------------------------------------------------------------------------
##-------------------------------------------------------------------------------------------------
## Test Area
# echo every line
set +x
# exit 0
## End Test Area
##-------------------------------------------------------------------------------------------------
##-------------------------------------------------------------------------------------------------
## Install Arduino
echo 'Installing Arduino IDE...'
program="arduino"
condition=$(which $program 2>/dev/null | grep -v "not found" | wc -l)
if [ $condition -eq 0 ] ; then
apt-get install -y arduino
# create the default sketchbook and libraries that the IDE would normally create on first run
mkdir /home/pi/sketchbook
mkdir /home/pi/sketchbook/libraries
else
echo "Arduino IDE is already installed - skipping"
fi
##-------------------------------------------------------------------------------------------------
## Enable Serial Port
# Findme look at using sed to toggle it
echo 'Enable Serial Port...'
#echo "enable_uart=1" | sudo tee -a /boot/config.txt
if grep -q 'enable_uart=1' /boot/config.txt; then
echo 'enable_uart=1 is already set - skipping'
else
echo 'enable_uart=1' | sudo tee -a /boot/config.txt
fi
if grep -q 'core_freq=250' /boot/config.txt; then
echo 'The frequency of GPU processor core is set to 250MHz already - skipping'
else
echo 'core_freq=250' | sudo tee -a /boot/config.txt
fi
## Disable Serial login
echo 'Disabling Serial Login...'
set +x
if [ $RPi3 != true ] || [ $RPi4 != true ]; then
# Non-RPi3 or 4
systemctl stop serial-getty@ttyAMA0.service
systemctl disable serial-getty@ttyAMA0.service
else
# Rpi 3 or 4
systemctl stop serial-getty@ttyS0.service
systemctl disable serial-getty@ttyS0.service
fi
## Disable Boot info
echo 'Disabling Boot info...'
#sudo sed -i'bk' -e's/console=ttyAMA0,115200.//' -e's/kgdboc=tty.*00.//' /boot/cmdline.txt
sed -i'bk' -e's/console=serial0,115200.//' /boot/cmdline.txt
## Link the Serial Port to the Arduino IDE
echo 'Link Serial Port to Arduino IDE...'
if [ $RPi3 != true ] || [ $RPi4 != true ]; then
# Anything other than Rpi 3
wget https://raw.githubusercontent.com/SpellFoundry/Sleepy-Pi-Setup/master/80-sleepypi.rules
mv /home/pi/80-sleepypi.rules /etc/udev/rules.d/
fi
# Note: On Rpi3 or 4 GPIO serial port defaults to ttyS0 which is what we want
##-------------------------------------------------------------------------------------------------
## Setup the Reset Pin
echo 'Setup the Reset Pin...'
program="autoreset"
condition=$(which $program 2>/dev/null | grep -v "not found" | wc -l)
if [ $condition -eq 0 ]; then
wget https://github.com/spellfoundry/avrdude-rpi/archive/master.zip
unzip master.zip
cd ./avrdude-rpi-master/
cp autoreset /usr/bin
cp avrdude-autoreset /usr/bin
mv /usr/bin/avrdude /usr/bin/avrdude-original
cd /home/pi
rm -f /home/pi/master.zip
rm -R -f /home/pi/avrdude-rpi-master
ln -s /usr/bin/avrdude-autoreset /usr/bin/avrdude
else
echo "$program is already installed - skipping..."
fi
##-------------------------------------------------------------------------------------------------
## Getting Sleepy Pi to shutdown the Raspberry Pi
echo 'Setting up the shutdown...'
cd ~
if grep -q 'shutdowncheck.py' /etc/rc.local; then
echo 'shutdowncheck.py is already setup - skipping...'
else
mkdir -p /home/pi/bin
mkdir -p /home/pi/bin/SleepyPi
wget https://raw.githubusercontent.com/SpellFoundry/Sleepy-Pi-Setup/master/shutdowncheck.py
mv -f shutdowncheck.py /home/pi/bin/SleepyPi
sed -i '/exit 0/i python /home/pi/bin/SleepyPi/shutdowncheck.py &' /etc/rc.local
# echo "python /home/pi/bin/SleepyPi/shutdowncheck.py &" | sudo tee -a /etc/rc.local
fi
##-------------------------------------------------------------------------------------------------
## Adding the Sleepy Pi to the Arduino environment
echo 'Adding the Sleepy Pi to the Arduino environment...'
# ...setup sketchbook
mkdir -p /home/pi/sketchbook
# ...setup sketchbook/libraries
mkdir -p /home/pi/sketchbook/libraries
# .../sketchbook/hardware
mkdir -p /home/pi/sketchbook/hardware
# .../sketchbook/hardware/sleepy_pi2
if [ -d "/home/pi/sketchbook/hardware/sleepy_pi2" ]; then
echo "sketchbook/hardware/sleepy_pi2 exists - skipping..."
else
mkdir /home/pi/sketchbook/hardware/sleepy_pi2
wget https://raw.githubusercontent.com/SpellFoundry/Sleepy-Pi-Setup/master/boards.txt
mv boards.txt /home/pi/sketchbook/hardware/sleepy_pi2
fi
# .../sketchbook/hardware/sleepy_pi
if [ -d "/home/pi/sketchbook/hardware/sleepy_pi" ]; then
echo "sketchbook/hardware/sleepy_pi exists - skipping..."
else
mkdir /home/pi/sketchbook/hardware/sleepy_pi
wget https://raw.githubusercontent.com/SpellFoundry/Sleepy-Pi-Setup/master/boards.txt
mv boards.txt /home/pi/sketchbook/hardware/sleepy_pi
fi
## Setup the Sleepy Pi Libraries
echo 'Setting up the Sleepy Pi Libraries...'
cd /home/pi/sketchbook/libraries/
if [ -d "/home/pi/sketchbook/libraries/SleepyPi2" ]; then
echo "SleepyPi2 Library exists - skipping..."
# could do a git pull here?
else
echo "Installing SleepyPi 2 Library..."
git clone https://github.com/SpellFoundry/SleepyPi2.git
fi
if [ -d "/home/pi/sketchbook/libraries/SleepyPi" ]; then
echo "SleepyPi Library exists - skipping..."
# could do a git pull here?
else
echo "Installing SleepyPi Library..."
git clone https://github.com/SpellFoundry/SleepyPi.git
fi
if [ -d "/home/pi/sketchbook/libraries/Time" ]; then
echo "Time Library exists - skipping..."
else
echo "Installing Time Library..."
git clone https://github.com/PaulStoffregen/Time.git
fi
if [ -d "/home/pi/sketchbook/libraries/LowPower" ]; then
echo "LowPower Library exists - skipping..."
else
echo "Installing LowPower Library..."
git clone https://github.com/rocketscream/Low-Power.git
# rename the directory as Arduino doesn't like the dash
mv /home/pi/sketchbook/libraries/Low-Power /home/pi/sketchbook/libraries/LowPower
fi
# Sleepy Pi 1
if [ -d "/home/pi/sketchbook/libraries/DS1374RTC" ]; then
echo "DS1374RTC Library exists - skipping..."
else
echo "Installing DS1374RTC Library..."
git clone https://github.com/SpellFoundry/DS1374RTC.git
fi
# Sleepy Pi 2
if [ -d "/home/pi/sketchbook/libraries/PCF8523" ]; then
echo "PCF8523 Library exists - skipping..."
else
echo "Installing PCF8523 Library..."
git clone https://github.com/SpellFoundry/PCF8523.git
fi
if [ -d "/home/pi/sketchbook/libraries/PinChangeInt" ]; then
echo "PinChangeInt Library exists - skipping..."
else
echo "Installing PinChangeInt Library..."
git clone https://github.com/GreyGnome/PinChangeInt.git
fi
cd ~
##-------------------------------------------------------------------------------------------------
# install i2c-tools
echo 'Enable I2C...'
if grep -q '#dtparam=i2c_arm=on' /boot/config.txt; then
# uncomment
sed -i '/dtparam=i2c_arm/s/^#//g' /boot/config.txt
else
echo 'i2c_arm parameter already set - skipping...'
fi
echo 'Install i2c-tools...'
if hash i2cget 2>/dev/null; then
echo 'i2c-tools are installed already - skipping...'
else
sudo apt-get install -y i2c-tools
fi
##-------------------------------------------------------------------------------------------------
echo "Sleepy Pi setup complete!"
echo "Would you like to reboot now? Y/n"
read RebootInput
if [ "$RebootInput" == "Y" ]; then
echo "Now rebooting..."
sleep 3
reboot
fi
exit 0
##-------------------------------------------------------------------------------------------------