· 10 years ago · Feb 07, 2016, 05:33 PM
1#!/bin/bash
2
3# Get time as a UNIX timestamp (seconds elapsed since Jan 1, 1970 0:00 UTC)
4startTime="$(date +%s)"
5columns=$(tput cols)
6user_response=""
7
8# Reset
9Color_Off='\e[0m' # Text Reset
10
11RED=$(tput setaf 1)
12GREEN=$(tput setaf 2)
13NORMAL=$(tput sgr0)
14
15# High Intensity
16IBlack='\e[0;90m' # Black
17IRed='\e[0;91m' # Red
18IGreen='\e[0;92m' # Green
19IYellow='\e[0;93m' # Yellow
20IBlue='\e[0;94m' # Blue
21IPurple='\e[0;95m' # Purple
22ICyan='\e[0;96m' # Cyan
23IWhite='\e[0;97m' # White
24
25# Bold High Intensity
26BIBlack='\e[1;90m' # Black
27BIRed='\e[1;91m' # Red
28BIGreen='\e[1;92m' # Green
29BIYellow='\e[1;93m' # Yellow
30BIBlue='\e[1;94m' # Blue
31BIPurple='\e[1;95m' # Purple
32BICyan='\e[1;96m' # Cyan
33BIWhite='\e[1;97m' # White
34#!/bin/bash
35
36skip=0
37
38clean_stdin()
39{
40while read -r -t 0; do
41 read -n 256 -r -s
42done
43}
44
45# Permanent loop until both passwords are the same..
46function user_input {
47 local VARIABLE_NAME=${1}
48 local VARIABLE_NAME_1="A"
49 local VARIABLE_NAME_2="B"
50 while true; do
51 printf "${BICyan}$2: ${BIWhite}";
52 if [ "$3" = "hide" ] ; then
53 stty -echo;
54 fi
55 read VARIABLE_NAME_1;
56 stty echo;
57 if [ "$3" = "hide" ] ; then
58 printf "\n${BICyan}$2 (again) : ${BIWhite}";
59 stty -echo;
60 read VARIABLE_NAME_2;
61 stty echo;
62 else
63 VARIABLE_NAME_2=$VARIABLE_NAME_1;
64 fi
65 if [ $VARIABLE_NAME_1 != $VARIABLE_NAME_2 ] ; then
66 printf "\n${BIRed}Sorry, did not match!${BIWhite}\n";
67 else
68 break;
69 fi
70 done
71 readonly ${VARIABLE_NAME}=$VARIABLE_NAME_1;
72 if [ "$3" == "hide" ] ; then
73 printf "\n";
74 fi
75}
76
77timecount(){
78 sec=30
79 while [ $sec -ge 0 ]; do
80 printf "${BIPurple}Continue Y(es)/n(0)/s(kip)/a(ll)- 00:0$min:$sec remaining\033[0K\r${BIWhite}"
81 sec=$((sec-1))
82 trap '' 2
83 stty -echo
84 read -t 1 -n 1 user_response
85 stty echo
86 trap - 2
87 if [ -n "$user_response" ]; then
88 break
89 fi
90 done
91}
92
93task_start(){
94printf "${BIGreen}%*s\n" $columns | tr ' ' -
95printf "$1"
96clean_stdin
97skip=0
98printf "\n%*s${BIWhite}\n" $columns | tr ' ' -
99elapsedTime="$(($(date +%s)-startTime))"
100printf "Elapsed Time: %02d hrs %02d mins %02d secs\n" "$((elapsedTime/3600%24))" "$((elapsedTime/60%60))" "$((elapsedTime%60))"
101clean_stdin
102if [ "$user_response" != "a" ]; then
103 timecount
104fi
105echo -e " \033[0K\r"
106if [ "$user_response" = "n" ]; then
107 printf "${BIWhite}"
108 exit 1
109fi
110if [ "$user_response" = "s" ];then
111 skip=1
112fi
113if [ -n "$2" ]; then
114 if [ $skip -eq 0 ]; then
115 printf "${BIYellow}$2${BIWhite}\n"
116 else
117 printf "${BICyan}%*s${BIWhite}\n" $columns '[SKIPPED]'
118 fi
119fi
120}
121
122task_end(){
123 printf "${BICyan}%*s${BIWhite}\n" $columns '[OK]'
124}
125
126
127
128
129# This script is intended for the Orange Pi PC 2 using the basic setup here
130# http://www.orangepi.org/orangepibbsen/forum.php?mod=viewthread&tid=867&extra=page%3D1&page=1
131#
132# Assuming access as orangepi user. Please note this will NOT WORK AS ROOT - The Node-Red install will fail.
133# Do not use this script as SUDO.
134#
135# including Mosquitto with web sockets (Port 9001), SQLITE ( xxx.xxx.xxx.xxx/phpliteadmin),
136# Node-Red-UI (xxx.xxx.xxx.xxx:1880/ui) and Webmin(xxx.xxx.xxx:10000)
137#
138# http://tech.scargill.net - much of this was thanks to help from others!
139#
140# If you want security you need to add this to the settings.js file in /home/orangepi/.node-red
141#
142# Suggested improvements welcome - I'm learning!!
143#
144# functionGlobalContext: {
145# // os:require('os'),
146# // bonescript:require('bonescript'),
147# // jfive:require("johnny-five"),
148# moment:require('moment'),
149# fs:require('fs')
150# },
151
152# adminAuth: {
153# type: "credentials",
154# users: [{
155# username: "admin",
156# password: "your encrypted password see node red site",
157# permissions: "*"
158# }]
159#},
160#
161# httpNodeAuth: {user:"user", pass:"your encrypted password see node red site"},
162
163task_start "Update" "Updating repositories then any programs"
164if [ $skip -eq 0 ]
165then
166cd
167sudo apt-get remove -y --purge nginx nginx-common
168sudo apt-get autoremove -y
169sudo apt-get -qq -o=Dpkg::Use-Pty=0 --yes --force-yes update
170sudo apt-get -qq -o=Dpkg::Use-Pty=0 --yes --force-yes upgrade
171task_end
172fi
173
174task_start "Prerequisites" "Enabling PING and SAMBA (to access the hostname externally - and STUFF"
175if [ $skip -eq 0 ]
176then
177# fix for RPI treating PING as a root function - by Dave
178sudo setcap cap_net_raw=ep /bin/ping
179sudo setcap cap_net_raw=ep /bin/ping6
180# this one ensures the unit shows up on the network by hostname... works a treat
181# sudo apt-get install samba samba-common-bin
182
183
184# Prerequisite suggested by Julian
185sudo apt-get install -y bash-completion unzip build-essential git python-serial scons libboost-filesystem-dev libboost-program-options-dev libboost-system-dev libsqlite3-dev subversion libcurl4-openssl-dev libusb-dev cmake # libboost-thread-dev libboost-all-dev
186task_end
187fi
188
189task_start "Mosquitto" "Loading Mosquitto and setting up user"
190if [ $skip -eq 0 ]
191then
192# installation of Mosquitto/w/Websockets
193cd
194wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
195sudo apt-key add mosquitto-repo.gpg.key
196cd /etc/apt/sources.list.d/
197sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
198sudo apt-get update -y
199cd
200sudo apt-get install -y mosquitto
201
202# Setup kill anonymous, emable websockets then set username and password for accessing Mosquitto
203# As we are PI - create temporary file with changes - then add to Mosquitto.conf
204echo '======================================= UPdating mosquitto.conf'
205cat <<EOT > /tmp/tmpnoderedfile
206listener 9001
207protocol websockets
208listener 1883
209allow_anonymous false
210password_file /etc/mosquitto/passwords
211EOT
212sudo bash -c "cat /tmp/tmpnoderedfile >> /etc/mosquitto/mosquitto.conf"
213rm /tmp/tmpnoderedfile
214
215# Create or append to mosquitto passwords then add user
216sudo bash -c "echo '' >> /etc/mosquitto/passwords"
217user_input mquser "Enter desired user (admin for example) for Mosquitto"
218user_input mqpass "Enter desired password for Mosquitto" "hide"
219sudo mosquitto_passwd -b /etc/mosquitto/passwords $mquser $mqpass
220task_end
221fi
222
223task_start "Node-Red" "Loading Node-Red"
224if [ $skip -eq 0 ]
225then
226# Dave provided most of this - installation of Node-Red and my selection of nodes
227sudo apt-get remove -y nodered nodejs nodejs-legacy npm
228sudo apt-get autoremove -y
229curl -sL https://deb.nodesource.com/setup_4.x | sudo bash -
230sudo apt-get install -y build-essential python-dev nodejs
231sudo npm cache clean
232sudo npm install -g --unsafe-perm node-red
233sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service -O /lib/systemd/system/nodered.service
234sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start -O /usr/bin/node-red-start
235sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop -O /usr/bin/node-red-stop
236sudo sed -i -e 's#=pi#=orangepi#g' /lib/systemd/system/nodered.service
237sudo chmod +x /usr/bin/node-red-st*
238sudo systemctl daemon-reload
239mkdir .node-red
240cd .node-red
241sudo npm install -g node-red-admin
242npm install moment
243npm install node-red-contrib-grove
244npm install node-red-contrib-bigtimer
245npm install node-red-contrib-esplogin
246npm install node-red-node-pushbullet
247npm install node-red-contrib-freeboard
248npm install node-red-node-openweathermap
249npm install node-red-node-google
250npm install node-red-node-sqlite
251npm install node-red-contrib-ui
252npm install node-red-node-emoncms
253npm install node-red-node-geofence
254#npm install node-red-contrib-ivona
255npm install node-red-contrib-moment
256npm install node-red-contrib-particle
257npm install node-red-contrib-graphs
258npm install node-red-node-ledborg
259npm install node-red-node-ping
260npm install node-red-node-random
261npm install node-red-node-smooth
262npm install node-red-contrib-npm
263#npm install raspi-io
264#npm install node-red-contrib-gpio
265npm install node-red-contrib-admin
266npm install node-red-node-arduino
267task_end
268fi
269
270task_start "Install Webmin" "Installing Webmin - expect interaction"
271if [ $skip -eq 0 ]
272then
273cd
274mkdir webmin
275cd webmin
276wget --no-verbose http://prdownloads.sourceforge.net/webadmin/webmin-1.780.tar.gz
277sudo gunzip -q webmin-1.780.tar.gz
278sudo tar xf webmin-1.780.tar
279cd webmin-1.780
280sudo ./setup.sh /usr/local/Webmin
281cd
282cd webmin
283sudo rm *.tar
284task_end
285fi
286
287task_start "Apache Installation with PHP and SQLITE and PHPLITEADMIN" "Installing Apache and PHP and SQLITE and PHPLITEADMIN..."
288if [ $skip -eq 0 ]
289then
290cd
291sudo groupadd -f -g33 www-data
292sudo apt-get -qq --yes -o=Dpkg::Use-Pty=0 --force-yes install apache2 php5 libapache2-mod-php5
293
294
295cd
296sudo apt-get install -y sqlite3 php5-sqlite
297mkdir dbs
298sqlite3 /home/orangepi/dbs/iot.db << EOF
299CREATE TABLE IF NOT EXISTS \`pinDescription\` (
300 \`pinID\` INTEGER PRIMARY KEY NOT NULL,
301 \`pinNumber\` varchar(2) NOT NULL,
302 \`pinDescription\` varchar(255) NOT NULL
303);
304CREATE TABLE IF NOT EXISTS \`pinDirection\` (
305 \`pinID\` INTEGER PRIMARY KEY NOT NULL,
306 \`pinNumber\` varchar(2) NOT NULL,
307 \`pinDirection\` varchar(3) NOT NULL
308);
309CREATE TABLE IF NOT EXISTS \`pinStatus\` (
310 \`pinID\` INTEGER PRIMARY KEY NOT NULL,
311 \`pinNumber\` varchar(2) NOT NULL,
312 \`pinStatus\` varchar(1) NOT NULL
313);
314CREATE TABLE IF NOT EXISTS \`users\` (
315 \`userID\` INTEGER PRIMARY KEY NOT NULL,
316 \`username\` varchar(28) NOT NULL,
317 \`password\` varchar(64) NOT NULL,
318 \`salt\` varchar(8) NOT NULL
319);
320CREATE TABLE IF NOT EXISTS \`device_list\` (
321 \`device_name\` varchar(80) NOT NULL DEFAULT '',
322 \`device_description\` varchar(80) DEFAULT NULL,
323 \`device_attribute\` varchar(80) DEFAULT NULL,
324 \`logins\` int(11) DEFAULT NULL,
325 \`creation_date\` datetime DEFAULT NULL,
326 \`last_update\` datetime DEFAULT NULL,
327 PRIMARY KEY (\`device_name\`)
328);
329CREATE TABLE IF NOT EXISTS \`readings\` (
330 \`location\` varchar(20) NOT NULL,
331 \`value\` int(11) NOT NULL,
332 \`logged\` timestamp NULL DEFAULT CURRENT_TIMESTAMP
333);
334CREATE TABLE IF NOT EXISTS \`pins\` (
335 \`gpio0\` int(11) NOT NULL DEFAULT '0',
336 \`gpio1\` int(11) NOT NULL DEFAULT '0',
337 \`gpio2\` int(11) NOT NULL DEFAULT '0',
338 \`gpio3\` int(11) NOT NULL DEFAULT '0'
339);
340INSERT INTO PINS VALUES(0,0,0,0);
341CREATE TABLE IF NOT EXISTS \`temperature_record\` (
342 \`device_name\` varchar(64) NOT NULL,
343 \`rec_num\` INTEGER PRIMARY KEY,
344 \`temperature\` float NOT NULL,
345 \`date_time\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
346);
347.exit
348EOF
349
350chmod 777 /home/orangepi/dbs
351chmod 666 /home/orangepi/dbs/iot.db
352cd
353
354cd /var/www/html
355sudo mkdir phpliteadmin
356cd phpliteadmin
357sudo wget --no-verbose https://bitbucket.org/phpliteadmin/public/downloads/phpLiteAdmin_v1-9-6.zip
358sudo unzip phpLiteAdmin_v1-9-6.zip
359sudo mv phpliteadmin.php index.php
360sudo mv phpliteadmin.config.sample.php phpliteadmin.config.php
361sudo rm *.zip
362sudo mkdir themes
363cd themes
364sudo wget --no-verbose https://bitbucket.org/phpliteadmin/public/downloads/phpliteadmin_themes_2013-12-26.zip
365sudo unzip phpliteadmin_themes_2013-12-26.zip
366sudo rm *.zip
367user_input litepasswd "Enter desired password for PHPLiteAdmin" "hide"
368sudo sed -i -e 's#\$directory = \x27.\x27;#\$directory = \x27/home/orangepi/dbs/\x27;#g' /var/www/html/phpliteadmin/phpliteadmin.config.php
369sudo sed -i -e "s#\$password = \x27admin\x27;#\$password = \x27$litepasswd\x27;#g" /var/www/html/phpliteadmin/phpliteadmin.config.php
370sudo sed -i -e "s#\$subdirectories = false;#\$subdirectories = true;#g" /var/www/html/phpliteadmin/phpliteadmin.config.php
371cd
372
373task_end
374fi
375
376task_start "MP3 Installation" "Installing mpg123..."
377if [ $skip -eq 0 ]
378then
379sudo apt-get install -y mpg123
380task_end
381fi
382
383#task_start "Internet Time Updater for Webmin" "Ntpdate installing..."
384#if [ $skip -eq 0 ]
385#then
386#sudo apt-get -qq --yes -o=Dpkg::Use-Pty=0 --force-yes install ntpdate
387#task_end
388#fi
389
390
391#task_start "Email SMTP Installation" "Installing mail utils and SMTP..."
392#if [ $skip -eq 0 ]
393#then
394#cd
395#sudo apt-get -qq --yes -o=Dpkg::Use-Pty=0 --force-yes install mailutils ssmtp
396#task_end
397#fi
398
399task_start "Install SCREEN" "Installing SCREEN"
400if [ $skip -eq 0 ]
401then
402cd
403sudo apt-get install screen
404task_end
405fi
406
407sudo service nodered start ; sleep 5 ; sudo service nodered stop
408sed -i -e "s/\/\/\ os:require('os')\,/os:require('os')\,\n\tmoment:require('moment')/g" .node-red/settings.js
409echo " "
410echo "All done. Rebooting"
411sudo reboot