· 10 years ago · Feb 07, 2016, 08:12 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# disable gui and enable shell login
183sudo systemctl set-default multi-user.target
184
185# Prerequisite suggested by Julian
186sudo 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 mc # libboost-thread-dev libboost-all-dev
187task_end
188fi
189
190task_start "Mosquitto" "Loading Mosquitto and setting up user"
191if [ $skip -eq 0 ]
192then
193# installation of Mosquitto/w/Websockets
194cd
195wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
196sudo apt-key add mosquitto-repo.gpg.key
197cd /etc/apt/sources.list.d/
198sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
199sudo apt-get update -y
200cd
201sudo apt-get install -y mosquitto
202
203# Setup kill anonymous, emable websockets then set username and password for accessing Mosquitto
204# As we are PI - create temporary file with changes - then add to Mosquitto.conf
205echo '======================================= UPdating mosquitto.conf'
206cat <<EOT > /tmp/tmpnoderedfile
207listener 9001
208protocol websockets
209listener 1883
210allow_anonymous false
211password_file /etc/mosquitto/passwords
212EOT
213sudo bash -c "cat /tmp/tmpnoderedfile >> /etc/mosquitto/mosquitto.conf"
214rm /tmp/tmpnoderedfile
215
216# Create or append to mosquitto passwords then add user
217sudo bash -c "echo '' >> /etc/mosquitto/passwords"
218user_input mquser "Enter desired user (admin for example) for Mosquitto"
219user_input mqpass "Enter desired password for Mosquitto" "hide"
220sudo mosquitto_passwd -b /etc/mosquitto/passwords $mquser $mqpass
221task_end
222fi
223
224task_start "Node-Red" "Loading Node-Red"
225if [ $skip -eq 0 ]
226then
227# Dave provided most of this - installation of Node-Red and my selection of nodes
228sudo apt-get remove -y nodered nodejs nodejs-legacy npm
229sudo apt-get autoremove -y
230curl -sL https://deb.nodesource.com/setup_4.x | sudo bash -
231sudo apt-get install -y build-essential python-dev nodejs
232sudo npm cache clean
233sudo npm install -g --unsafe-perm node-red
234sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service -O /lib/systemd/system/nodered.service
235sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start -O /usr/bin/node-red-start
236sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop -O /usr/bin/node-red-stop
237sudo sed -i -e 's#=pi#=orangepi#g' /lib/systemd/system/nodered.service
238sudo chmod +x /usr/bin/node-red-st*
239sudo systemctl daemon-reload
240mkdir .node-red
241cd .node-red
242sudo npm install -g node-red-admin
243npm install moment
244npm install node-red-contrib-grove
245npm install node-red-contrib-bigtimer
246npm install node-red-contrib-esplogin
247npm install node-red-node-pushbullet
248npm install node-red-contrib-freeboard
249npm install node-red-node-openweathermap
250npm install node-red-node-google
251npm install node-red-node-sqlite
252npm install node-red-contrib-ui
253npm install node-red-node-emoncms
254npm install node-red-node-geofence
255#npm install node-red-contrib-ivona
256npm install node-red-contrib-moment
257npm install node-red-contrib-particle
258npm install node-red-contrib-graphs
259npm install node-red-node-ledborg
260npm install node-red-node-ping
261npm install node-red-node-random
262npm install node-red-node-smooth
263npm install node-red-contrib-npm
264#npm install raspi-io
265#npm install node-red-contrib-gpio
266npm install node-red-contrib-admin
267npm install node-red-node-arduino
268task_end
269fi
270
271task_start "Install Webmin" "Installing Webmin - expect interaction"
272if [ $skip -eq 0 ]
273then
274cd
275echo "deb http://download.webmin.com/download/repository sarge contrib" |sudo tee --append /etc/apt/sources.list.d/webmin.list > /dev/null
276wget http://www.webmin.com/jcameron-key.asc
277sudo apt-key add jcameron-key.asc
278sudo apt-get update -y
279sudo apt-get install -y webmin
280# you can login to webmin on https://ip:10000 using user orangepi and his password, default orangepi
281task_end
282fi
283
284task_start "Apache Installation with PHP and SQLITE and PHPLITEADMIN" "Installing Apache and PHP and SQLITE and PHPLITEADMIN..."
285if [ $skip -eq 0 ]
286then
287cd
288sudo groupadd -f -g33 www-data
289sudo apt-get -qq --yes -o=Dpkg::Use-Pty=0 --force-yes install apache2 php5 libapache2-mod-php5
290
291
292cd
293sudo apt-get install -y sqlite3 php5-sqlite
294mkdir dbs
295sqlite3 /home/orangepi/dbs/iot.db << EOF
296CREATE TABLE IF NOT EXISTS \`pinDescription\` (
297 \`pinID\` INTEGER PRIMARY KEY NOT NULL,
298 \`pinNumber\` varchar(2) NOT NULL,
299 \`pinDescription\` varchar(255) NOT NULL
300);
301CREATE TABLE IF NOT EXISTS \`pinDirection\` (
302 \`pinID\` INTEGER PRIMARY KEY NOT NULL,
303 \`pinNumber\` varchar(2) NOT NULL,
304 \`pinDirection\` varchar(3) NOT NULL
305);
306CREATE TABLE IF NOT EXISTS \`pinStatus\` (
307 \`pinID\` INTEGER PRIMARY KEY NOT NULL,
308 \`pinNumber\` varchar(2) NOT NULL,
309 \`pinStatus\` varchar(1) NOT NULL
310);
311CREATE TABLE IF NOT EXISTS \`users\` (
312 \`userID\` INTEGER PRIMARY KEY NOT NULL,
313 \`username\` varchar(28) NOT NULL,
314 \`password\` varchar(64) NOT NULL,
315 \`salt\` varchar(8) NOT NULL
316);
317CREATE TABLE IF NOT EXISTS \`device_list\` (
318 \`device_name\` varchar(80) NOT NULL DEFAULT '',
319 \`device_description\` varchar(80) DEFAULT NULL,
320 \`device_attribute\` varchar(80) DEFAULT NULL,
321 \`logins\` int(11) DEFAULT NULL,
322 \`creation_date\` datetime DEFAULT NULL,
323 \`last_update\` datetime DEFAULT NULL,
324 PRIMARY KEY (\`device_name\`)
325);
326CREATE TABLE IF NOT EXISTS \`readings\` (
327 \`location\` varchar(20) NOT NULL,
328 \`value\` int(11) NOT NULL,
329 \`logged\` timestamp NULL DEFAULT CURRENT_TIMESTAMP
330);
331CREATE TABLE IF NOT EXISTS \`pins\` (
332 \`gpio0\` int(11) NOT NULL DEFAULT '0',
333 \`gpio1\` int(11) NOT NULL DEFAULT '0',
334 \`gpio2\` int(11) NOT NULL DEFAULT '0',
335 \`gpio3\` int(11) NOT NULL DEFAULT '0'
336);
337INSERT INTO PINS VALUES(0,0,0,0);
338CREATE TABLE IF NOT EXISTS \`temperature_record\` (
339 \`device_name\` varchar(64) NOT NULL,
340 \`rec_num\` INTEGER PRIMARY KEY,
341 \`temperature\` float NOT NULL,
342 \`date_time\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
343);
344.exit
345EOF
346
347chmod 777 /home/orangepi/dbs
348chmod 666 /home/orangepi/dbs/iot.db
349cd
350
351cd /var/www/html
352sudo mkdir phpliteadmin
353cd phpliteadmin
354sudo wget --no-verbose https://bitbucket.org/phpliteadmin/public/downloads/phpLiteAdmin_v1-9-6.zip
355sudo unzip phpLiteAdmin_v1-9-6.zip
356sudo mv phpliteadmin.php index.php
357sudo mv phpliteadmin.config.sample.php phpliteadmin.config.php
358sudo rm *.zip
359sudo mkdir themes
360cd themes
361sudo wget --no-verbose https://bitbucket.org/phpliteadmin/public/downloads/phpliteadmin_themes_2013-12-26.zip
362sudo unzip phpliteadmin_themes_2013-12-26.zip
363sudo rm *.zip
364user_input litepasswd "Enter desired password for PHPLiteAdmin" "hide"
365sudo sed -i -e 's#\$directory = \x27.\x27;#\$directory = \x27/home/orangepi/dbs/\x27;#g' /var/www/html/phpliteadmin/phpliteadmin.config.php
366sudo sed -i -e "s#\$password = \x27admin\x27;#\$password = \x27$litepasswd\x27;#g" /var/www/html/phpliteadmin/phpliteadmin.config.php
367sudo sed -i -e "s#\$subdirectories = false;#\$subdirectories = true;#g" /var/www/html/phpliteadmin/phpliteadmin.config.php
368cd
369
370task_end
371fi
372
373task_start "MP3 Installation" "Installing mpg123..."
374if [ $skip -eq 0 ]
375then
376sudo apt-get install -y mpg123
377task_end
378fi
379
380#task_start "Internet Time Updater for Webmin" "Ntpdate installing..."
381#if [ $skip -eq 0 ]
382#then
383#sudo apt-get -qq --yes -o=Dpkg::Use-Pty=0 --force-yes install ntpdate
384#task_end
385#fi
386
387
388#task_start "Email SMTP Installation" "Installing mail utils and SMTP..."
389#if [ $skip -eq 0 ]
390#then
391#cd
392#sudo apt-get -qq --yes -o=Dpkg::Use-Pty=0 --force-yes install mailutils ssmtp
393#task_end
394#fi
395
396task_start "Install SCREEN" "Installing SCREEN"
397if [ $skip -eq 0 ]
398then
399cd
400sudo apt-get install screen
401task_end
402fi
403
404sudo service nodered start ; sleep 5 ; sudo service nodered stop
405sed -i -e "s/\/\/\ os:require('os')\,/os:require('os')\,\n\tmoment:require('moment')/g" .node-red/settings.js
406echo " "
407echo "All done. Rebooting"
408sudo systemctl enable nodered.service
409sudo reboot